#include #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair; using pll = std::pair; template void chkmn(T &x, T y) { if (y < x) x = y; } template void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 310; int n, m, a[maxn][maxn], f[maxn][maxn], cnt[maxn][maxn], rd[maxn][maxn]; int calc(int xl, int xr, int yl, int yr) { return cnt[xr][yr] - cnt[xl - 1][yr] - cnt[xr][yl - 1] + cnt[xl - 1][yl - 1]; } int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> m; rep (i, 1, n) rep (j, 1, m) cin >> a[i][j]; rep (i, 1, n) rep (j, 1, m) cnt[i][j] = !a[i][j]; rep (i, 1, n) rep (j, 1, m) cnt[i][j] += cnt[i - 1][j]; rep (i, 1, n) rep (j, 1, m) cnt[i][j] += cnt[i][j - 1]; rep (i, 1, n) rep (j, 1, m) rd[i][j] = a[i][j]; per (i, n, 1) per (j, m, 1) rd[i][j] += rd[i + 1][j]; per (i, n, 1) per (j, m, 1) rd[i][j] += rd[i][j + 1]; per (i, n, 1) per (j, m, 1) { if (calc(i, i, j, m) == m - j + 1) { f[i][j] = f[i + 1][j]; continue; } if (calc(i, n, j, j) == n - i + 1) { f[i][j] = f[i][j + 1]; continue; } f[i][j] = calc(i, n, j, m); if (!rd[i][j]) f[i][j] = 0; rep (x, i, n + 1) rep (y, j, m + 1) if (!rd[x][y]) chkmn(f[i][j], f[x][j] + f[i][y] + calc(i, x - 1, j, y - 1)); } cout << f[1][1] << '\n'; }