#include using namespace std; typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define PQ priority_queue #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define FOR(it, v) for(auto it = v.begin(); it != v.end(); ++it) #define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it) template bool setmin(T & a, T b) { if(b < a) { a = b; return true; } else { return false; } } template bool setmax(T & a, T b) { if(b > a) { a = b; return true; } else { return false; } } template bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } } template bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- class UnionFind { public: void init(int size); int unite(int v, int w); int find(int v); int get_size_eq(int v); int get_size_part(); virtual ~UnionFind(); private: int *parent; int *size_eq; int size_part; }; void UnionFind::init(int size) { parent = new int[size]; size_eq = new int[size]; for(int i = 0; i < size; i++) { parent[i] = -1; size_eq[i] = 1; } size_part = size; } int UnionFind::unite(int v, int w) { int fv = find(v); int fw = find(w); if(fv == fw) { return fv; } size_part--; int &sv = size_eq[fv]; int &sw = size_eq[fw]; if(sv < sw) { parent[fv] = fw; sw += sv; return fw; } else { parent[fw] = fv; sv += sw; return fv; } } int UnionFind::find(int v) { if(parent[v] == -1) { return v; } return parent[v] = find( parent[v] ); } int UnionFind::get_size_eq(int v) { return size_eq[ find(v) ]; } int UnionFind::get_size_part() { return size_part; } UnionFind::~UnionFind() { delete[] parent; delete[] size_eq; } // ---- #define UQ(v) v.erase(unique(ALL(v)), v.end()) int n, m; string s[1003]; UnionFind uf; vector v[1003 * 1003]; pair dfs(int pos, int par, int t) { auto p1 = MP(0, 0); inc(i, v[pos].size()) { if(v[pos][i] == par) { continue; } auto p2 = dfs(v[pos][i], pos, 1 - t); p1.FI += p2.FI; p1.SE += p2.SE; } if(t == 0) { return p1; } swap(p1.FI, p1.SE); p1.FI += uf.get_size_eq(pos); setmax(p1.FI, p1.SE); return p1; } int main() { cin >> n >> m; s[0] = s[1] = s[n + 2] = string(m + 3, '.'); incID(i, 2, n + 2) { cin >> s[i]; s[i] = ".." + s[i] + "."; } s[0][0] = 'x'; n += 3; m += 3; uf.init(n * m); vector> vp; int di[4] = { 0, 1, 1, 1 }; int dj[4] = { 1, -1, 0, 1 }; inc(q, 2) { inc(i, n) { inc(j, m) { inc(k, 4) { int ii = i + di[k]; if(! inID(ii, 0, n)) { continue; } int jj = j + dj[k]; if(! inID(jj, 0, m)) { continue; } if(q == 0) { if( (s[i][j] == 'x' && s[ii][jj] == 'x') || (s[i][j] == '.' && s[ii][jj] == '.' && k % 2 == 0) ) { uf.unite(m * i + j, m * ii + jj); } } else { if(s[i][j] != s[ii][jj]) { vp.EB(uf.find(m * i + j), uf.find(m * ii + jj)); vp.EB(uf.find(m * ii + jj), uf.find(m * i + j)); } } } } } } sort(ALL(vp)); UQ(vp); inc(i, vp.size()) { v[vp[i].FI].PB(vp[i].SE); } cout << dfs(uf.find(m * 0 + 0), -1, 1).SE << endl; return 0; }