#include <bits/stdc++.h>

using namespace std;

char arr[1005][1005];
bool good[1005][1005];
int n, m;
int sumh[1005],sumv[1005];

int main()
{
    ios_base::sync_with_stdio(0);
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    //int n, m;
    cin >> n >> m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >> arr[i][j];
            arr[i][j] -= '1';
        }
    }
    int ans = 0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            good[i][j] = (arr[i][j] + arr[i][(j+1)%m] + arr[(i+1)%n][j] + arr[(i+1)%n][(j+1)%m]) % 4 == 0;
            ans += good[i][j];
            sumh[i] += good[i][j];
            sumv[j] += good[i][j];
        }
    }
    int ma = 1000000000;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ma = min(ma, sumh[i] + sumv[j] - good[i][j]);
        }
    }
    cout << ans - ma;
    return 0;
}
