#include #include #include #include using namespace std; template ostream& operator<<(ostream& os, const vector& vec) { for (auto &vi: vec) os << vi << " "; return os; } int main() { int h, w; cin >> h >> w; vector s(h); for (auto &si: s) cin >> si; vector field(h + 2, string(w + 2, '.')); // .x for (int i = 0; i < h; i++) field[i + 1] = '.' + s[i] + '.'; h += 2, w += 2; vector> visited(h, vector(w, -1)); vector> g; auto dfs = [&](auto dfs, int i, int j, int id, int &cnt) { if (~visited[i][j]) return ; visited[i][j] = id, cnt++; for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) { if (abs(di) + abs(dj) > 1 + (field[i][j] == 'x')) continue; int ni = i + di, nj = j + dj; if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if (field[ni][nj] == field[i][j]) dfs(dfs, ni, nj, id, cnt); else if (~visited[ni][nj]) g[visited[ni][nj]].emplace(id); } }; int id = 0; vector num; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (visited[i][j] == -1) { int cnt = 0; dfs(dfs, i, j, id, cnt); num.emplace_back(cnt); g.resize(++id); } } vector> dp(id, vector(2)); auto dfs2 = [&](auto dfs2, int u) -> int { dp[u][1] = num[u]; for (int v: g[u]) for (int w: g[v]) { dp[u][0] += dfs2(dfs2, w); dp[u][1] += dp[w][0]; } return max(dp[u][0], dp[u][1]); }; int ans = 0; for (int u: g[0]) ans += dfs2(dfs2, u); cout << ans << endl; return 0; }