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)*}
    };
    
    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $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")
    };
}
 
//
// # Examples
//
// # integers
// input! { n: usize, m: usize, ... }
//
// # array
// input! { n: usize, v: [i32; n], }
//
// # matrix
// input! { n: usize, m: usize, mat: [[]] }
//
// # string
// input! { s: chars }
//
// 3 4
// ####
// #..#
// .#.#
//
// input!{
//     h: usize,
//     w: usize,
//     board: [chars; h],
// }
 
// https://practice.contest.atcoder.jp/tasks/practice_1


fn main() {
    input! { h: usize, w: usize, board: [chars; h]}
    let mut cnt = 0;
    let mut nokori = h*w;
    let mut init = vec![];
    let mut black = vec![];
    let mut check = vec![vec![false; w];h];
    for i in 0..h {
        for j in 0..w {
            if board[i][j] == '#' {
                check[i][j] = true;
                nokori -= 1;
                init.push((i,j));
            }
        }
    }
    black.push(init);
    while nokori > 0 {
        let mut a = black.pop().unwrap();
        let mut tmp = vec![];
        let mut tmpsize = 0;
        for (i,j) in a {
            //println!("(i,j) ({}, {})", i,j);
            if nokori <= 0 { break; }
            if i >= 1 && !check[i-1][j] {
                check[i-1][j] = true;tmp.push((i-1, j));nokori -= 1; tmpsize += 1;
            }
            if i+1 <= h-1 && !check[i+1][j] {
                check[i+1][j] = true;tmp.push((i+1, j));nokori -= 1; tmpsize += 1;
            }
            if j >= 1 && !check[i][j-1] {
                check[i][j-1] = true;tmp.push((i, j-1));nokori -= 1; tmpsize += 1;
            }
            if j+1 <= w-1 && !check[i][j+1] {
                check[i][j+1] = true;tmp.push((i, j+1));nokori -= 1; tmpsize += 1;
            }
        }
        if tmpsize > 0 {
            black.push(tmp);
            cnt += 1;
        }
        //println!("----");
        //println!("check {:?}", check);
        //println!("black {:?}", black);
        //println!("nokori {}", nokori);
        //println!("cnt {}", cnt);
    }
    println!("{}", cnt);
}

Measured memory_bytes: 26868