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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;


class Program {

	static long mod = 998244353;
	static void Main(string[] args) {

		char[] cs = new char[] { ' ' };
		//char[] cs = new char[] { ' ', ',' };

		//int val = int.Parse(Console.ReadLine());
		//int[] vals = parseAry(Console.ReadLine().Split(cs));
		//string str = Console.ReadLine();
		//string[] strs = Console.ReadLine().Split(cs);

		//string res="";
		//int res = 0; ;
		//int t = int.Parse(Console.ReadLine());
		//string str = Console.ReadLine();
		//int[] vals = parseAry(Console.ReadLine().Split(cs));
		//int n = int.Parse(Console.ReadLine());
		//string res="";

		long res = 0;
		//int n = int.Parse(Console.ReadLine());

		int[] vals = parseAry(Console.ReadLine().Split(cs));
		int x = vals[0];
		int y = vals[1];

		List<int[]> now = new List<int[]>();
		List<int[]> next = new List<int[]>();

		string strs = "";
		int[,] map = new int[y, x];
		for ( int i = 0; i < y; i++ ) {
			strs = Console.ReadLine();
			for ( int k = 0; k < strs.Length; k++ ) {
				if ( strs[k] == '#' ) { map[i, k] = '#'; now.Add(new int[] { i, k }); } else { map[i, k] = '.'; }
			}
		}

		int[,] nextmap =
			(int[,])map.Clone(); ;

		int[] DX = new int[4] { -1, 1, 0, 0 };
		int[] DY = new int[4] { 0, 0, -1, 1 };
		while (true ) {
			for ( int i = 0; i < now.Count; i++ ) {
				for ( int d = 0; d < DX.Length; d++ ) {
					int xx = now[i][1] + DX[d];
					int yy = now[i][0] + DY[d];
					if ( 0 <= yy && yy < y && 0 <= xx && xx < x ) {
						if ( map[yy, xx] == '.' ) {
							map[yy, xx] = '#';
							next.Add(new int[2] { yy, xx });
						}
					}
				}
			}
			res++;
			if ( next.Count == 0 ) { break; }
			now=new List<int[]>();
			for ( int i = 0; i < next.Count; i++ ) {
				now.Add((int[])next[i].Clone());
			}
			next = new List<int[]>();
		}


		res--;

		Console.WriteLine(res);


		return;

	}

	//	文字列strをindex番目からdで上書き
	static string strOverWrite(string str, int i, string s) {
		return str.Remove(i, s.Length).Insert(i, s);
	}

	//　String[]  ----->  int[]
	static int[] parseAry(string[] str) {
		int[] nums = new int[str.Length];
		for ( int i = 0; i < str.Length; i++ ) { nums[i] = int.Parse(str[i]); }
		return nums;
	}
	//　String[]  ----->  long[]
	static long[] parseAryL(string[] str) {
		long[] nums = new long[str.Length];
		for ( int i = 0; i < str.Length; i++ ) { nums[i] = long.Parse(str[i]); }
		return nums;
	}

	//文字列を1文字ずつString[]に入れる
	static string[] strAry(String str) {
		string[] re = new string[str.Length];
		for ( int i = 0; i < str.Length; i++ ) { re[i] = str[i].ToString(); }
		return re;
	}

}