import sys sys.setrecursionlimit(10**6) H, W = map(int, input().split()) grid = [list(input()) for _ in range(H)] dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] visited = [[False] * W for _ in range(H)] def dfs(x, y, char): stack = [(x, y)] component = [(x, y)] visited[x][y] = True while stack: cx, cy = stack.pop() for i in range(4): nx, ny = cx + dx[i], cy + dy[i] if 0 <= nx < H and 0 <= ny < W and not visited[nx][ny] and grid[nx][ny] == char: visited[nx][ny] = True stack.append((nx, ny)) component.append((nx, ny)) return component 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]) if len(component) >= 4: for x, y in component: grid[x][y] = '.' for row in grid: print(''.join(row))