inf = open("video.in", "r")
outf = open("video.out", "w")

n, m = map(int, inf.readline().split())
lst = []
for i in range(n):
    lst.append(list(map(str, inf.readline().split()))[0])

glst = [[0] * m for i in range(n)]
for i in range(n):
    for j in range(m):
        if lst[i][j] == lst[(i + 1) % n][j] and lst[i][j] == lst[i][(j + 1) % m] and lst[i][j] == lst[(i + 1) % n][(j + 1) % m]:
            glst[i][j] = 1

pref = [[[0, 0] for j in range(m)] for i in range(n)]

for j in range(m):
    i = n - 1
    sm = 0
    while i > 0:
        sm += glst[i][j]
        i -= 1
    pref[i + 1][j][0] = sm
    while i > (-1) * (n - 1):
        sm -= glst[i + n - 1][j]
        sm += glst[i][j]
        pref[i][j][0] = sm
        i -= 1

for i in range(n):
    j = m - 1
    sm = 0
    while j > 0:
        sm += glst[i][j]
        j -= 1
    pref[i][j + 1][1] = sm
    while j > (-1) * (m - 1):
        sm -= glst[i][j + m - 1]
        sm += glst[i][j]
        pref[i][j][1] = sm
        j -= 1

ans = 0
dm = [[0] * m for i in range(n)] 
for  i in range(n - 1):
    dm[0][0] += pref[i][0][1]
    ans = dm[0][0]
for i in range(n):
    for j in range(m):
        if not (i == 0 and j == 0):
            if j != 0:
                dm[i][j] = dm[i][j - 1] - pref[i][j - 1][0] + pref[i][(j + m - 2) % m][0]
            else:
                dm[i][j] = dm[i - 1][j] - pref[i - 1][j][1] + pref[(i + n - 2) % n][j][1]
            cn = dm[i][j]
            ans = max(cn, ans)
outf.write(str(ans))
outf.close()