#include using namespace std; int main () { int H, W; cin >> H >> W; std::vector A(H + 2, vector(W + 2, '#')) ; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j ++) { cin >> A[i][j]; } } vector> did(H + 2, vector(W + 2, -1)); vector siz; for (int i = 1; i <= H; i ++) { for (int j = 1; j <= W; j ++) { if (did[i][j] >= 0 || (A[i][j] == '.')) continue; char c = A[i][j]; int mi[] = {0, 1, 0, -1}, mj[] = {1, 0, -1, 0}; stack> st; st.emplace(i, j); int n = 1; did[i][j] = siz.size(); while (!st.empty()) { auto [p, q] = st.top(); st.pop(); for (int d = 0; d < 4; d ++) { int r = p + mi[d], s = q + mj[d]; if (A[r][s] == c && !did[r][s]) { did[r][s] = did[i][j]; st.emplace(r, s); n ++; } } } siz.push_back(n); } } for (int i = 1; i <= H; i ++) { for (int j = 1; j <= W; j ++) { if (did[i][j] < 0) cout << A[i][j]; else cout << (siz[did[i][j]] >= 4 ? '.' : A[i][j]); } cout << endl; } }