#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

char c[1500];
bool ans[1500][1500];

int main(){
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    gets(c);
    int a = 0, b = 0 , bb = 0;
    for (int i = 0; i < n; i++){
        gets(c);
        for (int i = 0; i < m; i++)
            if (c[i] == '1')
                a++;
            else
                b++;
    }
    bb = b;
    int res = 0, best = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (b == 0){
                break;
            }else{
                ans[i][j] = 1;
                b--;
            }

    for (int i = 1; i < n; i++)
        for (int j = 1; j < m; j++)
            if (ans[i][j] == ans[i - 1][j - 1] && ans[i - 1][j] == ans[i][j - 1] && ans[i - 1][j] == ans[i][j]){
                res++;
                //cout << i << " " << j << endl;
            }
    best = res;
    b = bb;
    res = 0;
    memset(ans, 0, sizeof(ans));
    for (int j = 0; j < m; j++)
        for (int i = 0; i < n; i++)
            if (b == 0){
                break;
            }else{
                ans[i][j] = 1;
                b--;
            }

    for (int i = 1; i < n; i++)
        for (int j = 1; j < m; j++)
            if (ans[i][j] == ans[i - 1][j - 1] && ans[i - 1][j] == ans[i][j - 1] && ans[i - 1][j] == ans[i][j])
                res++;
    cout << max(res, best);
    return 0;
}
