#include char g[1003][1003]; int stack[1000006], ss; int used[1003][1003]; int ind[1000006], l; int main() { int h, w; scanf("%d %d", &h, &w); int i, j; for (i = 0; i < h; i++) scanf("%s", g[i]); int ii, jj, k, s, t; int di[4] = { -1,0,1,0 }; int dj[4] = { 0,-1,0,1 }; for (i = 0; i < h; i++) for (j = 0; j < w; j++) used[i][j] = 0; int cnt; char c; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (g[i][j] == '.') used[i][j]++; if (used[i][j] > 0) continue; cnt = 1; ind[0] = w * i + j; l = 1; c = g[i][j]; stack[0] = w * i + j; ss = 1; used[i][j]++; while (ss > 0) { ss--; s = stack[ss]; t = s % w; s /= w; for (k = 0; k < 4; k++) { ii = s + di[k]; jj = t + dj[k]; if (ii < 0 || jj < 0 || ii >= h || jj >= w) continue; if (used[ii][jj] > 0 || g[ii][jj] != c) continue; cnt++; used[ii][jj]++; ind[l] = w * ii + jj; l++; stack[ss] = w * ii + jj; ss++; } } if (cnt > 3) { for (k = 0; k < l; k++) { ii = ind[k] / w; jj = ind[k] % w; g[ii][jj] = '.'; } } } } for (i = 0; i < h; i++) printf("%s\n", g[i]); return 0; }