use proconio::{input, marker::Chars}; use std::collections::*; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { input! { h: usize, w: usize, g: [Chars; h] } let mut seen = vec![vec![false; w]; h]; let mut ans = vec![vec!['$'; w]; h]; for i in 0..h { for j in 0..w { if seen[i][j] || g[i][j] == '.' { continue; } let mut cc = 0; let mut que = Deque::new(); que.push_back((i, j)); let mut his = Vec::new(); while let Some((x, y)) = que.pop_front() { for &(dx, dy) in [(0, 1), (1, 0), (0, !0), (!0, 0)].iter() { let (x, y) = (x + dx, y + dy); if x < h && y < w { if !seen[x][y] && g[x][y] == g[i][j] { seen[x][y] = true; his.push((x, y)); que.push_back((x, y)); } } } } if his.len() >= 4 { while let Some((x, y)) = his.pop() { ans[x][y] = '.'; } } } } for i in 0..h { for j in 0..w { if ans[i][j] == '$' { ans[i][j] = g[i][j]; } } } for i in 0..h { println!("{}", ans[i].iter().collect::()); } }