def main():
    fin = open('video.in')
    fout = open('video.out', 'w')
    n, m = map(int, fin.readline().split())
    A = [0] * (n + 1)
    N = [[0] * m for i in range(n)]
    M = [0] * n
    for i in range(n):
        A[i] = fin.readline()
        if A[i][-1] not in '12':
            A[i] = A[i][:-1]
    A[-1] = A[0][:]
    for i in range(n + 1):
        A[i] += A[i][0]
    B = [[0] * m for i in range(n)]
    for i in range(n):
        for j in range(m):
            if A[i][j] == A[i][j + 1] and A[i + 1][j] == A[i + 1][j + 1] and A[i][j] == A[i + 1][j]:
                for t in range(i - 1, i + n - 2):
                    N[t % n][j] += 1
                if i < n - 1:
                    M[i] += 1                
                if i < n - 1 and j < m - 1:
                    B[0][0] += 1
    ans = 0
    for i in range(n):
        if i != 0:
            B[i][0] = B[i - 1][0] - M[i - 1] + M[n - i]
        ans = max(ans, B[i][0])
        for j in range(1, m):
                B[i][j] = B[i][j - 1] - N[i][j - 1] + N[i][m - j]
                ans = max(ans, B[i][j])
    fout.write(str(ans))
    fin.close()
    fout.close()


main()