#include #include #include #include #include constexpr size_t di[] = {size_t(-1), 0, 1, 0}; constexpr size_t dj[] = {0, size_t(-1), 0, 1}; std::pair dfs(std::vector& s, size_t i, size_t j) { std::pair wb; if (s[i][j] == '.') return {0, 0}; if (s[i][j] == 'w') ++wb.first; if (s[i][j] == 'b') ++wb.second; s[i][j] = '.'; for (int k = 0; k < 4; ++k) { size_t ni = i + di[k]; size_t nj = j + dj[k]; if (!(ni < s.size() && nj < s[0].length())) continue; if (s[ni][nj] == '.') continue; auto tmp = dfs(s, ni, nj); wb.first += tmp.first; wb.second += tmp.second; } return wb; } int main() { size_t N, M; scanf("%zu %zu", &N, &M); std::vector S(N); for (auto& si: S) { char buf[64]; scanf("%s", buf); si = buf; } std::vector> wb; for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) if (S[i][j] != '.') wb.push_back(dfs(S, i, j)); int res = 0; int w = 0; int b = 0; for (const auto& p: wb) { int wi, bi; std::tie(wi, bi) = p; if (wi < bi) { res += 100 * wi; b += bi-wi; } else { res += 100 * bi; w += wi-bi; } } if (w > b) std::swap(w, b); res += 10*w; res += b-w; printf("%d\n", res); }