namespace AtCoder; #nullable enable using System.Numerics; class UnionFind { (int parent, int edgeCount)[] Verticals { get; set; } bool EnabledUndo { get; init; } Stack<(int v, (int parent, int edges))> Histories { get; init; } public UnionFind(int verticals, bool enableUndo = false) { Verticals = new (int, int)[verticals]; for (int i = 0; i < verticals; i++) Verticals[i] = (-1, 0); Histories = new(); EnabledUndo = enableUndo; } public int Union(int x, int y) { (x, y) = (Find(x), Find(y)); var (sx, sy) = (Size(x), Size(y)); if (sx < sy) (x, y, sy) = (y, x, sx); var (nx, ny) = (Verticals[x], Verticals[y]); Histories.Push((x, nx)); Histories.Push((y, ny)); if (x == y) nx.edgeCount += 1; else { ny.parent = x; nx.parent -= sy; nx.edgeCount += ny.edgeCount + 1; } (Verticals[y], Verticals[x]) = (ny, nx); return x; } public int Find(int x) { var pid = Verticals[x].parent; if (pid < 0) return x; var aid = Find(pid); if (!EnabledUndo) Verticals[x].parent = aid; return aid; } public int Size(int x) => -Verticals[Find(x)].parent; public int EdgeCount(int x) => Verticals[Find(x)].edgeCount; public bool Undo() { if (!EnabledUndo || Histories.Count == 0) return false; for (var i = 0; i < 2; i++) { var (v, h) = Histories.Pop(); Verticals[v] = h; } return true; } } static class Extensions { public static T[] Repeat(this int time, Func F) => Enumerable.Range(0, time).Select(_ => F()).ToArray(); } class AtCoder { object? Solve() { var h = Int(); var w = Int(); var g = new char[h, w]; for (var i = 0; i < h; i++) { var str = String().AsSpan(); for (var j = 0; j < w; j++) g[i, j] = str[j]; } var s = h * w; var uf = new UnionFind(s); int ID(int x, int y) => y + x * w; var moves = new[]{ (-1, 0), (0, -1), (0, 1), (1, 0) }; bool Inside(int x, int y) => 0 <= x && x < h && 0 <= y && y < w; for (var i = 0; i < h; i++) for (var j = 0; j < w; j++) { var id = ID(i, j); var c = g[i, j]; if (c == '.') continue; foreach (var (dx, dy) in moves) { var ni = i + dx; var nj = j + dy; if (!Inside(ni, nj) || g[ni, nj] != c) continue; var nid = ID(ni, nj); uf.Union(id, nid); } } for (var i = 0; i < h; i++) { var row = new char[w]; for (var j = 0; j < w; j++) { var id = ID(i, j); if (uf.Size(id) >= 4) row[j] = '.'; else row[j] = g[i, j]; } Out(new string(row)); } return null; } public static void Main() => new AtCoder().Run(); public void Run() { var res = Solve(); if (res != null) { if (res is bool yes) res = yes ? "Yes" : "No"; sw.WriteLine(res); } sw.Flush(); } string[] input = Array.Empty(); int iter = 0; readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false }; string String() { while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0); return input[iter++]; } T Input() where T : IParsable => T.Parse(String(), null); int Int() => Input(); void Out(object? x, string? separator = null) { separator ??= Environment.NewLine; if (x is System.Collections.IEnumerable obj and not string) { var firstLine = true; foreach (var item in obj) { if (!firstLine) sw.Write(separator); firstLine = false; sw.Write(item); } } else sw.Write(x); sw.WriteLine(); } }