#include #include #include #include #include #include #include #include #include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) using namespace std; template auto vectors(T a, X x) { return vector(x, a); } template auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector(x, cont); } struct edge_t { int to, cap, rev; }; int maximum_flow_destructive(int s, int t, vector > & g) { // ford fulkerson, O(EF) int n = g.size(); vector used(n); function dfs = [&](int i, int f) { if (i == t) return f; used[i] = true; for (edge_t & e : g[i]) { if (used[e.to] or e.cap <= 0) continue; int nf = dfs(e.to, min(f, e.cap)); if (nf > 0) { e.cap -= nf; g[e.to][e.rev].cap += nf; return nf; } } return 0; }; int result = 0; while (true) { used.clear(); used.resize(n); int f = dfs(s, numeric_limits::max()); if (f == 0) break; result += f; } return result; } void add_edge(vector > & g, int from, int to, int cap) { g[from].push_back((edge_t) { to, cap, int(g[ to].size() ) }); g[ to].push_back((edge_t) { from, 0, int(g[from].size() - 1) }); } const int dy[4] = { -1, 1, 0, 0 }; const int dx[4] = { 0, 0, 1, -1 }; int main() { // input int h, w; cin >> h >> w; vector > f = vectors(false, h, w); repeat (y,h) repeat (x,w) { char c; cin >> c; f[y][x] = c != '.'; } // compute auto is_on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; }; vector > g(h * w + 2); auto index = [&](int y, int x) { return y * w + x; }; const int src = h * w; const int dst = h * w + 1; int white = 0, black = 0; repeat (y,h) repeat (x,w) { if (not f[y][x]) continue; if (y % 2 == x % 2) { white += 1; add_edge(g, src, index(y, x), 1); repeat (i,4) { int ny = y + dy[i]; int nx = x + dx[i]; if (not is_on_field(ny, nx)) continue; if (not f[ny][nx]) continue; add_edge(g, index(y, x), index(ny, nx), 1); } } else { black += 1; add_edge(g, index(y, x), dst, 1); } } int flow = maximum_flow_destructive(src, dst, g); int ans = 0; ans += flow * 100; white -= flow; black -= flow; int x = min(white, black); ans += x * 10; white -= x; black -= x; ans += max(white, black); // output cout << ans << endl; return 0; }