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 (Python):

H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]


blacks = []
for i in range(H):
    for j in range(W):
        if maze[i][j] == '
            blacks.append((i, j))


count = 0
while len(blacks) != 0:
    count += 1

    nexts = []
    for x, y in blacks:
        for nx, ny in [(x-1, y), (x+1,y), (x,y-1),(x,y+1)]:
            if 0<=nx<H and 0<=ny<W and maze[nx][ny] == '.':
                maze[nx][ny] = '
                nexts.append((nx, ny))

    blacks = nexts
    
print(count-1)

Solution (Python):
import numpy as np

H, W = map(int,input().split(' '))


max_value = H+W
values =[]
 

for i in range(H):
    values.append([0 if i == '
    


values = np.array(values)
 










for x in range(W-1):
    values[:,x+1] = np.minimum(values[:,x]+1,values[:,x+1])






for x in range(W-1,0,-1):
    values[:,x-1] = np.minimum(values[:,x-1],values[:,x]+1)






for x in range(H-1):
    values[x+1,:] = np.minimum(values[x,:]+1,values[x+1,:])






for x in range(H-1,0,-1):
    values[x-1,:] = np.minimum(values[x,:]+1,values[x-1,:])


    

print(np.max(values))

Solution (Python):
from collections import deque
H, W = [int(n) for n in input().split()]
map = [0 for _ in range(H)]
INF = 10**8
dis = [[INF for _ in range(W)]for __ in range(H)]
Q = deque()
for i in range(H):
    map[i] = list(str(input()))
    for j in range(W):
        if map[i][j] == "
            Q.append((i, j))
            dis[i][j] = 0
dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
ans = 0
while(len(Q) != 0):
    n = Q.pop()
    for d in dxy:
        tx, ty = n[0] + d[0], n[1] + d[1]
        if(0 <= tx <= H - 1 and 0 <= ty <= W - 1 and map[tx][ty] == "."):
            map[tx][ty] = "
            Q.appendleft((tx, ty))
            dis[tx][ty] = dis[n[0]][n[1]] + 1
            ans = max(dis[tx][ty], ans)
print(ans)

Solution (Python):

h,w = map(int, input().split())
al = [list(input()) for _ in range(h)]
cnt = 0

q = []
for hi in range(h):
    for wi in range(w):
        if al[hi][wi]=="
            q.append((hi,wi))

while len(q)>0:
    nq = []
    cnt += 1
    for hi,wi in q:
        for d in [[1,0],[0,1],[-1,0],[0,-1]]:
            if h>hi+d[0]>=0 and w>wi+d[1]>=0:
                if al[hi+d[0]][wi+d[1]]==".":
                    al[hi+d[0]][wi+d[1]] = "
                    nq.append((hi+d[0], wi+d[1]))
    q = nq

print(cnt-1)

Solution (Python):
h, w = map(int, input().split())

a = ''.join(input() + '
a = a.replace('.', '1').replace('

bits = int(a, 2)

w1 = w + 1
h1 = h + 1
bits <<= w1

frame = (1 << w1) - 1
frame |= frame << h1 * w1
frame |= sum(1 << b for b in range(w1, h1 * w1, w1))

count = 0
while bits:
    count += 1
    mask = bits | frame
    
    bits &= (mask << w1) & (mask >> w1) & (mask << 1) & (mask >> 1)
print(count)

Solution (Python):
import sys
input = sys.stdin.readline
 
H, W = map(int, input().split())
mark_matrix = [[True]*(W+2)]
mark_matrix.extend([[True]+[x!='.' for x in input()] for _ in range(H)])
mark_matrix[H].append(True)
mark_matrix.append([True]*(W+2))

queue = []
for i in range(1,H+1):
  for j in range(1,W+1):
    if mark_matrix[i][j]:
      queue.append((i,j))
 
max_value = 0
while(True):
  if len(queue) == 0:
    break
  
  new_queue = []
  for i,j in queue:
  
    if not mark_matrix[i-1][j]:
      mark_matrix[i-1][j] = True
      new_queue.append((i-1,j))
    if not mark_matrix[i+1][j]:
      mark_matrix[i+1][j] = True
      new_queue.append((i+1,j))
    if not mark_matrix[i][j-1]:
      mark_matrix[i][j-1] = True
      new_queue.append((i,j-1))
    if not mark_matrix[i][j+1]:
      mark_matrix[i][j+1] = True
      new_queue.append((i,j+1))
 
  if len(new_queue) == 0:
    break
  else:
    queue = new_queue
    max_value += 1
  
print(max_value)

Solution (Python):
def f(h, w):
    t = []
    for _ in range(h):
        t += [[0 if c == "
    for ti in t:
        for j in range(1, w):
            if ti[j - 1] + 1 < ti[j]:
                ti[j] = ti[j - 1] + 1
        for j in range(w - 2, -1, -1):
            if ti[j + 1] + 1 < ti[j]:
                ti[j] = ti[j + 1] + 1
    for j in range(w):
        for i in range(1, h):
            if t[i - 1][j] + 1 < t[i][j]:
                t[i][j] = t[i - 1][j] + 1
        for i in range(h - 2, -1, -1):
            if t[i + 1][j] + 1 < t[i][j]:
                t[i][j] = t[i + 1][j] + 1

    print(max(max(ti) for ti in t))


h, w = map(int, input().split())
f(h, w)

Solution (Python):
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            ti=t[i]
            if not ti[j+1]:
                ti[j + 1]=True
                nxt += [(i, j+1)]
            if not ti[j-1]:
                ti[j - 1]=True
                nxt += [(i, j-1)]
            if not t[i - 1][j]:
                t[i - 1][j]=True
                nxt += [(i - 1, j)]
            if not t[i + 1][j]:
                t[i + 1][j]=True
                nxt += [(i + 1, j)]
        kuro = nxt
    print(cnt)


h, w = map(int, input().split())
f(h, w)

Solution (Python):
from collections import deque

def main():
    tosearch = deque()

    H, W = map(int, input().split())

    grid = [[-1 for _ in range(W + 2)]]
    for i in range(H):
        grid.append([-1] + [0 if char == "
    grid.append([-1 for _ in range(W + 2)])

    for i, line in enumerate(grid):
        for j, char in enumerate(line):
            if char == 0:
                tosearch.append((i, j))

    movement = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    while tosearch:
        y, x = tosearch.popleft()

        for move in movement:
            ny, nx = y + move[0], x + move[1]

            if grid[ny][nx] == -2:
                grid[ny][nx] = grid[y][x] + 1
                tosearch.append((ny, nx))

    ans = 0
    for line in grid:
        ans = max(ans, max(line))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import collections

h,w=list(map(int,input().split()))
a=[input() for _ in range(h)]

direction=[[-1,0],[1,0],[0,-1],[0,1]]
visited=[[-1]*w for _ in range(h)]
q=collections.deque()


for hi in range(h):
  for wi in range(w):
    if a[hi][wi]=="
      q.append([hi,wi])
      visited[hi][wi]=0

while len(q)>0:
  ch,cw=q.popleft()
  for d in direction:
    nh=ch+d[0]
    nw=cw+d[1]
    
    if nh>=0 and nw>=0 and nh<h and nw<w and visited[nh][nw]==-1:
      q.append([nh,nw])
      visited[nh][nw]=visited[ch][cw]+1

ans=0
for hi in range(h):
  tmp=max(visited[hi])
  if tmp > ans:
    ans=tmp
print(ans)

Solution (Python):
import sys
import numpy as np
 

 
input = sys.stdin.readline
 
H, W = map(int, input().split())
 
status = np.full((H, W), H*W, dtype=int)
for i in range(H):
    for j, c in enumerate(input()):
        if c == "
            status[i,j] = 0
 
 
for h in range(1, H):
    status[h, :] = np.minimum(status[h, :], status[h-1, :] + 1)
for h in range(0, H-1)[::-1]:
    status[h, :] = np.minimum(status[h, :], status[h+1, :] + 1)
for w in range(1, W):
    status[:, w] = np.minimum(status[:, w], status[:, w-1] + 1)
for w in range(0, W-1)[::-1]:
    status[:, w] = np.minimum(status[:, w], status[:, w+1] + 1)
 
print(np.max(status))

Solution (Python):
from collections import deque
 
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]
 
que = deque()
INF = float('inf')
dx = [1, 0, -1, 0]
dy = [0, 1, 0 , -1]            
D = [[INF] * W for i in range(H)]

for i in range(H):
    for j in range(W):
        if A[i][j] == '
            que.append([i, j])
            D[i][j] = 0
                       
def bfs():
    while len(que) != 0:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < H and 0 <= ny < W and A[nx][ny] != '
                que.append([nx, ny])
                D[nx][ny] = D[p[0]][p[1]] + 1
    
bfs()

ans = 0
for i in range(H):
    for j in range(W):
        tmp = D[i][j]
        ans = max(ans, tmp)
        
print(ans)

Solution (Python):
import sys
import numpy as np



input = sys.stdin.readline

H, W = map(int, input().split())

status = np.full((H, W), H*W, dtype=int)
for i in range(H):
    for j, c in enumerate(input()):
        if c == "
            status[i,j] = 0


for h in range(1, H):
    status[h, :] = np.minimum(status[h, :], status[h-1, :] + 1)
for h in range(0, H-1)[::-1]:
    status[h, :] = np.minimum(status[h, :], status[h+1, :] + 1)
for w in range(1, W):
    status[:, w] = np.minimum(status[:, w], status[:, w-1] + 1)
for w in range(0, W-1)[::-1]:
    status[:, w] = np.minimum(status[:, w], status[:, w+1] + 1)

print(np.max(status))

Solution (Python):

"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""


"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""


"""
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
"""


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())


dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')


ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)


(x >> i) & 1


dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

add = 1 
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)


li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    


org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)



"""
H, W = list(map(int, input().split()))

A = [""]*H 

INF = 2000


D = [[0]*W for h in range(H)]
for h in range(H):
    A[h] = input()
    for w in range(W):
        if A[h][w] == '.':
            D[h][w] = INF

def main():
    d_max = 0 
    for h in range(1,H):
        for w in range(1,W):
            D[h][w] = min(D[h][w], D[h-1][w]+1, D[h][w-1]+1, D[h-1][w-1]+2)
    
    
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)
    
    
    print(int(d_max))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
board=[[True]*(W+2)]
for i in range(H):
    board.append([True]+[a=='
board.append([True]*(W+2))

q=[]
for h in range(1,H+1):
    for w in range(1,W+1):
        if board[h][w]:
            q.append((h,w))

cnt=0
while q:
    nq=[]
    for h,w in q:
        if not board[h+1][w]:
            board[h+1][w]=True
            nq.append((h+1,w))
        if not board[h-1][w]:
            board[h-1][w]=True
            nq.append((h-1,w))
        if not board[h][w+1]:
            board[h][w+1]=True
            nq.append((h,w+1))
        if not board[h][w-1]:
            board[h][w-1]=True
            nq.append((h,w-1))

    q=nq
    cnt+=1

print(cnt-1)

Solution (Python):
from collections import deque

h,w = map(int, input().split())

A = [['*']*(w+2)] + [['*']+[s for s in input()]+['*'] for _ in range(h)] + [['*']*(w+2)]
visited = [[0]*(w+2) for _ in range(h+2)]

dq = deque()
n=0
directions = [[0,1],[1,0],[0,-1],[-1,0]]

for hi in range(1,h+1):
    for wi in range(1,w+1):
        if A[hi][wi] == '
            dq.append([hi,wi,n])
            visited[hi][wi] = 1

while dq:
    hi,wi,n = dq.popleft()

    for d in directions:
        new_hi, new_wi = hi+d[0],wi + d[1]
        
        if visited[new_hi][new_wi] == 0 and A[new_hi][new_wi] == '.':
            dq.append([new_hi,new_wi,n+1])
            visited[new_hi][new_wi] = 1
print(n)

Solution (Python):
import collections 

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] 

dx4=[0, 1, 0,-1]
dy4=[1, 0,-1, 0]




memo = [[-1]*w for _ in range(h)]

q = collections.deque()


for i in range(h):
    for j in range(w):
        if board[i][j] == "
            q.append((i,j)) 
            memo[i][j] = 0 

while len(q) != 0:
    x,y = q.popleft() 
    
    for i in range(4):
        nx = x + dx4[i]
        ny = y + dy4[i]
        
        if nx < 0 or nx>=h or ny <0 or ny>=w: continue
        
        
        
        if memo[nx][ny] >= 0: continue
        
        memo[nx][ny] = memo[x][y]+1
        q.append((nx,ny))
        
        

ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,memo[i][j])

print(ans)

Solution (Python):
from collections import deque

h,w = map(int, input().split())
A = [[s for s in input()] for _ in range(h)]

visited = [[-1]*(w) for _ in range(h)]
dq = deque()
directions = [[0,1],[1,0],[0,-1],[-1,0]]


for hi in range(h):
    for wi in range(w):
        if A[hi][wi] == '
            dq.append([hi,wi])
            visited[hi][wi] = 0


while dq:
    hi,wi = dq.popleft()
    for d in directions:
        nhi, nwi = hi+d[0],wi + d[1]
        
        if nhi<0 or nhi>=h or nwi<0 or nwi>=w: continue 
        
        if visited[nhi][nwi] == -1 and A[nhi][nwi] == '.':
            dq.append([nhi,nwi])
            visited[nhi][nwi] = visited[hi][wi] + 1

ans = -1
for x in visited:
    ans = max(ans,max(x))
print(ans)

Solution (Python):
H, W = [int(_) for _ in input().split()]
A = [list(input()) for i in range(H)]


ps = []
pcount = 0
for y, row in enumerate(A):
    for x, c in enumerate(row):
        if c == "
            ps.append((x, y))
            pcount += 1


result = 0




while pcount < H * W:
    nps = []
    def pset(x, y):
        global pcount
        if 0 <= y < len(A) and 0 <= x < len(A[y]) and A[y][x] == ".":
            A[y][x] = "
            nps.append((x, y))
            pcount += 1
    for x, y in ps:
        pset(x - 1, y)
        pset(x + 1, y)
        pset(x, y - 1)
        pset(x, y + 1)
    ps = nps
    result += 1
    
    
    

print(result)

Solution (Python):



H, W = map(int, input().split())
q = []
a = [None] * H
for y in range(H):
    l = list(input())
    a[y] = l
    for x in range(W):
        if l[x] == '
            q.append((y,x))
ans = 0
while len(q) > 0:
    ans+=1
    tq = []
    for qq in q:
        if qq[0] > 0:
            if a[qq[0]-1][qq[1]] == '.':
                a[qq[0] - 1][qq[1]] = '
                tq.append((qq[0] - 1, qq[1]))
        if qq[0] < (H-1):
            if a[qq[0]+1][qq[1]] == '.':
                a[qq[0] + 1][qq[1]] = '
                tq.append((qq[0] + 1, qq[1]))
        if qq[1] > 0:
            if a[qq[0]][qq[1]-1] == '.':
                a[qq[0]][qq[1]-1] = '
                tq.append((qq[0], qq[1]-1))
        if qq[1] < (W-1):
            if a[qq[0]][qq[1]+1] == '.':
                a[qq[0]][ qq[1]+1] = '
                tq.append((qq[0], qq[1]+1))
    q = tq
print(ans-1)

Solution (Python):
H, W = map(int,input().split())
A = [input() for i in range(H)]
q1 = []
q2 = []
visit = [[False]*W for i in range(H)]
cnt = 0

for i in range(H):
    for j in range(W):
        if A[i][j] == "
            q1.append([i,j])
            visit[i][j] = True

while q1:
    while q1:
        index = q1.pop()
        for i in [-1,1]:
            for j in [-1,1]:
                if index[0]+i >= 0 and index[0]+i < H:
                    if not visit[index[0]+i][index[1]]:
                        visit[index[0]+i][index[1]] = True
                        q2.append([index[0]+i,index[1]])
                if index[1]+j >= 0 and index[1]+j < W:
                    if not visit[index[0]][index[1]+j]:
                        visit[index[0]][index[1]+j] = True
                        q2.append([index[0],index[1]+j])
    q1 = q2[:]
    q2 = []
    cnt += 1
    
print(cnt-1)

Solution (Python):
def main():
    from collections import deque
    H, W = map(int, input().split())
    grid = [["
                                for i in range(H)] + [["
    
    stack = deque()
    for h in range(1, H+1):
        for w in range(1, W+1):
            
            if grid[h][w] == "
                stack.append([h, w])

    for i in range(h + w + 3):
        tmp = deque()
        while stack:
            h, w = stack.pop()
            for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if grid[h + dh][w + dw] == ".":
                    grid[h + dh][w + dw] = "
                    tmp.append([h + dh, w + dw])
        stack = tmp
        if not stack:
            print(i)
            break


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = map(int, input().split())
    grid = [["
                                for i in range(H)] + [["
    
    stack = deque()
    for h in range(1, H+1):
        for w in range(1, W+1):
            
            if grid[h][w] == "
                stack.append([h, w])

    for i in range(h + w + 3):
        tmp = deque()
        while stack:
            h, w = stack.pop()
            for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if grid[h + dh][w + dw] == ".":
                    grid[h + dh][w + dw] = "
                    tmp.append([h + dh, w + dw])
        stack = tmp
        if not stack:
            print(i)
            break


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = map(int, input().split())
    grid = [["
                                for i in range(H)] + [["
    
    stack = deque()
    for h in range(1, H+1):
        for w in range(1, W+1):
            
            if grid[h][w] == "
                stack.append([h, w])

    for i in range(h + w + 3):
        tmp = deque()
        while stack:
            h, w = stack.pop()
            for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if grid[h + dh][w + dw] == ".":
                    grid[h + dh][w + dw] = "
                    tmp.append([h + dh, w + dw])
        stack = tmp
        if not stack:
            print(i)
            break


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = map(int, input().split())
    grid = [["
                                for i in range(H)] + [["
    
    stack = deque()
    for h in range(1, H+1):
        for w in range(1, W+1):
            
            if grid[h][w] == "
                stack.append([h, w])

    for i in range(h + w + 3):
        tmp = deque()
        while stack:
            h, w = stack.pop()
            for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if grid[h + dh][w + dw] == ".":
                    grid[h + dh][w + dw] = "
                    tmp.append([h + dh, w + dw])
        stack = tmp
        if not stack:
            print(i)
            break


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    H, W = map(int, input().split())
    grid = [["
                                for i in range(H)] + [["
    
    stack = deque()
    for h in range(1, H+1):
        for w in range(1, W+1):
            
            if grid[h][w] == "
                stack.append([h, w])

    for i in range(h + w + 3):
        tmp = deque()
        while stack:
            h, w = stack.pop()
            for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if grid[h + dh][w + dw] == ".":
                    grid[h + dh][w + dw] = "
                    tmp.append([h + dh, w + dw])
        stack = tmp
        if not stack:
            print(i)
            break


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
H, W = map(int, input().split())
grid = [["
                            for i in range(H)] + [["

stack = deque()
for h in range(1, H+1):
    for w in range(1, W+1):
        
        if grid[h][w] == "
            stack.append([h, w])

for i in range(h + w + 3):
    tmp = deque()
    while stack:
        h, w = stack.pop()
        for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
            if grid[h + dh][w + dw] == ".":
                grid[h + dh][w + dw] = "
                tmp.append([h + dh, w + dw])
    stack = tmp
    if not stack:
        print(i)
        break

Solution (Python):
from collections import deque
H, W = map(int, input().split())
grid = [["
                            for i in range(H)] + [["

stack = deque()
for h in range(1, H+1):
    for w in range(1, W+1):
        
        if grid[h][w] == "
            stack.append([h, w])

for i in range(h + w + 3):
    tmp = deque()
    while stack:
        h, w = stack.pop()
        for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
            if grid[h + dh][w + dw] == ".":
                grid[h + dh][w + dw] = "
                tmp.append([h + dh, w + dw])
    stack = tmp
    if not stack:
        print(i)
        break

Solution (Python):
from collections import deque
H, W = map(int, input().split())
grid = [["
                            for i in range(H)] + [["

stack = deque()
for h in range(1, H+1):
    for w in range(1, W+1):
        
        if grid[h][w] == "
            stack.append([h, w])

for i in range(h + w + 3):
    tmp = deque()
    while stack:
        h, w = stack.pop()
        for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
            if grid[h + dh][w + dw] == ".":
                grid[h + dh][w + dw] = "
                tmp.append([h + dh, w + dw])
    stack = tmp
    if not stack:
        print(i)
        break

Solution (Python):
from collections import deque
H, W = map(int, input().split())
grid = [["
                            for i in range(H)] + [["

stack = deque()
for h in range(1, H+1):
    for w in range(1, W+1):
        
        if grid[h][w] == "
            stack.append([h, w])

for i in range(h + w + 3):
    tmp = deque()
    while stack:
        h, w = stack.pop()
        for dh, dw in ([0, 1], [0, -1], [1, 0], [-1, 0]):
            if grid[h + dh][w + dw] == ".":
                grid[h + dh][w + dw] = "
                tmp.append([h + dh, w + dw])
    stack = tmp
    if not stack:
        print(i)
        break

Solution (Python):
import functools
@functools.lru_cache(maxsize=None)

def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
import functools
@functools.lru_cache(maxsize=None)

def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
import functools
@functools.lru_cache(maxsize=None)

def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys;input=sys.stdin.readline

h,w=map(int,input().split())
grid=[["

stack=deque()
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x]=="
            
            stack.append((y,x))

for i in range(h*w):
    tmp=deque()
    while stack:
        my,mx=stack.pop()
        for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
            if grid[my+y][mx+x]==".":
                
                grid[my+y][mx+x]="
                tmp.append([my+y,mx+x])
    stack=tmp
    if not tmp:
        print(i)
        break

Solution (Python):
from collections import deque
import sys;input=sys.stdin.readline

h,w=map(int,input().split())
grid=[["

stack=deque()
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x]=="
            
            stack.append((y,x))

for i in range(h*w):
    tmp=deque()
    while stack:
        my,mx=stack.pop()
        for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
            if grid[my+y][mx+x]==".":
                
                grid[my+y][mx+x]="
                tmp.append([my+y,mx+x])
    stack=tmp
    if not tmp:
        print(i)
        break

Solution (Python):
from collections import deque
import sys;input=sys.stdin.readline

h,w=map(int,input().split())
grid=[["

stack=deque()
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x]=="
            
            stack.append((y,x))

for i in range(h*w):
    tmp=deque()
    while stack:
        my,mx=stack.pop()
        for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
            if grid[my+y][mx+x]==".":
                
                grid[my+y][mx+x]="
                tmp.append([my+y,mx+x])
    stack=tmp
    if not tmp:
        print(i)
        break

Solution (Python):
def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    
                    grid[my+y][mx+x]="
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
    import sys;input=sys.stdin.readline

    h,w=map(int,input().split())
    grid=[["

    stack=deque()
    for y in range(1,h+1):
        for x in range(1,w+1):
            if grid[y][x]=="
                grid[y][x]=0
                stack.append((y,x))

    for i in range(h*w):
        tmp=deque()
        while stack:
            my,mx=stack.pop()
            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
                if grid[my+y][mx+x]==".":
                    grid[my+y][mx+x]=i+1
                    tmp.append([my+y,mx+x])
        stack=tmp
        if not tmp:
            print(i)
            break
if __name__ == '__main__':
    main()