macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[derive(Debug)]
struct Arecur {
    a: Vec<Vec<usize>>,
    w: usize,
    h: usize,
}
impl Arecur {
    pub fn new(a: Vec<Vec<usize>>, w: usize, h: usize) -> Arecur {
        Arecur {
            a: a,
            w: w,
            h: h,
        }
    }

    fn calc(&mut self) -> usize {
        let mut black_st: Vec<(usize, usize)> = Vec::new();
        for i in 0..self.h {
            for j in 0..self.w {
                if self.a[i][j] == 1 {
                    black_st.push((i, j));
                }
            }
        }

        let mut count = 0;
        while black_st.len() != 0 {
            count += 1;
            let mut tmp_st = black_st;
            black_st = Vec::new();
            while let Some((y, x)) = tmp_st.pop() {
                if 0 < y {
                    if self.a[y-1][x] == 0 {
                        black_st.push((y-1, x));
                        self.a[y-1][x] = 1;
                    }
                }
                if y < self.h-1 {
                    if self.a[y+1][x] == 0 {
                        black_st.push((y+1, x));
                        self.a[y+1][x] = 1;
                    }
                }
                if 0 < x {
                    if self.a[y][x-1] == 0 {
                        black_st.push((y, x-1));
                        self.a[y][x-1] = 1;
                    }
                }
                if x < self.w-1 {
                    if self.a[y][x+1] == 0 {
                            black_st.push((y, x+1));
                        self.a[y][x+1] = 1;
                    }
                }
            }
        }

        count-1
    }
}
fn main() {
    input! {
        h: usize,
        w: usize,
        a: [String; h],
    };

    let mut arecur = Arecur::new(a.iter().map(|x| x.chars().map(|c| if c == '.' { 0 } else { 1 }).collect()).collect(), w, h);

    println!("{}", arecur.calc());
}



Measured memory_bytes: 29428