#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(), (a).end() struct UnionFind { vector par, siz; int v, group; UnionFind(int n) { par = vector(n, -1); siz = vector(n, 1); v = n; group = n; } int root(int x) { if (par[x] == -1) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; group--; return true; } int size(int x) { return siz[root(x)]; } }; int main() { cin.tie(nullptr); int H, W; cin >> H >> W; vector G(H, vector(W)); rep(i, 0, H) rep(j, 0, W) cin >> G[i][j]; UnionFind uf(H * W); rep(i, 0, H) rep(j, 0, W) { if (i != 0) { if (G[i][j] == G[i - 1][j]) uf.unite(i * W + j, (i - 1) * W + j); } if (i != H - 1) { if (G[i][j] == G[i + 1][j]) uf.unite(i * W + j, (i + 1) * W + j); } if (j != 0) { if (G[i][j] == G[i][j - 1]) uf.unite(i * W + j, i * W + j - 1); } if (j != W - 1) { if (G[i][j] == G[i][j + 1]) uf.unite(i * W + j, i * W + j + 1); } } rep(i, 0, H) rep(j, 0, W) { if (uf.size(i * W + j) >= 4) cout << '.'; else cout << G[i][j]; if (j == W - 1) cout << '\n'; } }