Problem:
Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

// Try AtCoder
// author: Leonardone @ NEETSDKASU

fn main() {
	let mut stdin = String::new();
    std::io::Read::read_to_string(
    	&mut std::io::stdin(),
        &mut stdin).unwrap();
	let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
    	($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    
    let h = get!();
    let w = get!();
    let n = get!(usize);
    let sr = get!();
    let sc = get!();
    let s = get().as_bytes();
    let t = get().as_bytes();
    
    let mut ss = vec![(0, 0, 0, 0); n+1];
    let mut tt = vec![(0, 0, 0, 0); n+1];
    for i in 0..n {
    	let j = (n-1)-i;
        ss[i+1] = ss[i];
        match s[j] {
        b'L' => ss[i+1].0 += 1,
        b'R' => ss[i+1].1 += 1,
        b'U' => ss[i+1].2 += 1,
        _ => ss[i+1].3 += 1
        }
        tt[i+1] = tt[i];
        match t[j] {
        b'L' => tt[i+1].0 += 1,
        b'R' => tt[i+1].1 += 1,
        b'U' => tt[i+1].2 += 1,
        _ => tt[i+1].3 += 1
        }
    }
    
    use std::cmp::max;
    for i in 0..n {
    	if sc- ss[i].0 + max(0,tt[i].1 -1) <1
    		|| sc + ss[i].1 - max(0,tt[i].0 -1) > w
        	|| sr - ss[i].2 + max(0,tt[i].3 -1) < 1
        	|| sr + ss[i].3 - max(0,tt[i].2 -1) > h {
        	println!("NO");
        	return;
		}
    }
    
    if sc- ss[n].0 + max(0,tt[n].1 -1) <1
    	|| sc + ss[n].1 - max(0,tt[n].0 -1) > w
        || sr - ss[n].2 + max(0,tt[n].3 -1) < 1
        || sr + ss[n].3 - max(0,tt[n].2 -1) > h {
        println!("NO");
	} else {
    	let mut u = 0;
    	if sc - ss[n].0 < 1 { u |= 1; }
        if sc + ss[n].1 > w { u |= 2; }
        if sr - ss[n].2 < 1 { u |= 4; }
        if sr + ss[n].3 > h { u |= 8; }
        match u {
        0|1|2|4|8 => println!("YES"),
        _ => println!("NO"),
        }
    }
    
    
    
}