#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], px[maxn], py[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], 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]; per (i, n, 1) per (j, m, 1) { px[i - 1] = py[j - 1] = 0; rep (k, i, n) px[k] = px[k - 1] + (calc(k, k, j, m) > 0); rep (k, j, m) py[k] = py[k - 1] + (calc(i, n, k, k) > 0); f[i][j] = px[n] * py[m] - calc(i, n, j, m); for (int x = i, y = m; x <= n; x++) { while (y > j && !calc(x + 1, n, y, m)) y--; chkmn(f[i][j], f[x + 1][j] + f[i][y + 1] + px[x] * py[y] - calc(i, x, j, y)); } } cout << f[1][1] << '\n'; }