#f = open('input.txt', 'r')
f = open('video.in', 'r')
ff = open('video.out', 'w')
n, m = map(int, f.readline().split())
a = [[] for i in range(n)]
for i in range(n):
    s = f.readline().rstrip()
    for j in range(m):
        a[i].append(int(s[j]))
stolbec = [0] * m
for j in range(m):
    for i in range(n):
        if a[i][j] == a[(i + 1) % n][j] == a[i][(j + 1) % m] == a[(i + 1) % n][(j + 1) % m]:
            stolbec[j] += 1
stroka = [0] * n
for i in range(n):
    for j in range(m):
        if a[i][j] == a[(i + 1) % n][j] == a[i][(j + 1) % m] == a[(i + 1) % n][(j + 1) % m]:
            stroka[i] += 1
mn = 99999
for i in range(n):
    for j in range(m):
        mn = min(mn, stroka[i] + stolbec[j] - (a[i][j] == a[(i + 1) % n][j] == a[i][(j + 1) % m] == a[(i + 1) % n][(j + 1) % m]))
print(sum(stroka) - mn, file = ff)
ff.close()
#print(sum(stroka) - mn)
