#include using namespace std; const int INF = 2000000000; int dh[] = {1, -1, 0, 0}; int dw[] = {0, 0, 1, -1}; int n, m; int dfs(vector& s, int h, int w, int& c) { int res = 0; if (s[h][w] == 'w') c++; else c--; if (c == 0) res += 100; s[h][w] = '.'; for (int i = 0; i < 4; i++) { int h1 = h + dh[i]; int w1 = w + dw[i]; if (h1 < 0 || h1 >= n || w1 < 0 || w1 >= m) continue; if (s[h1][w1] == '.') continue; res += dfs(s, h1, w1, c); } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vector s(n); for (int i = 0; i < n; i++) cin >> s[i]; int c_all = 0; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int c = 0; if (s[i][j] != '.') ans += dfs(s, i, j, c); if (c_all * c < 0) ans += min(abs(c_all), abs(c)) * 10; c_all += c; } } ans += abs(c_all); cout << ans << "\n"; return 0; }