·```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 30;
int sum[maxn][maxn];
int dp[maxn][maxn][maxn][maxn];
int n,m;
inline int get_sum(int lx,int ly,int rx,int ry)
{
return sum[rx][ry] - sum[rx][ly - 1] - sum[lx - 1][ry] + sum[lx - 1][ly - 1];
}
inline int DP(int lx,int ly,int rx,int ry)
{
int t = get_sum(lx,ly,rx,ry);
if(t == 1)
{
return 0;
}
if(t == 0)
{
return 100000;
}
if(dp[lx][ly][rx][ry] != 100000)
{
return dp[lx][ly][rx][ry];
}
for(int i(lx);i < rx;++i)
{
dp[lx][ly][rx][ry] = min(dp[lx][ly][rx][ry],DP(lx,ly,i,ry) + DP(i + 1,ly,rx,ry) + ry - ly + 1);
}
for(int i(ly);i < ry;++i)
{
dp[lx][ly][rx][ry] = min(dp[lx][ly][rx][ry],DP(lx,ly,rx,i) + DP(lx,i + 1,rx,ry) + rx - lx + 1);
}
return dp[lx][ly][rx][ry];
}
signed main()
{
int T = 0;
memset(sum,0,sizeof(sum));
cin >> n >> m;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
cin >> sum[i][j];
}
}
for(int i(1);i <= n;++i)
{
for(int j(1);j <= m;++j)
{
sum[i][j] = sum[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}
}
for(int i(1);i <= n;++i)
{
for(int j(1);j <= m;++j)
{
for(int k(1);k <= n;++k)
{
for(int l(1);l <= m;++l)
{
dp[i][j][k][l] = 100000;
}
}
}
}
cout << DP(1,1,n,m) << ‘\n’;
return 0;
}