// #define _GLIBCXX_DEBUG // (提出時はコメントアウトする) #include #define rep(i, a, n) for (int i = a; i < (int)(n); i++) #define all(a) begin(a), end(a) #define rall(a) rbegin(a), rend(a) #define vi vector #define vs vector #define vvi vector> #define vb vector #define ll long long #define dd double using namespace std; void faster() { ios::sync_with_stdio(false); cin.tie(0); } template void input(T &x) { cin >> x; } template void input(T &x, Args &...args) { cin >> x; input(args...); } template void sorts(vector &vec) { sort(vec.begin(), vec.end()); } template void rsorts(vector &vec) { sort(vec.begin(), vec.end(), greater()); } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct timekeeper { chrono::high_resolution_clock::time_point starttime; chrono::high_resolution_clock::time_point endtime; chrono::duration duration; }; ll sum_1_to_n(const ll &a) { return a * (a + 1) / 2; } ll rounding(ll target, ll keta) { ll now = 1; rep(i, 0, keta) { target /= now; int k = target % 10; if (k >= 5) { target += 10 - k; } else { target -= k; } target *= now; now *= 10; } return target; } unsigned ll ceiling(const unsigned ll &a, const unsigned ll &b) // a/b切り上げ { return (a + b - 1) / b; } // 上、下、右、左、右上、右下、左上、左下 vi xmove = {0, 0, 1, -1, 1, 1, -1, -1}; vi ymove = {-1, 1, 0, 0, -1, 1, -1, 1}; int main() { int h, w; cin >> h >> w; vector> grid(h, vector(w)); rep(i, 0, h) { rep(j, 0, w) { cin >> grid[i][j]; } } vector> visited(h, vector(w, false)); queue> q; rep(i, 0, h) { rep(j, 0, w) { if (!visited[i][j]) { char targ = grid[i][j]; q.push({i, j}); visited[i][j] = true; set> zahyo; zahyo.insert({i, j}); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); rep(m, 0, 4) { int nx = x + xmove[m]; int ny = y + ymove[m]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) { continue; } else { if (grid[nx][ny] == targ && !visited[nx][ny]) { visited[nx][ny] = true; q.push({nx, ny}); zahyo.insert({nx, ny}); } } } } if (zahyo.size() >= 4) { for (auto &[x, y] : zahyo) { grid[x][y] = '.'; } } } } } rep(i, 0, h) { rep(j, 0, w) { cout << grid[i][j]; } cout << endl; } }