#include "bits/stdc++.h" using namespace std; int main() { int h, w; cin >> h >> w; vector> S(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> S[i][j]; } } vector> cum(h + 1, vector(w + 1, 0)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cum[i + 1][j + 1] = cum[i + 1][j] + cum[i][j + 1] - cum[i][j] + S[i][j]; } } auto f = [&](int i0, int i1, int j0, int j1) -> int { return cum[i1][j1] - cum[i0][j1] - cum[i1][j0] + cum[i0][j0]; }; vector zz(h + 1, w); for (int i = 0; i <= h; i++) { while (zz[i] > 0 && f(i, h, zz[i] - 1, w) == 0) zz[i]--; } vector> dp(h + 1, vector(w + 1, 1 << 30)); dp[h][w] = 0; for (int i = h; i >= 0; i--) { for (int j = w; j >= 0; j--) { if (i != h && f(i, i + 1, j, w) == 0) dp[i][j] = min(dp[i][j], dp[i + 1][j]); if (j != w && f(i, h, j, j + 1) == 0) dp[i][j] = min(dp[i][j], dp[i][j + 1]); vector row(h + 1, 0); vector col(w + 1, 0); for (int i2 = i; i2 < h; i2++) row[i2 + 1] = row[i2] + (f(i2, i2 + 1, j, w) > 0 ? 1 : 0); for (int j2 = j; j2 < w; j2++) col[j2 + 1] = col[j2] + (f(i, h, j2, j2 + 1) > 0 ? 1 : 0); for (int i2 = i + 1; i2 <= h; i2++) { int j2 = max(j + 1, zz[i2]); if (j2 > w) continue; int tmp = dp[i2][j] + dp[i][j2] + row[i2] * col[j2] - f(i, i2, j, j2); dp[i][j] = min(dp[i][j], tmp); } } } cout << dp[0][0] << endl; }