#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::*; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok(); return ret; } fn get_word() -> String { let mut stdin = std::io::stdin(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = std::string::String::from_utf8(buf).unwrap(); return ret; } } } fn parse(s: &str) -> T { s.parse::().ok().unwrap() } #[allow(dead_code)] fn get() -> T { parse(&get_word()) } fn main() { let h: i32 = get(); let w: i32 = get(); let s: Vec> = (0 .. h).map(|_| get_word().chars().collect()).collect(); let mut que: VecDeque<(i32, i32, i32)> = VecDeque::with_capacity(((h + 2) * (w + 2)) as usize); let mut dp: Vec> = (0 .. h).map(|_| vec![100_000; w as usize]).collect(); for i in 0 .. w { que.push_back((0, -1, i as i32)); que.push_back((0, h, i as i32)); } for j in 0 .. h { que.push_back((0, j as i32, -1)); que.push_back((0, j as i32, w)); } for i in 0 .. h { for j in 0 .. w { if s[i as usize][j as usize] == '.' { que.push_back((0, i, j)); } } } while let Some((cost, x, y)) = que.pop_front() { if !(x >= 0 && x < h && y >= 0 && y < w) || cost < dp[x as usize][y as usize] { if x >= 0 && x < h && y >= 0 && y < w { dp[x as usize][y as usize] = cost; } for dx in -1 .. 2 { for dy in -1 .. 2 { let nx = x + dx; let ny = y + dy; if nx < 0 || nx >= h || ny < 0 || ny >= w { continue; } que.push_back((cost + 1, nx, ny)); } } } } let mut ma: i32 = 0; for i in 0 .. h { ma = max(ma, *dp[i as usize].iter().max().unwrap()); } println!("{}", ma); }