use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn run() { input! { h: usize, w: usize, s: [bytes; h], } let inf = std::usize::MAX / 10; let mut dp = vec![0; w]; for mut s in s.into_iter().rev() { let mut next = vec![inf; w]; for _ in 0..2 { let rmq = RMQ::new(dp.clone()); let mut cnt = vec![0; w + 1]; for (i, c) in s.iter().enumerate().rev() { cnt[i] += cnt[i + 1]; if *c == b'#' { cnt[i] += 1; } } for i in 0..w { if s[i] == b'#' { continue; } next[i] = next[i].min(dp[i]); let mut ok = w; let mut ng = i; while ok - ng > 1 { let mid = (ok + ng) / 2; if cnt[i] - cnt[mid] == 0 { ok = mid; } else { ng = mid; } } ng = i; while ok - ng > 1 { let mid = (ok + ng) / 2; if rmq.find(i, mid) <= mid - i - 1 { ok = mid; } else { ng = mid; } } next[i] = next[i].min((ok - i - 1).max(rmq.find(i, ok))); } dp.reverse(); s.reverse(); next.reverse(); } dp = next; for (dp, s) in dp.iter_mut().zip(s.iter()) { if *s == b'#' { *dp = inf; } } } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for dp in dp { writeln!(out, "{}", dp).ok(); } } fn main() { run(); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- pub struct RMQ { data: Vec, table: SparseTable, bit: Vec, } impl RMQ where T: Ord + Copy, { pub fn new(data: Vec) -> Self { assert!(!data.is_empty()); let mut bit = vec![0; data.len()]; let w = 8 * std::mem::size_of_val(&bit[0]); let mut stack: Vec = vec![]; let mut table_ini = Vec::with_capacity((data.len() + w - 1) / w); for (bit, data) in bit.chunks_mut(w).zip(data.chunks(w)) { stack.clear(); let mut b = 0; for (i, (bit, d)) in bit.iter_mut().zip(data.iter()).enumerate() { while stack.last().map_or(false, |x| data[*x] > *d) { b ^= 1 << stack.pop().unwrap(); } b |= 1 << i; *bit = b; stack.push(i); } table_ini.push(data[stack[0]]); } let table = SparseTable::new(table_ini); RMQ { data, table, bit } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l < r && r <= self.data.len()); let w = 8 * std::mem::size_of_val(&self.bit[0]); let r = r - 1; let p = l / w; let q = r / w; if p == q { let pos = l + (self.bit[r] >> (l % w)).trailing_zeros() as usize; self.data[pos] } else { let pos = l + (self.bit[l / w * w + w - 1] >> (l % w)).trailing_zeros() as usize; let mut res = self.data[pos]; let pos = r / w * w + (self.bit[r] >> 0).trailing_zeros() as usize; res = std::cmp::min(res, self.data[pos]); if l / w + 1 < r / w { res = std::cmp::min(res, self.table.find(l / w + 1, r / w)); } res } } } // ---------- begin sparse table (min) ---------- pub struct SparseTable { table: Vec>, size: usize, } impl SparseTable where T: Ord + Copy, { pub fn new(mut a: Vec) -> Self { assert!(a.len() > 0); let size = a.len(); let mut table = vec![]; let mut w = 1; while w + 1 <= a.len() { let next = a .iter() .zip(a[w..].iter()) .map(|p| std::cmp::min(*p.0, *p.1)) .collect::>(); table.push(a); a = next; w <<= 1; } table.push(a); SparseTable { table: table, size: size, } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l < r && r <= self.size); let k = (r - l + 1).next_power_of_two().trailing_zeros() as usize - 1; let table = &self.table[k]; std::cmp::min(table[l], table[r - (1 << k)]) } } // ---------- end sparse table (min) ----------