import sys import pypyjit H, W = map(int, input().split()) S = [list(input()) for _ in range(H)] pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(2000000) class dsu(): n = 1 parent_or_size = [-1 for i in range(n)] def __init__(self, N): self.n = N self.parent_or_size = [-1 for i in range(N)] def merge(self, a, b): assert 0 <= a < self.n, "0<=a 0: result2.append(result[i]) return result2 dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]] DSU = dsu(H * W) for i in range(H): for j in range(W): if S[i][j] == '.': continue for dx, dy in dxy: ni, nj = i + dx, j + dy if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == S[i][j]: DSU.merge(i * W + j, ni * W + nj) # 連結成分のサイズが4以上の場所を.に変える for g in DSU.groups(): if len(g) < 4: continue for i in g: S[i // W][i % W] = '.' print('\n'.join([''.join(s) for s in S]))