#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include #include //< in.txt > out.txt using namespace std; //std::ios::sync_with_stdio(false); //std::cin.tie(0); const long long MOD = 998244353; const long long INF = 1e18; typedef long long LL; typedef long double LD; //typedef boost::multiprecision::cpp_int bigint; typedef pair PLL; typedef pair pdl; typedef pair pdd; typedef vector VLL; typedef vector VVLL; typedef unsigned long long ULL; template< typename flow_t > struct Dinic { const flow_t INF; struct edge { int to; flow_t cap; int rev; bool isrev; int idx; }; vector< vector< edge > > graph; vector< int > min_cost, iter; Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {} void add_edge(int from, int to, flow_t cap, int idx = -1) { edge a = { to, cap, (int)graph[to].size(), false, idx }; graph[from].emplace_back(a); a = { from, 0, (int)graph[from].size() - 1, true, idx }; graph[to].emplace_back(a); } bool bfs(int s, int t) { min_cost.assign(graph.size(), -1); queue< int > que; min_cost[s] = 0; que.push(s); while (!que.empty() && min_cost[t] == -1) { int p = que.front(); que.pop(); for (auto& e : graph[p]) { if (e.cap > 0 && min_cost[e.to] == -1) { min_cost[e.to] = min_cost[p] + 1; que.push(e.to); } } } return min_cost[t] != -1; } flow_t dfs(int idx, const int t, flow_t flow) { if (idx == t) return flow; for (int& i = iter[idx]; i < graph[idx].size(); i++) { edge& e = graph[idx][i]; if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) { flow_t d = dfs(e.to, t, min(flow, e.cap)); if (d > 0) { e.cap -= d; graph[e.to][e.rev].cap += d; return d; } } } return 0; } flow_t max_flow(int s, int t) { flow_t flow = 0; while (bfs(s, t)) { iter.assign(graph.size(), 0); flow_t f = 0; while ((f = dfs(s, t, INF)) > 0) flow += f; } return flow; } void output() { for (int i = 0; i < graph.size(); i++) { for (auto& e : graph[i]) { if (e.isrev) continue; auto& rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl; } } } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); LL N, M; cin >> N >> M; vector> maps; maps.resize(M, vector(N, true)); LL bl = 0, wh = 0; for (LL y = 0; y < N; y++) { string S; cin >> S; for (LL x = 0; x < M; x++) { if (S[x] == '.')maps[x][y] = false; else { if ((x + y) & 1)bl++; else wh++; } } } Dinic D(M * N + 2); LL vx[4] = { 0,1,0,-1 }; LL vy[4] = { 1,0,-1,0 }; for (LL x = 0; x < M; x++) { for (LL y = 0; y < N; y++) { if ((x + y) & 1)D.add_edge(x + y * M, M * N + 1, 1); else D.add_edge(M * N, x + y * M, 1); if (!maps[x][y])continue; for (LL v = 0; v < 4; v++) { LL nx = x + vx[v]; LL ny = y + vy[v]; if (nx < 0 || nx >= M || ny < 0 || ny >= N)continue; if (!maps[nx][ny])continue; if ((x + y) & 1)D.add_edge(nx + ny * M, x + y * M, 1); else D.add_edge(x + y * M, nx + ny * M, 1); } } } LL flow = D.max_flow(M * N, M * N + 1); bl -= flow; wh -= flow; LL ans = min(bl, wh); bl -= ans; wh -= ans; ans *= 10; ans += bl + wh; ans += flow * 100; cout << ans << "\n"; return 0; }