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[,] map2,bool [,] visited, int sx, int sy) { var c = map[sx, sy]; var aa = new List<(int x, int y)>(); 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(); 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; } } } var aac = aa.Count; foreach (var z in aa) map2[z.x, z.y] = aac; } static void print(int h, int w, char[,] map, int[,] map2) { var p = new char[w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i,j] == '.') { p[j] = '.'; continue; } if (map2[i, j] >= 4) p[j] = '.'; else p[j] = map[i, j]; } Console.WriteLine(new string(p)); } } static void getAns(int h, int w, char[,] map) { var map2 = new int[h, w]; var visited = new bool[h, w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map2[i,j] == 0) bfs(h, w, map, map2,visited, i, j); } } print(h, w, map, map2); } }