#include #include using namespace std; const int SIZE = 55; const int NODE = SIZE * SIZE; int row, col; char ch[SIZE][SIZE]; void read() { cin >> row >> col; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { cin >> ch[i][j]; } } } bool dfs(int curr, int L2R[NODE], int R2L[NODE], bool visited[NODE], int nNode, bool adj[NODE][NODE]) { if (curr == -1) return true; visited[curr] = true; for (int i = 0; i < nNode; ++i) { if (!adj[curr][i]) continue; int next = R2L[i]; if (next != -1 && visited[next]) continue; if (dfs(next, L2R, R2L, visited, nNode, adj)) { L2R[curr] = i; R2L[i] = curr; return true; } } return false; } int maxMatch(int nNode, bool adj[NODE][NODE]) { int cnt = 0; int L2R[NODE]; int R2L[NODE]; memset(L2R, -1, sizeof(L2R)); memset(R2L, -1, sizeof(R2L)); for (int i = 0; i < nNode; ++i) { bool visited[NODE] = {}; cnt += dfs(i, L2R, R2L, visited, nNode, adj); } return cnt; } void work() { int cntBlack = 0; int cntWhite = 0; static bool adj[NODE][NODE]; memset(adj, 0, sizeof(adj)); for (int r = 0; r < row; ++r) { for (int c = 0; c < col; ++c) { if (ch[r][c] == '.') continue; cntBlack += ch[r][c] == 'b'; cntWhite += ch[r][c] == 'w'; int selfId = r * row + c; static int dr[] = {-1, 0, 1, 0}; static int dc[] = {0, 1, 0, -1}; for (int i = 0; i < 4; ++i) { int nr = r + dr[i]; int nc = c + dc[i]; if (!(0 <= nr && nr < row && 0 <= nc && nc < col)) continue; if (ch[nr][nc] == '.') continue; int oppId = nr * row + nc; if ((r + c) % 2 == 0) { adj[selfId][oppId] = true; } else { adj[oppId][selfId] = true; } } } } int cntMatch = maxMatch(row * col, adj); cntBlack -= cntMatch; cntWhite -= cntMatch; cout << cntMatch * 100 + min(cntBlack, cntWhite) * 10 + max(cntBlack, cntWhite) - min(cntBlack, cntWhite) << endl; } int main() { read(); work(); return 0; }