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 JavaScript (node.js v5.12) (judge verdict: Accepted)

module.exports=function(t){var e={};function i(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)i.d(n,r,function(e){return t[e]}.bind(null,r));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){i(1)(i(3).readFileSync("/dev/stdin","UTF-8").split("\n"))},function(t,e,i){var n=i(2);t.exports=function(t){for(var e=t[0].split(" "),i=Number(e[0]),r=Number(e[1]),s=[],o=0,u=new n(1e7),d=0;d<i;d++){for(var f=t[d+1].split(""),l=0;l<r;l++)"#"==f[l]?u.enq({x:l,y:d,dist:0}):o+=1;s.push(f)}for(;u.size>0;){var c=u.deq();if("."==s[c.y][c.x]||0==c.dist){if("."==s[c.y][c.x]&&(s[c.y][c.x]="#",o-=1),o<=0)return void console.log(c.dist);c.x+1<r&&"#"!=s[c.y][c.x+1]&&u.enq({x:c.x+1,y:c.y,dist:c.dist+1}),c.x-1>=0&&"#"!=s[c.y][c.x-1]&&u.enq({x:c.x-1,y:c.y,dist:c.dist+1}),c.y+1<i&&"#"!=s[c.y+1][c.x]&&u.enq({x:c.x,y:c.y+1,dist:c.dist+1}),c.y-1>=0&&"#"!=s[c.y-1][c.x]&&u.enq({x:c.x,y:c.y-1,dist:c.dist+1})}}}},function(t,e,i){"use strict";t.exports=class{constructor(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e4;this.size=0,this.items=[],this.ind=0,this.maxGap=t}enq(t){this.size+=1,this.items.push(t),this.items.size-this.ind>this.maxGap&&(this.items=this.items.slice(this.ind))}deq(){var t=this.items[this.ind];return this.size-=1,this.ind+=1,t}}},function(t,e){t.exports=require("fs")}]);