#include using namespace std; int main(){ int H, W; cin >> H >> W; assert(1 <= H && H <= 300); assert(1 <= W && W <= 300); vector a(H + 1, vector(W + 1, 0)); for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ cin >> a[i + 1][j + 1]; assert(0 <= a[i + 1][j + 1] && a[i + 1][j + 1] <= 1); } } for(int i = 0; i <= H; i++){ for(int j = 0; j < W; j++){ a[i][j + 1] += a[i][j]; } } for(int i = 0; i < H; i++){ for(int j = 0; j <= W; j++){ a[i + 1][j] += a[i][j]; } } auto query = [&](int lx, int rx, int ly, int ry){ return a[rx][ry] - a[lx][ry] - a[rx][ly] + a[lx][ly]; }; vector>points; for(int i = 1; i <= H; i++){ for(int j = 1; j <= W; j++){ if(query(i, H, j, W) == 0 && query(i-1, H, j - 1, W) != 0){ points.emplace_back(i, j); } } } vector dp(H + 1, vector(W + 1, 0)); for(int i = H - 1; i >= 0; i--){ for(int j = W - 1; j >= 0; j--){ dp[i][j] = 1e9; vectorrow,col; for(int k = i; k < H; k++){ if(query(k, k+1, j, W) > 0)row.push_back(k); } for(int k = j; k < W; k++){ if(query(i, H, k, k+1) > 0)col.push_back(k); } if(row.empty()){ dp[i][j] = 0; continue; } for(auto [x, y]: points){ if(i >= x || j >= y)continue; int row_cnt = lower_bound(row.begin(), row.end(), x) - row.begin(); int col_cnt = lower_bound(col.begin(), col.end(), y) - col.begin(); dp[i][j] = min(dp[i][j], row_cnt * col_cnt - query(i, x, j, y) + dp[x][j] + dp[i][y]); } } } cout << dp[0][0] << endl; return 0; }