#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll LINF = 1e18; inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; } template ostream& operator << (ostream& out,const pair& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template ostream& operator << (ostream& out,const vector V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template ostream& operator << (ostream& out,const vector > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template ostream& operator << (ostream& out,const map mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ int dx[8] = {1,1,0,-1,-1,-1, 0, 1}; int dy[8] = {0,1,1,1 ,0 ,-1,-1,-1}; map mp; void rec(int n,vector>& T,vector>& dp){ ll sum1 = 0; // 使用しない ll sum2 = mp[n]; // 使用する for(auto next:T[n]){ rec(next,T,dp); sum1 += max(dp[next][0],dp[next][1]); sum2 += dp[next][0]; } dp[n][0] = sum1; dp[n][1] = sum2; } ll solve(){ ll res = 0; int N,M; cin >> N >> M; vector> grid(N+2,vector(M+2,'.')); for(int i = 1; i <= N; i++) for(int j = 1; j <= M;j++) cin >> grid[i][j]; vector> checked(N+2,vector(M+2,0)); int dotcnt = 0; int ringcnt = 0; // 連なる.やxをグループ分け for(int i = 0; i < N+2; i++){ for(int j = 0; j < M+2; j++){ if(checked[i][j]!=0) continue; char c = grid[i][j]; int add = 0; if(c == '.'){ dotcnt--; add = 2;} else{ ringcnt++; add = 1;} queue q; q.push({i,j}); while(q.size()){ int x,y; tie(y,x) = q.front(); q.pop(); for(int k = 0; k < 8; k+=add){ int nx = x + dx[k]; int ny = y + dy[k]; if(ny < 0 || nx < 0 || ny >= N+2 || nx >= M+2) continue; if(checked[ny][nx]!=0) continue; if(grid[ny][nx] != c) continue; if(c == '.'){ checked[ny][nx] = dotcnt; q.push({ny,nx}); }else{ checked[ny][nx] = ringcnt; q.push({ny,nx}); } } } } } // グループ分けしたものからグラフを作る int SZ = abs(dotcnt)+1+abs(ringcnt); vector> G(SZ); for(int i = 0; i < N+2; i++){ for(int j = 0; j < M+2; j++){ char c = grid[i][j]; int add = 0; if(c == '.'){ add = 2;} else{ add = 1;} pii S; if(checked[i][j] < 0) S = {abs(checked[i][j]),0}; else S = {abs(dotcnt) + checked[i][j],1}; for(int k = 0; k < 8; k+=add){ int nx = j + dx[k]; int ny = i + dy[k]; if(ny < 0 || nx < 0 || ny >= N+2 || nx >= M+2) continue; if(checked[i][j] == checked[ny][nx]) continue; pii T; if(checked[ny][nx] < 0) T = {abs(checked[ny][nx]),0}; else T = {abs(dotcnt) + checked[ny][nx],1}; G[S.first].push_back(T); G[T.first].push_back(S); } } } for(int i = 0; i < SZ;i++){ sort(G[i].begin(),G[i].end()); G[i].erase(unique(G[i].begin(),G[i].end()),G[i].end()); } // リングの大きさを数える for(int i = 0; i < N+2; i++){ for(int j = 0; j < M+2; j++){ if(checked[i][j] > 0){ mp[checked[i][j]]++; } } } // 外側のリングから内側のリングへグラフを貼って木を作る 0 はコスト0の仮想のリング vector> T(abs(ringcnt)+1); vector flag(abs(ringcnt)+1); for(auto p:G[1]){ T[0].push_back(p.first-abs(dotcnt)); flag[p.first-abs(dotcnt)] = 1;} for(int i = 2; i <= abs(dotcnt);i++){ for(auto s:G[i]){ if(flag[s.first-abs(dotcnt)]==0) continue; for(auto t:G[i]){ if(s.first == t.first) continue; T[s.first-abs(dotcnt)].push_back(t.first-abs(dotcnt)); flag[t.first-abs(dotcnt)] = 1; } break; } } // dp[i][フラグ] := 頂点iを見たとき、自分を使用したかどうか? vector> dp(abs(ringcnt)+1,vector(2)); rec(0,T,dp); res = max(dp[0][0],dp[0][1]); return res; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); cout << solve() << endl; return 0; }