#include #include #include #include #include #include using namespace std; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int n, m; vector choco; bool change() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c1 = choco[i][j]; if (c1 == '.') { continue; } for (int k = 0; k < 4; k++) { int ni = i + dy[k]; int nj = j + dx[k]; char c2 = choco[ni][nj]; if (c2 != '.' && c1 != c2) { choco[i][j] = '.'; choco[ni][nj] = '.'; return true; } } } } return false; } int main() { string s; cin >> n >> m; choco.emplace_back(string(m+2, '.')); for (int i = 0; i < n; i++) { cin >> s; s = "." + s + "."; choco.emplace_back(s); } choco.emplace_back(string(m+2, '.')); int score = 0; while (1) { bool update = change(); if (update) { score += 100; } else { break; } } int nw = 0; int nb = 0; for (auto ccc : choco) { for (auto c : ccc) { if (c == 'b') { nb++; } else if (c == 'w') { nw++; } } } score += 10 * min(nw, nb) + abs(nw - nb); cout << score << endl; return 0; }