#include using namespace std; int main() { int H, W; cin >> H >> W; vector dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1}; vector G(H); for (string &S : G) cin >> S; vector seen(H, vector(W)); queue> Q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (G.at(i).at(j) == '.') continue; int Count = 0; char A = G.at(i).at(j); Q.push({i, j}); while (!Q.empty()){ Count++; int y = Q.front().first, x = Q.front().second; seen.at(y).at(x) = true; Q.pop(); for (int k = 0; k < 4; k++) { int ny = y + dy.at(k), nx = x + dx.at(k); if (ny < 0 || ny >= H || nx < 0 || nx >= W || seen.at(ny).at(nx) || G.at(ny).at(nx) != A) continue; Q.push({ny, nx}); } } if (Count > 3) { Q.push({i, j}); while (!Q.empty()) { int y = Q.front().first, x = Q.front().second; G.at(y).at(x) = '.'; Q.pop(); for (int k = 0; k < 4; k++) { int ny = y + dy.at(k), nx = x + dx.at(k); if (ny < 0 || ny >= H || nx < 0 || nx >= W || G.at(ny).at(nx) != A) continue; Q.push({ny, nx}); } } } } } for (string S : G) cout << S << endl; }