#include using namespace std; #ifdef LOCAL #include "settings/debug.cpp" #else #define Debug(...) void(0) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; int main() { int h, w; cin >> h >> w; vector Grid(h, vector(w)); rep(i, h) rep(j, w) cin >> Grid[i][j]; rep(i, h) rep(j, w) { if (Grid[i][j] == '.') continue; queue> q; set> cc; q.push({ i, j }); cc.insert({ i, j }); constexpr array dx = { 0, 1, 0, -1 }; constexpr array dy = { 1, 0, -1, 0 }; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); rep(d, 4) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (Grid[nx][ny] == Grid[i][j]) { if (cc.contains({ nx, ny })) continue; q.push({ nx, ny }); cc.insert({ nx, ny }); } } } if (cc.size() >= 4) for (auto [x, y] : cc) Grid[x][y] = '.'; } rep(i, h) { rep(j, w) cout << Grid[i][j]; cout << endl; } return 0; }