fin = open('video.in', 'r')
fout = open('video.out', 'w')
input = fin.readline

read = lambda: map(int, input().split())
n, m = read()
a = [input() for i in range(n)]
b = [[0] * m for i in range(n)]
for i in range(n):
    for j in range(m):
        i1, j1 = (i + 1) % n, (j + 1) % m
        f = a[i][j] == a[i1][j] == a[i][j1] == a[i1][j1]
        b[i][j] = int(f)
[print(*i) for i in b]
ans = 0
Sum = sum(sum(i) for i in b)
for i in range(n):
    for j in range(m):
        si = sum(b[i][k] for k in range(m))
        sj = sum(b[k][j] for k in range(n))
        cur = Sum  - si - sj + b[i][j]
        ans = max(ans, cur)
print(ans, file = fout)
print(ans)

fin.close()
fout.close()