#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},cx[n+2][m+2]={0},cm[n+2][m+2]={0};
for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=m+1;j++)
{
pre[i][j]=cx[i][j]=cm[i][j]=0;
// cout<<pre[i][j]<<' ';
}
// cout<<endl;
}
int t1=0,t0=0,mi=0,mx=0;
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][j-1]+a[i][j];
// cx[i][j]=max(max(cx[i-1][j],cx[i][j-1]),pre[i][j]);
// cm[i][j]=min(min(cm[i-1][j],cm[i][j-1]),pre[i][j]);
// mx=max(mx,pre[i][j]-cm[i][j]);
// mi=min(mi,pre[i][j]-cx[i][j]);
// if(i==4 and j==6)
// {
// cout<<"cur "<<endl;
// cout<<pre[i][j]<<' '<<endl;
// cout<<cm[i][j]<<' '<<cx[i][j]<<endl;
// cout<<"endin"<<endl;
// }
}
int tlp[m+2];
// cout<<"doing "<<i<<endl;
// mx=0,mi=0;
for(int j=0;j<=m+1;j++)tlp[j]=0;
for(int i1=i;i1>0;i1--)
{
int mcx=0,mci=0;
for(int j=0;j<=m;j++)
{
tlp[j]+=pre[i1][j];
// cout<<tlp[j]<<' ';
mcx=max(mcx,tlp[j]);
mci=min(mci,tlp[j]);
mx=max(mx,tlp[j]-mci);
mi=min(mi,tlp[j]-mcx);
}
// cout<<endl;
// cout<<"next "<<i1<<endl;
}
// cout<<"Fnl "<<mx<<' '<<mi<<endl;
// cout<<endl;
}
cout<<min(t1-mx,t0+mi)<<endl;
}