using System.Collections.Generic; using System; public class Hello { public static int[] dx, dy; static void Main() { dx = new int[] { 0, 1, 0, -1 }; dy = new int[] { 1, 0, -1, 0 }; string[] line = Console.ReadLine().Trim().Split(' '); var h = int.Parse(line[0]); var w = int.Parse(line[1]); var map = new char[h, w]; for (int i = 0; i < h; i++) { var s = Console.ReadLine().Trim(); for (int j = 0; j < w; j++) map[i, j] = s[j]; } getAns(h, w, map); } static void bfs(int h, int w, char[,] map, int sx, int sy) { var c = map[sx, sy]; var aa = new List<(int x, int y)>(); var visited = new bool[h, w]; var q = new Queue<(int x, int y)>(); q.Enqueue((sx, sy)); visited[sx, sy] = true; aa.Add((sx, sy)); while (q.Count > 0) { var t = q.Dequeue(); if (visited[t.x, t.y]) continue; for (int i = 0; i < 4; i++) { var nx = t.x + dx[i]; var ny = t.y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx, ny] == c && !visited[nx, ny]) { aa.Add((nx, ny)); q.Enqueue((nx, ny)); visited[nx, ny] = true; } } } if (aa.Count >= 4) foreach (var z in aa) map[z.x, z.y] = '.'; } static void print(int h, int w, char[,] map) { var p = new char[w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) p[j] = map[i, j]; Console.WriteLine(new string(p)); } } static void getAns(int h, int w, char[,] map) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[j, i] != '.') bfs(h, w, map, i, j); } } print(h, w, map); } }