#include #include #include #include using namespace std; int main () { int H, W; cin >> H >> W; vector G(H); for (int i = 0; i < H; i++) cin >> G[i]; vector size(H * W); vector> vis(H, vector(W, -1)); auto is_in = [&] (int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }; const auto dxy = vector>{ make_pair(0, 1), make_pair(1, 0), make_pair(0, -1), make_pair(-1, 0), }; queue> q; int cur = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vis[i][j] != -1) continue; q.push(make_pair(i, j)); vis[i][j] = cur; size[cur]++; while (!q.empty()) { auto pos = q.front(); q.pop(); int y = pos.first, x = pos.second; for (auto& d: dxy) { int ny = y + d.first, nx = x + d.second; if (is_in(ny, nx) && vis[ny][nx] == -1 && G[ny][nx] == G[y][x]) { vis[ny][nx] = cur; size[cur]++; q.push(make_pair(ny, nx)); } } } cur++; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c = G[i][j]; if (4 <= size[vis[i][j]]) c = '.'; cout << c; } cout << "\n"; } }