#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
int a[n+2][m+2]={0},pre[n+2][m+2]={0};
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
pre[i][j]=0;
// cout<<pre[i][j]<<' ';
}
// cout<<endl;
}
int t1=0,t0=0;
int cm=0,mi=1e9,cx=0,mx=-1e9;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]==1)
{
t1++;
a[i][j]=1;
}
else if(a[i][j]==0)
{
t0++;
a[i][j]=-1;
}
pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+a[i][j];
cm=min(cm,pre[i][j]);
cx=max(cx,pre[i][j]);
mx=max(mx,pre[i][j]-cm);
// cout<<pre[i][j]<<' ';
mi=min(mi,pre[i][j]-cx);
}
// cout<<endl;
}
// cout<<mi<<endl;
// cout<<mx<<endl;
// cout<<t1<<' '<<t0<<endl;
cout<<(n*m)-max(t1+mx,t0-mi)<<endl;
}