f_r = open('video.in', 'r')
f_w = open('video.out', 'w')

def check(i, j):
    p = arr[i][j]
    if p == arr[i][j - 1] and p == arr[i - 1][j] and p == arr[i - 1][j - 1]:
        return True
    
    return False

    

n, m = [int(x) for x in f_r.readline().split()]

arr = [[0] * 3 * m for i in range(3 * n)]

for i in range(n):
    a = [int(x) for x in f_r.readline().strip()]
    
    for j in range(m):
        arr[i][j] = a[j]
        
for i in range(n):
    for j in range(m, 3 * m):
        arr[i][j] = arr[i][j - m]
        

for i in range(n, 3 * n):
    for j in range(3 * m):
        arr[i][j] = arr[i - n][j]

ans = None
now = 0

ans_x = [[0] * 3 * m for i in range(3 * n)]
ans_y = [[0] * 3 * m for i in range(3 * n)]

for i in range(1, 3 * n):
    for j in range(1, 3 * m):
        if check(i, j):
            ans_x[i][j] = 1 + ans_x[i][j - 1]
            ans_y[i][j] = 1 + ans_y[i - 1][j]
        else:
            ans_x[i][j] = ans_x[i][j - 1]
            ans_y[i][j] = ans_y[i - 1][j]
            
ans = 0
now = 0

step = 0

for i in range(2 * n + 1):
    if i != 0:
        now -= ans_x[i][m - 1]
        now += ans_x[i + n - 1][m - 1]
    ans = max(ans, now)
    
    
    step = now
    for j in range(2 * m + 1):
        if i == 0 and j == 0:
            for z in range(n):
                ans += ans_x[z][m - 1]
            now = ans
                
        
        if j != 0:
            if i == 0:
                now -= ans_y[i + n - 1][j]    
                now += ans_y[i + n - 1][j + m - 1]
            else:
                now -= ans_y[i + n - 1][j] - ans_y[i][j]
                now += ans_y[i + n - 1][j + m - 1] - ans_y[i][j + m - 1]
                
        if j == 0:
            step = now
        
                
        ans = max(ans, now)       
    now = step     
    
print(ans, file=f_w)   