import sys sys.setrecursionlimit(10**6) input = sys.stdin.read # 上下左右の移動方向 DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)] def dfs(x, y, char, visited, component): stack = [(x, y)] while stack: cx, cy = stack.pop() if visited[cx][cy]: continue visited[cx][cy] = True component.append((cx, cy)) for dx, dy in DIRECTIONS: nx, ny = cx + dx, cy + dy if 0 <= nx < H and 0 <= ny < W and not visited[nx][ny] and grid[nx][ny] == char: stack.append((nx, ny)) # 入力の読み込み data = input().splitlines() H, W = map(int, data[0].split()) grid = [list(line) for line in data[1:]] # 訪問済みのセルを管理するための配列 visited = [[False] * W for _ in range(H)] # 連結成分を探して、条件に合うものを.に変換 for i in range(H): for j in range(W): if grid[i][j] != '.' and not visited[i][j]: component = [] dfs(i, j, grid[i][j], visited, component) # 連結成分のサイズが4以上ならば.に置き換え if len(component) >= 4: for x, y in component: grid[x][y] = '.' # 結果を出力 for row in grid: print("".join(row))