#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template inline void amin(T &x, U y) { if(y < x) x = y; } template inline void amax(T &x, U y) { if(x < y) x = y; } struct UnionFind { vector data; void init(int n) { data.assign(n, -1); } bool unionSet(int x, int y) { x = root(x); y = root(y); if(x != y) { if(data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; vector memo; int rec(int i, int p, bool b, const vector &g, const vector &grid, UnionFind &uf) { int &r = memo[i * 2 + b]; if(r != -1) return r; if(grid[i / grid[0].size()][i % grid[0].size()] == '.') { r = 0; each(j, g[i]) if(*j != p) r += rec(*j, i, b, g, grid, uf); } else { int t = 0, u = 0; each(j, g[i]) if(*j != p) { t += rec(*j, i, false, g, grid, uf); u += rec(*j, i, true, g, grid, uf); } r = t; if(!b) amax(r, u + uf.size(i)); } return r; } int main() { int N; int M; while(~scanf("%d%d", &N, &M)) { vector grid(N); rep(i, N) cin >> grid[i]; UnionFind uf; uf.init(N * M); rep(i, N) rep(j, M) { rer(dy, -1, 1) rer(dx, -1, 1) if(dy || dx) { int yy = i + dy, xx = j + dx; if(yy == -1 || xx == -1 || yy == N || xx == M) continue; if(grid[i][j] == '.' && abs(dy) + abs(dx) > 1) continue; if(grid[i][j] != grid[yy][xx]) { continue; } uf.unionSet(i * M + j, yy * M + xx); } } vector g(N * M); rep(i, N) rep(j, M) { rer(dy, -1, 1) rer(dx, -1, 1) if(dy || dx) { int yy = i + dy, xx = j + dx; if(yy == -1 || xx == -1 || yy == N || xx == M) continue; if(abs(dy) + abs(dx) > 1) continue; if(grid[i][j] != grid[yy][xx]) { g[uf.root(i * M + j)].push_back(uf.root(yy * M + xx)); continue; } } } rep(i, N * M) { vi &v = g[i]; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } memo.assign(N * M * 2, -1); int ans = rec(uf.root(0), -1, false, g, grid, uf); printf("%d\n", ans); } return 0; }