#include #include #include using namespace std; const int MAX_N = 60; struct BipartiteMatching { int V; // 頂点数 vector > graph; // グラフの隣接リスト表現 vector match; // 各頂点ごとのマッチングの対応 vector used; // DFSの際に使用 BipartiteMatching(int v) { V = v; graph = vector >(V); match = vector(V); used = vector(V); } // uとvを連結にする void AddEdge(int u, int v) { graph[u].push_back(v); graph[v].push_back(u); } bool DFS(int v) { used[v] = true; for (int i = 0; i < graph[v].size(); i++) { int u = graph[v][i], w = match[u]; if (w < 0 || (!used[w] && DFS(w))) { match[v] = u; match[u] = v; return true; } } return false; } // 二部マッチングを解く int Solve() { int res = 0; fill(match.begin(), match.end(), -1); for (int v = 0; v < V; v++) { if (match[v] < 0) { fill(used.begin(), used.end(), false); if (DFS(v)) { res++; } } } return res; } }; int n, m; int conv_id(int row, int col) { return row * m + col; } string s[MAX_N]; int main() { cin >> n >> m; BipartiteMatching bm(n * m); int w_num = 0, b_num = 0; for (int r = 0; r < n; r++) { cin >> s[r]; for (int c = 0; c < m; c++) { if (s[r][c] == 'w') w_num++; else if (s[r][c] == 'b') b_num++; } } for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { if (s[r][c] == 'w') { if (r - 1 >= 0 && s[r - 1][c] == 'b') { bm.AddEdge(conv_id(r, c), conv_id(r - 1, c)); } if (r + 1 < n && s[r + 1][c] == 'b') { bm.AddEdge(conv_id(r, c), conv_id(r + 1, c)); } if (c - 1 >= 0 && s[r][c - 1] == 'b') { bm.AddEdge(conv_id(r, c), conv_id(r, c - 1)); } if (c + 1 < m && s[r][c + 1] == 'b') { bm.AddEdge(conv_id(r, c), conv_id(r, c + 1)); } } } } int matching = bm.Solve(), score3 = matching * 100, score2 = min(w_num - matching, b_num - matching) * 10, score1 = (w_num + b_num) - matching * 2 - score2 / 10 * 2; cout << score1 + score2 + score3 << endl; /* cout << "w: " << w_num << " " << "b: " << b_num << endl; cout << score1 << " " << score2 << " " << score3 << endl; */ return 0; }