#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(){
    ifstream in("video.in");
    ofstream out("video.out");
    int n, m;
    char a;
    in >> n >> m;
    vector<vector<bool> > v(n, vector<bool> (m));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            in >> a;
            if(a == '1') v[i][j] = true;
            else v[i][j] = false;
        }
    }
    int s;
    int summ = 0;
    vector<vector<bool> > v1(n, vector<bool> (m));
    vector<int> res1(n, 0);
    vector<int> res2(m, 0);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            s = int(v[i][j]) + int(v[(i + 1) % n][j]) + int(v[i][(j + 1) % m]) + int(v[(i + 1) % n][(j + 1) % m]);
            if(s == 0 or s == 4){
            summ += 1;
            v1[i][j] = true;
            res1[i] += 1;
            res2[j] += 1;
        }
            else v1[i][j] = false;
        }
    }
    int mmin = 2001;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            mmin = min(mmin, res1[i] + res2[j] - int(v1[i][j]));
        }
    }
    cout << summ - mmin << endl;
    return 0;
}
