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

read = lambda: map(int, input().split())
n, m = read()
arr = [0] * (m + 2)
a = [arr[:]] + [[0]  + list(map(int, input()[:-1])) + [0] for i in range(n)] + [arr[:]]
for i in range(m + 2):
    cur = a[n][(i + m - 1) % m + 1]
    a[0][i] = a[n + 1][i] = cur
for i in range(n + 2):
    cur = a[(i + n - 1) % n + 1][m]
    a[i][0] = a[i][m + 1] = cur
#[print(*i) for i in a]

ans = 0
for di in range(3):
    for dj in range(3):
        cnt = 0
        for i in range(n - 1):
            for j in range(m - 1):
                x, y = i + di, j + dj
                f = a[x][y] == a[x + 1][y] == a[x][y + 1] == a[x + 1][y + 1]
                cnt += int(f)
        ans = max(ans, cnt)
print(ans, file = fout)

fin.close()
fout.close()