def main():
    #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
    stroka = [0] * n
    sm = 0
    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[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)
main()
