#![allow(non_snake_case, unused_must_use, unused_imports)] use std::io::{self, prelude::*}; fn main() { let (stdin, stdout) = (io::read_to_string(io::stdin()).unwrap(), io::stdout()); let (mut stdin, mut buffer) = (stdin.split_whitespace(), io::BufWriter::new(stdout.lock())); macro_rules! input { ($t: ty, $n: expr) => { (0..$n).map(|_| input!($t)).collect::>() }; ($t: ty) => { stdin.next().unwrap().parse::<$t>().unwrap() }; } let H = input!(usize); let W = input!(usize); let G = (0..H) .map(|_| input!(String).chars().collect::>()) .collect::>(); let mut uf = unionfind::UnionFind::new(H * W); let f = |y: usize, x: usize| y * W + x; for i in 0..H { for j in 0..W { if i + 1 < H && G[i][j] == G[i + 1][j] { uf.unite(f(i, j), f(i + 1, j)); } if j + 1 < W && G[i][j] == G[i][j + 1] { uf.unite(f(i, j), f(i, j + 1)); } } } for (i, g) in G.into_iter().enumerate() { writeln!( buffer, "{}", g.into_iter() .enumerate() .map(|(j, c)| if uf.size(f(i, j)) >= 4 { ".".to_string() } else { format!("{}", c) }) .collect::>() .join("") ); } } #[rustfmt::skip] pub mod unionfind {pub struct UnionFind { data: Vec,}impl UnionFind { pub fn new(size: usize) -> Self { return Self { data: vec![-1; size], }; } pub fn is_same(&mut self, u: usize, v: usize) -> bool { assert!(v < self.data.len() && u < self.data.len()); self.find(u) == self.find(v) } pub fn unite(&mut self, mut a: usize, mut b: usize) -> () { assert!(a < self.data.len() && b < self.data.len()); a = self.find(a); b = self.find(b); if a == b { return; } if self.data[a] > self.data[b] { (a, b) = (b, a); } self.data[a] += self.data[b]; self.data[b] = a as i32; } pub fn size(&mut self, mut v: usize) -> i32 { assert!(v < self.data.len()); v = self.find(v); -self.data[v] } pub fn find(&mut self, v: usize) -> usize { assert!(v < self.data.len()); if self.data[v] < 0 { return v; } self.data[v] = self.find(self.data[v] as usize) as i32; return self.data[v] as usize; }}}