def main():
    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)
    ans = 0
    Sum = sum(sum(i) for i in b)
    si = [0] * n
    sj = [0] * m
    for i in range(n):
        si[i] = sum(b[i][k] for k in range(m))
    for i in range(m):
        sj[i] = sum(b[k][i] for k in range(n))
    for i in range(n):
        for j in range(m):
            cur = Sum  - si[i] - sj[j] + b[i][j]
            ans = max(ans, cur)
    print(ans)
    
    fin.close()
    fout.close()
main()