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

def get_ans():
    ans = 0
    for i in range(1, n):
        for j in range(1, m):
            x = arr[i][j] 
            if x == arr[i - 1][j] and x == arr[i][j - 1] and arr[i - 1][j - 1] == x:
                ans += 1
                
    return ans


def update_right(y, x):
    delta = 0
    for i in range(y + 1, y + n):
        p = arr[i][x]
        if p == arr[i - 1][x] and p == arr[i][x - 1] and arr[i - 1][x - 1] == p:
            delta -= 1
        
        p = arr[i][x + m - 1]
        if p == arr[i - 1][x + m - 2] and p == arr[i][x + m - 2] and arr[i - 1][x + m - 1] == p:
            delta += 1
            
    return delta
        
            
def update_up(y, x):
    delta = 0
    
    i = y
    for j in range(1, m):
        p = arr[i][j]
        if p == arr[i - 1][j] and p == arr[i][j - 1] and arr[i - 1][j - 1] == p:
            delta -= 1
        
        p = arr[i + n - 1][j]
        if p == arr[i + (n - 1) - 1][j] and p == arr[i + (n - 1)][j - 1] and arr[i + (n - 1) - 1][j - 1] == p:
            delta += 1
    
    return delta
    

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
for i in range(2 * n + 1):
    if i != 0:
        now += update_up(i, 0)
    
    for j in range(2 * m + 1):
        if i == 0 and j == 0:
            ans = get_ans()
            now = ans
            continue
            
        if j != 0:
            now += update_right(i, j)        
        
        ans = max(ans, now)
            
    ans = max(ans, now)

print(ans, file=f_w)
f_w.close()
f_r.close()