//Shirasu Azusa 2024.03 #include #define fi first #define se second #define eb emplace_back #define mp make_pair using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef __int128 i128; template T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);} template T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);} template bool chmax(T &a,const S b) {return (a bool chmin(T &a,const S b) {return (a>b?a=b,1:0);} int popcnt(int x) {return __builtin_popcount(x);} int popcnt(ll x) {return __builtin_popcountll(x);} int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));} int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));} int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));} int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));} #define int long long #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) typedef pair pii; typedef vector vi; typedef vector vp; typedef tuple tiii; int read() { int x=0,w=1; char c=getchar(); while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();} while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} return x*w; } const int N=305; int n,m,a[N][N],f[N][N],s[N][N]; int sum(int u,int d,int l,int r) { if(u>d||l>r) return 0; else return s[d][r]-s[u-1][r]-s[d][l-1]+s[u-1][l-1]; } signed main() { n=read(), m=read(); rep(i,1,n) rep(j,1,m) a[i][j]=read(); rep(i,1,n) rep(j,1,m) s[i][j]=s[i-1][j]+a[i][j]; rep(i,1,n) rep(j,1,m) s[i][j]=s[i][j]+s[i][j-1]; per(i,n,1) per(j,m,1) { static int cx[N],cy[N]; cx[i-1]=cy[j-1]=0; if(!sum(i,n,j,m)) continue; rep(k,i,n) cx[k]=cx[k-1]+!!sum(k,k,j,m); rep(k,j,m) cy[k]=cy[k-1]+!!sum(i,n,k,k); f[i][j]=(n-i+1)*(m-j+1); for(int x=i,y=m;x<=n;x++) { while(y>=j&&!sum(x+1,n,y+1,m)) y--; y++; chmin(f[i][j],f[x+1][j]+f[i][y+1]+cx[x]*cy[y]-sum(i,x,j,y)); } } printf("%lld\n",f[1][1]); return 0; }