#include using namespace std; using ll = long long; using vi = vector; using vb = vector; using vd = vector; using vl = vector; using vvi = vector; using vvb = vector; using vvd = vector; using vvl = vector; #define REP(i,n) for(ll i=0; i<(n); ++i) #define FOR(i,b,n) for(ll i=(b); i<(n); ++i) #define ALL(v) (v).begin(), (v).end() #define TEN(x) ((ll)1e##x) struct Node{ ll size = 0; vector children; }; int dx[] = { -1, 0, 0, 1,-1, 1, -1, 1 }; int dy[] = { 0, -1, 1, 0, 1, 1, -1,-1 }; bool contain(ll n, ll x) { return 0 <= x && x < n; } void blank_dfs(const vvb & table, ll x, ll y, vvb & visited, vector> * discover) { ll n = table.size(); ll m = table[0].size(); stack> st; st.push({ x, y }); while (!st.empty()) { ll x = st.top().first; ll y = st.top().second; st.pop(); if (visited[y][x]) continue; visited[y][x] = true; REP(i, 4) { ll nx = x + dx[i]; ll ny = y + dy[i]; if (!contain(m, nx) || !contain(n, ny)) continue; if (visited[ny][nx]) continue; if (table[ny][nx]) { if(discover!=nullptr) discover->push_back({ nx, ny }); } else st.push({ nx, ny }); } } } Node ring_search(const vvb & table, ll x, ll y, vvb & visited); void blank_search(const vvb & table, ll x, ll y, vvb & visited, vector & nodes) { vector> discover; blank_dfs(table, x, y, visited, &discover); for (auto pos : discover) { if (visited[pos.second][pos.first]) continue; nodes.emplace_back(ring_search(table, pos.first, pos.second, visited)); } } ll ring_dfs(const vvb & table,ll x, ll y, vvb & visited) { ll n = table.size(); ll m = table[0].size(); ll px = -1, py = -1, cnt = 0; while (true) { if (visited[y][x]) return cnt; visited[y][x] = true; REP(i, 8) { ll nx = x + dx[i]; ll ny = y + dy[i]; if (!contain(m, nx) || !contain(n, ny)) continue; if (nx == px && ny == py) continue; if (table[ny][nx]) { cnt++; px = x; py = y; x = nx; y = ny; break; } } } } Node ring_search(const vvb & table, ll x, ll y, vvb & visited) { ll n = table.size(); ll m = table[0].size(); Node node; node.size = ring_dfs(table, x, y, visited); REP(i, 8) { ll nx = x + dx[i]; ll ny = y + dy[i]; if (!contain(m, nx) || !contain(n, ny)) continue; if (!visited[ny][nx] && !table[ny][nx]) { blank_search(table, nx, ny, visited, node.children); return node; } } return node; } pair dfs(Node & node) { auto ans = make_pair(node.size, 0); for (auto&& child : node.children) { auto _ = dfs(child); ans.first += _.second; ans.second += max(_.first, _.second); } return ans; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); ll n, m; cin >> n >> m; vvb table(n, vb(m)); REP(i, n) REP(j, m) { char c; cin >> c; table[i][j] = c!='.'; } vvb visited(n, vb(m, false)); REP(y, n) { if (!visited[y][0] && !table[y][0]) blank_dfs(table, 0, y, visited, nullptr); if (!visited[y][m - 1] && !table[y][m - 1]) blank_dfs(table, m - 1, y, visited, nullptr); } REP(x, m) { if (!visited[0][x] && !table[0][x]) blank_dfs(table, x, 0, visited, nullptr); if (!visited[n - 1][x] && !table[n - 1][x]) blank_dfs(table, x, n - 1, visited, nullptr); } vector nodes; REP(y, n) REP(x, m){ if (visited[y][x]) continue; nodes.emplace_back(ring_search(table, x, y, visited)); } ll ans = 0; for (auto&& child : nodes) { auto _ = dfs(child); ans += max(_.first, _.second); } cout << ans << endl; return 0; }