from collections import deque def bfs(si, sj, moji):#sensorsからパクった que = deque() que.append((si, sj)) positions = [] while que: oi, oj = que.popleft() positions.append((oi, oj)) for ni, nj in neighbor8(oi, oj): if not seen[ni][nj] and S[ni][nj] == moji: seen[ni][nj] = True que.append((ni, nj)) return positions def neighbor8(i, j):#sensorsからパクった2 lst = [] moved=[(-1,0),(1,0),(0,-1),(0,1)] for mi, mj in moved: ni, nj = i+mi, j+mj if 0 <= ni < H and 0 <= nj < W: lst.append((ni, nj)) return lst H, W = map(int, input().split()) S = [list(input()) for _ in range(H)] seen = [[False] * W for _ in range(H)] for i in range(H): for j in range(W): if not seen[i][j]: positions = bfs(i, j, S[i][j]) if len(positions) > 4: for pi, pj in positions: S[pi][pj] = '.' for row in S: print(''.join(row))