#include using namespace std; int main() { int H, W; cin >> H >> W; assert(1 <= H && H <= 22); assert(1 <= W && W <= 22); vector S(H, vector(W, 0)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> S[i][j]; assert(S[i][j] == 0 || S[i][j] == 1); } } vector dp(H + 1, vector(W + 1, 0)); for (int i = H - 1; i >= 0; i--) { for (int j = W - 1; j >= 0; j--) { vector row(H + 1, 0); vector col(W + 1, 0); vector ng(H + 1, vector(W + 1, false)); vector count(H + 1, vector(W + 1, 0)); for (int x = H - 1; x >= i; x--) { for (int y = W - 1; y >= j; y--) { if (S[x][y] or ng[x + 1][y] or ng[x][y + 1]) { ng[x][y] = true; } if (S[x][y]) { count[x + 1][y + 1]++; row[x + 1] = 1; col[y + 1] = 1; } } } for (int x = 0; x < H; x++) { row[x + 1] += row[x]; } for (int y = 0; y < W; y++) { col[y + 1] += col[y]; } int mi = 1e9; for (int x = i + 1; x <= H; x++) { for (int y = j + 1; y <= W; y++) { count[x][y] += count[x - 1][y] + count[x][y - 1] - count[x - 1][y - 1]; if (!ng[x][y]) { mi = min(mi, dp[i][y] + dp[x][j] + row[x] * col[y] - count[x][y]); } } } dp[i][j] = mi; } } cout << dp[0][0] << endl; return 0; }