#include <bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(nullptr)->sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
  #endif
  int N, M;  cin >> N >> M;
  vector<string> grid(N);
  for(auto& row: grid) cin >> row;
  vector vis(N, vector(M, false));

  const int dirs[] = {0, 1, 0, -1, 0};
  for(int i = 0; i < N; ++i){
    for(int j = 0; j < M; ++j){
      if(!vis[i][j]){
        char c = grid[i][j];
        vis[i][j] = true;
        queue<pair<int, int>> q{{{i, j}}};
        vector<pair<int, int>> tmp;
        while(!q.empty()){
          auto [x, y] = q.front();
          // print__(x, y);
          tmp.emplace_back(x, y);
          q.pop();
          for(int k = 0; k < 4; ++k){
            const int nx = x + dirs[k];
            const int ny = y + dirs[k + 1];
            if(nx < 0 || ny < 0 || nx == N || ny == M) continue;
            if(vis[nx][ny] || grid[nx][ny] != c) continue;
            vis[nx][ny] = true;
            q.emplace(nx, ny);
          }
        }
        // print__("________________________");
        if(tmp.size() >= 4){
          for(const auto& [x, y]: tmp){
            grid[x][y] = '.';
          }
        }
      }
    }
  }
  for(auto& row: grid) cout << row << '\n';
  return 0;
}