#include #include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const ll MOD = 1000000007; //const ll MOD = 998244353; const double pi = 3.14159265358979323846; #define Sp(p) cout< idx2p(h*w); vll c(h, vl(w)); ll solve() { UnionFind uf(25); ll ans = INF; rep (l, 16) { for (int r = l; r < 16; r++) { if (l == r and l != 0) continue; vll color(h, vl(w)); for (int i = l; i < r; i++) { color[idx2p[i].first][idx2p[i].second] = 1; } rep (i, 1 << 9) { uf = UnionFind(25); rep (j, 9) { int u = 16 + j; int y = idx2p[u].first, x = idx2p[u].second; if (i & (1 << j)) color[y][x] = 1; else color[y][x] = 0; } rep (y, h) { rep (x, w) { if (y != h - 1) { if (color[y][x] == color[y + 1][x]) uf.unite(p2idx[y][x], p2idx[y + 1][x]); } if (x != w - 1) { if (color[y][x] == color[y][x + 1]) uf.unite(p2idx[y][x], p2idx[y][x + 1]); } } } if (uf.g != 2) continue; ll sa = 0; rep (y, h) rep (x, w) { if (color[y][x]) sa += c[y][x]; else sa -= c[y][x]; } chmin(ans, abs(sa)); } } } return ans; } int main() { rep (i, h) rep (j, w) cin >> c[i][j]; int cnt = 0; rep (j, 5) { p2idx[0][j] = cnt; idx2p[cnt] = pii(0, j); cnt++; } rep1 (i, 3) { p2idx[i][4] = cnt; idx2p[cnt] = pii(i, 4); cnt++; } rrep (j, 5) { p2idx[4][j] = cnt; idx2p[cnt] = pii(4, j); cnt++; } rrep1 (i, 3) { p2idx[i][0] = cnt; idx2p[cnt] = pii(i, 0); cnt++; } rep1 (i, 3) { rep1 (j, 3) { p2idx[i][j] = cnt; idx2p[cnt] = pii(i, j); cnt++; } } //DEBUG_MAT(p2idx); //DEBUG_VEC(idx2p); ll ans = solve(); cout << ans << endl; }