// ACM-ICPC国内予選2010 D. ぐらぐら #include #include #include using namespace std; char dx[] = {0, 1, 0, -1}; char dy[] = {1, 0, -1, 0}; int w, h; string s[60]; bool check[60][10]; bool isStable(int _y, int _x, int *rootM, int *rootCnt){ bool res = true; int M = 2, cnt = 4, XL = 100, XR = -100; queue< pair > qu; qu.push(make_pair(_y,_x)); while(!qu.empty()){ pair p = qu.front(); qu.pop(); int y = p.first, x = p.second; if(check[y][x]) continue; check[y][x] = true; M += x; if(y==h-1||(s[y+1][x]!='.'&&s[y+1][x]!=s[y][x])){ XL = min(XL, x ); XR = max(XR, x+1); } if(y!=0&&!check[y-1][x]&&s[y-1][x]!='.'&&s[y-1][x]!=s[y][x]) if(!isStable(y-1, x, &M, &cnt)) return false; for(int k=0;k<4;k++){ int ny = y+dy[k], nx = x+dx[k]; if(ny<0||h<=ny||nx<0||w<=nx) continue; if(s[ny][nx]=='.'||s[ny][nx]!=s[y][x]) continue; qu.push(make_pair(ny, nx)); } } *rootM += M; *rootCnt += cnt; return cnt*XL < M && M < cnt*XR; } int main(){ int dM, dC; while(cin >> w >> h, w){ for(int i=0;i> s[i]; memset(check, false, sizeof(check)); for(int i=0;i