#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++ ) using ll = long long; int main() { int H, W; cin >> H >> W; vector s(H); rep(i, H) cin >> s[i]; vector dy{1, -1, 0, 0}, dx{0, 0, 1, -1}; rep(i, H) rep(j, W) { char c = s[i][j]; if(c == '.') continue; queue q; int t = i * W + j; set st; st.insert(t); q.push(t); while(q.size()) { int u = q.front(); q.pop(); int y = u / W; int x = u % W; rep(k, 4) { int ny = y + dy[k]; int nx = x + dx[k]; if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue; if(s[ny][nx] != c) continue; t = ny * W + nx; if(st.count(t)) continue; st.insert(t); q.push(t); } } if(st.size() > 3) { for(int e : st) { int y = e / W; int x = e % W; s[y][x] = '.'; } } } rep(i, H) cout << s[i] << endl; }