/***************************************************************************** * Surrounding Area (Practice for domestic contest, 2007) * * Copyright(C) 2007, T.Yoshino *****************************************************************************/ #include #include #include using namespace std; /***** DEFINITIONS ***********************************************************/ #define BLACK 1 #define WHITE 2 #define STONE 4 #define XY_LOOP \ for(int y = 0; y < H; y++) for(int x = 0; x < W; x++) /***** MAIN ROUTINE **********************************************************/ static inline void fill(vector >& table, int color) { const int W = table[0].size(), H = table.size(); queue > Q; // use stack<...> instead to perform DFS #define add(x, y) Q.push(make_pair(x, y)) #define distr() \ do { if(y > 0) add(x, y - 1); if(y < H - 1) add(x, y + 1); \ if(x > 0) add(x - 1, y); if(x < W - 1) add(x + 1, y); } while(0) XY_LOOP if(table[y][x] == (STONE|color)) distr(); while(! Q.empty()) { const int x = Q.front().first, y = Q.front().second; Q.pop(); if((table[y][x] & STONE) || (table[y][x] & color) == color) continue; table[y][x] |= color; // state of (x,y) changed distr(); } #undef distr #undef add } int main() { int W, H; while(cin >> W >> H, W || H) { vector > table(H, vector(W, 0)); XY_LOOP { char c; cin >> c; switch(c) { case 'B': table[y][x] = BLACK|STONE; break; case 'W': table[y][x] = WHITE|STONE; break; } } fill(table, BLACK); fill(table, WHITE); int blacks = 0, whites = 0; XY_LOOP if(table[y][x] == BLACK) blacks++; else if(table[y][x] == WHITE) whites++; cout << blacks << ' ' << whites << endl; } }