#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(a) (a).begin(),(a).end() #include using namespace atcoder; #define int long long using ll = long long; using P = pair; using mint = modint998244353; int INF=1e18+1; template inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;} template inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;} template inline T lcm(T a, T b) {return (a * b) / gcd(a, b);} template inline T ceil(T a,T b) {return (a+(b-1))/b;} template inline void input(T&... a) { ((cin >> a), ...); } int H, W; vector> grid; vector> visited; struct Position { int x, y; }; const vector directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; bool is_in_grid(int x, int y) { return x >= 0 && x < H && y >= 0 && y < W; } vector dfs(int x, int y, char target_char) { vector component; stack stk; stk.push({x, y}); visited[x][y] = true; while (!stk.empty()) { Position pos = stk.top(); stk.pop(); component.push_back(pos); for (const Position &dir : directions) { int nx = pos.x + dir.x; int ny = pos.y + dir.y; if (is_in_grid(nx, ny) && !visited[nx][ny] && grid[nx][ny] == target_char) { visited[nx][ny] = true; stk.push({nx, ny}); } } } return component; } signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input(H, W); grid.resize(H, vector(W)); visited.resize(H, vector(W, false)); rep(i, H) { rep(j, W) { input(grid[i][j]); } } rep(i, H) { rep(j, W) { if (grid[i][j] != '.' && !visited[i][j]) { vector component = dfs(i, j, grid[i][j]); if (component.size() >= 4) { for (const Position &pos : component) { grid[pos.x][pos.y] = '.'; } } } } } rep(i, H) { rep(j, W) { cout << grid[i][j]; } cout << endl; } }