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):
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'
    
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase

read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
input=lambda: sys.stdin.readline().rstrip()

class Tree:
    def __init__(self,inp_size=None,ls=None,init=True,index=1):
        self.LCA_init_stat=False
        self.ETtable=[]
        if init:
            if ls==None:
                self.stdin(inp_size,index=index)
            else:
                self.node_size=len(ls)+1
                self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index)
        return
 
    def stdin(self,inp_size=None,index=1):
        if inp_size==None:
            self.node_size=int(input())
        else:
            self.node_size=inp_size
        self.edges,_=GI(self.node_size,self.node_size-1,index=index)
        return
    
    def listin(self,ls,index=0):
        self.node_size=len(ls)+1
        self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index)
        return
 
    def dfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v
 
    def bfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.popleft()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v
 
    def parent(self,x):
        return self.dfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1)
 
    def topological_sort(self,x):  
        tps=[]
        q=deque([x])
        v=[None]*self.node_size
        v[x]=0
        while q:
            c=q.popleft()
            tps.append(c)
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=0
        return tps
 
    def EulerTour(self,x):
        q=deque()
        q.append(x)
        self.depth=[None]*self.node_size
        self.depth[x]=0
        self.ETtable=[]
        self.ETdepth=[]
        self.ETin=[-1]*self.node_size
        self.ETout=[-1]*self.node_size
        cnt=0
        while q:
            c=q.pop()
            if c<0:
                ce=~c
            else:
                ce=c
                for nb,d in self.edges[ce]:
                    if self.depth[nb]==None:
                        q.append(~ce)
                        q.append(nb)
                        self.depth[nb]=self.depth[ce]+1
            self.ETtable.append(ce)
            self.ETdepth.append(self.depth[ce])
            if self.ETin[ce]==-1:
                self.ETin[ce]=cnt
            else:
                self.ETout[ce]=cnt
            cnt+=1
        return
    
    def LCA_init(self,root):
        self.EulerTour(root)
        self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
        
        self.LCA_init_stat=True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            self.LCA_init(root)
        xin,xout=self.ETin[x],self.ETout[x]
        yin,yout=self.ETin[y],self.ETout[y]
        a=min(xin,yin)
        b=max(xout,yout,xin,yin)
        id_of_min_dep_in_et=self.st.query_id(a,b+1)
        return self.ETtable[id_of_min_dep_in_et]
 
    def __str__(self):
        return  str(self.edges)
 
    def show(self):
        if all([all([d==1 for nd,d in e]) for e in self.edges]):
            print( [[nd for nd,d in e] for e in self.edges] )
        else:
            print(self)

class SparseTable: 
    def __init__(self,ls,init_func=min,init_idl=float('inf')):
        self.func=init_func
        self.idl=init_idl
        self.size=len(ls)
        self.N0=self.size.bit_length()
        self.table=[ls[:]]
        self.index=[list(range(self.size))]
        self.lg=[0]*(self.size+1)
        
        for i in range(2,self.size+1):
            self.lg[i]=self.lg[i>>1]+1
 
        for i in range(self.N0):
            tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
            tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
            self.table+=[tmp]
            self.index+=[tmp_id]
    
    
    def query(self,l,r):
        if r>self.size:r=self.size
        
        N=self.lg[r-l]
        return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
    
    
    def query_id(self,l,r):
        if r>self.size:r=self.size
        
        N=self.lg[r-l]
        a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
        if self.table[0][a]==self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))]):
            b=a
        return b
    
    
    def right_bound(self,pos,x):
        k=(self.size-pos).bit_length()
        for j in range(k)[::-1]:
            nx=pos+(1<<j)
            if nx<n and self.query(pos,nx+1)<=x:
                pos+=(1<<j)
        return pos
    
    
    def left_bound(self,pos,x):
        k=pos.bit_length()
        for j in range(k)[::-1]:
            nx=pos-(1<<j)
            if 0<=nx and self.query(nx,pos+1)<=x:
                pos-=(1<<j)
        return pos
 
    def __str__(self):
        return str(self.table[0])
 
    def print(self):
        for i in self.table:
            print(*i)


show_flg=False
show_flg=True
ans=0


class Graph:
    def __init__(self,inp_node_size=None,inp_edge_size=None,ls=None,init=True,Directed=False,index=1):
        if init:
            self.IsGrid=False
            if ls==None:
                self.stdin(inp_node_size,inp_edge_size,Directed=Directed,index=index)
            else:
                self.listin(ls,Directed=Directed,index=index)
        else:
            self.edges=[]
        return

    def stdin(self,inp_node_size=None,inp_edge_size=None,Directed=False,index=1):
        if (inp_node_size,inp_edge_size)==(None,None):
            self.node_size,self.edge_size=LI()
            self.edges,_=GI(self.node_size,self.edge_size,Directed=Directed,index=index)
        else:
            self.node_size,self.edge_size=inp_node_size,inp_edge_size
            self.edges,_=GI(self.node_size,self.edge_size,Directed=Directed,index=index)
        return

    def listin(self,inp_node_size,ls,Directed=False,index=0):
        self.node_size,self.edge_size=inp_node_size,len(ls)
        self.edges,_=GI(self.node_size,self.edge_size,ls,Directed=Directed,index=index)
        return

    def edgein(self,inp_edge,index=0):
        self.node_size,self.edge_size=len(inp_edge),0
        self.edges=[0]*self.node_size
        for i in range(self.node_size):
            self.edges[i]=inp_edge[i][:]
        return

    def gridin(self,h,w,search=None,replacement_of_found='.',mp_def={'
        
        self.mp,found=[],{}
        if search==None:
            search=[]
        self.node_size,self.edge_size,self.grid_h,self.grid_w=h*w,0,h,w
        self.edges=[[] for i in range(self.node_size)]
        self.IsGrid=True
        for i in range(h):
            s=input()
            for char in search:
                if char in s:
                    found[char]=(i*w+s.index(char))
                    mp_def[char]=mp_def[replacement_of_found]
            for j in s:
                self.mp.append(mp_def[j])
        for dd in [1,w]:
            r_end=0
            for node in range(self.node_size):
                r_end+=1
                if r_end==w:
                    r_end=0
                nx=node+dd
                if nx<0 or self.node_size<=nx or (dd==1 and r_end==0):
                    continue
                if self.mp[nx]==0:
                    self.edges[node].append(nx)
                if self.mp[node]==0:
                    self.edges[nx].append(node)
        return found

    def dfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v

    def bfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        q=deque([i for i,j in enumerate(self.mp) if j==1])
        v=[None]*self.node_size
        
        for y in q:v[y]=root_v
        while q:
            c=q.popleft()
            for nb in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=v[c]+1
        return v

    def parent(self,x):
        return self.bfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1)

    def topological_sort(self,x):  
        tps=[]
        q=deque([x])
        v=[None]*self.node_size
        v[x]=0
        while q:
            c=q.popleft()
            tps.append(c)
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=0
        return tps

    def dijkstra(self,st):
        
        
        d=[(0 if st==i else inf) for i in range(self.node_size)]
        prev=[0]*self.node_size
        q=[(j,i) for i,j in enumerate(d)]
        heapify(q)
        v=[False]*self.node_size
        
        while q:
            dist,cur=heappop(q)
            if v[cur]:
                continue
            v[cur]=True
            for dst,dist in self.edges[cur]:
                alt=d[cur]+dist
                if alt<d[dst]:
                    d[dst]=alt
                    prev[dst]=cur
                    heappush(q,(alt,dst))
        return d,prev
    
    def __str__(self):
        if self.IsGrid:
            for i in range(self.grid_h):
                print(*self.mp[i*self.grid_w:-~i*self.grid_w])
            return str(str(self.show()))
        else:
            return  str(str(self.show()))

    def show(self):  
        if self.IsGrid:
            rt=[[nd for nd,d in e] for e in self.edges]
            return 'Grid',rt
        else:
            if all([all([d==1 for nd,d in e]) for e in self.edges]):
                rt=[[nd for nd,d in e] for e in self.edges]
                return 'Simplified',rt
            else:
                return self


gr=Graph(init=False)
h,w=LI()

f=gr.gridin(h,w)
v=gr.bfs(0)

print(max(v))

Solution (Python):
from collections import deque

h, w = map(int, input().split())
tizu = [list(input()) for _ in range(h)]
check = [[False for _ in range(w)] for _ in range(h)]
queue = deque()
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for i in range(h):
    for j in range(w):
        if tizu[i][j] == "
            queue.append([i, j])
            queue.append(0)
            check[i][j] = True
            
while queue:
    pos = queue.popleft()
    ans = queue.popleft() + 1
    for dy, dx in directions:
        next_y = pos[0] + dy
        next_x = pos[1] + dx
        if next_x == -1 or next_y == -1 or next_x == w or next_y == h:
            continue
        if check[next_y][next_x]:
            continue
        check[next_y][next_x] = True
        queue.append([next_y, next_x])
        queue.append(ans)
        
print(ans - 1)

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

def solve():
    H, W = map(int, input().split())
    M = [[] for _ in range(H)]
    queue = deque()
    for i in range(H):
        M[i] = list(input())
        for j in range(W):
            if M[i][j] == '
                queue.append((i, j))
    ans = 0
    while queue:
        
        n = len(queue)
        flag = False
        for _ in range(n):
            cy, cx = queue.popleft()
            for dy, dx in ((-1, 0), (0, 1), (1, 0), (0, -1)):
                ny, nx = cy + dy, cx + dx
                if 0 <= nx < W and 0 <= ny < H and M[ny][nx] == '.':
                    M[ny][nx] = '
                    flag = True
                    queue.append((ny, nx))
        if flag:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque
h, w = map(int, input().split())
n = h*w


def to_v(i, j):
    return i*w+j


init_vs = []
for i in range(h):
    for j, x in enumerate(input()):
        if x == '
            init_vs.append(to_v(i, j))


INF = 10**18
dist = [INF]*n
for v in init_vs:
    dist[v] = 0


def generate_v2(v):
    i, j = divmod(v, w)
    if 0 < i:
        yield v-w
    if i < h-1:
        yield v+w
    if 0 < j:
        yield v-1
    if j < w-1:
        yield v+1


def bfs(init_vs):
    next_v = deque([*init_vs])
    while next_v:
        v = next_v.popleft()
        for v2 in generate_v2(v):
            if dist[v2] <= dist[v]+1:
                continue
            dist[v2] = dist[v]+1
            next_v.append(v2)


bfs(init_vs)
print(max(dist))

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

def bfs():
    H, W = map(int, input().split())
    ls = deque([])
    D = [[True] * (W+2)]
    D += ([[True] + [False]*W + [True] for i in range(H)])
    D.append([True]*(W+2))
    for i in range(H):
        line = input()
        for j in range(W):
            if line[j] == "
                ls.append([i+1, j+1, 0])
                D[i+1][j+1] = True

    while len(ls) > 0:
        h, w, depth = ls.popleft()
        if not D[h+1][w]:
            ls.append([h+1, w, depth+1])
            D[h+1][w] = True
        if not D[h-1][w]:
            ls.append([h-1, w, depth+1])
            D[h-1][w] = True
        if not D[h][w+1]:
            ls.append([h, w+1, depth+1])
            D[h][w+1] = True
        if not D[h][w-1]:
            ls.append([h, w-1, depth+1])
            D[h][w-1] = True

    return depth
    
print(bfs())

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

def bfs():
    H, W = map(int, input().split())
    ls = deque([])
    D = [[0] * (W+2)]
    D += ([[0] + [float("inf")]*W + [0] for i in range(H)])
    D.append([0]*(W+2))
    G = [["
    for i in range(H):
        line = [i for i in input().strip()]
        for j in range(W):
            if line[j] == "
                ls.append([i+1, j+1, 0])
                D[i+1][j+1] = 0
        G.append(["
    G.append([["

    while len(ls) > 0:
        h, w, depth = ls.popleft()
        if depth + 1 < D[h+1][w] and G[h+1][w] != "
            ls.append([h+1, w, depth+1])
            D[h+1][w] = depth + 1
        if depth + 1 < D[h-1][w] and G[h-1][w] != "
            ls.append([h-1, w, depth+1])
            D[h-1][w] = depth + 1
        if depth + 1 < D[h][w+1] and G[h][w+1] != "
            ls.append([h, w+1, depth+1])
            D[h][w+1] = depth + 1
        if depth + 1 < D[h][w-1] and G[h][w-1] != "
            ls.append([h, w-1, depth+1])
            D[h][w-1] = depth + 1

    ans = 0
    for h in range(H+2):
        ans = max(ans, max(D[h]))
        
    return ans
    
print(bfs())

Solution (Python):
from collections import deque

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

flag = [[True] * w for _ in range(h)]

q = deque()

for i in range(h):
    for j in range(w):
        if m[i][j] == "
            q.appendleft((i, j, 0))
            flag[i][j] = False

ans = 0
while q:
    (i, j, n) = q.pop()
    ans = n
    for dx, dy in zip([1,0, -1, 0], [0, 1, 0, -1]):
        ni = i + dx
        nj = j + dy
        if (0 <= ni < h and 0 <= nj < w and flag[ni][nj]):
            flag[ni][nj] = False
            q.appendleft((ni, nj, n + 1))

print(ans)

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit

@njit("i8(i8[:,:],i8,i8)",cache=True)
def search(grid,h,w):
    q=np.zeros(2*10**6+4,dtype=np.int64)
    now=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]==1:
                q[now]=i
                q[now+1]=j
                now+=2

    now,nex=0,now
    cnt=0
    while now!=nex:
        tmp=nex
        for i in range(now,nex,2):
            y=q[i]
            x=q[i+1]
            for dy,dx in (-1,0),(1,0),(0,-1),(0,1):
                ny=y+dy
                nx=x+dx
                if 0<=ny<h and 0<=nx<w:
                    if grid[ny][nx]==0:
                        grid[ny][nx]=1
                        q[tmp]=ny
                        q[tmp+1]=nx
                        tmp+=2

        now,nex=nex,tmp
        cnt+=1

    return cnt-1

def main():
    
    readline=stdin.readline
    h,w=map(int,readline().split())
    grid=[readline().strip() for _ in range(h)]
    grid=np.array([[1 if grid[i][j]=="
    print(search(grid,h,w))

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin
import numpy as np
from numba import njit

@njit("i8(i8[:,:],i8,i8)")
def search(grid,h,w):
    q=np.zeros(2*10**6+4,dtype=np.int64)
    now=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]==1:
                q[now]=i
                q[now+1]=j
                now+=2

    now,nex=0,now
    cnt=0
    while now!=nex:
        tmp=nex
        for i in range(now,nex,2):
            y=q[i]
            x=q[i+1]
            for dy,dx in (-1,0),(1,0),(0,-1),(0,1):
                ny=y+dy
                nx=x+dx
                if 0<=ny<h and 0<=nx<w:
                    if grid[ny][nx]==0:
                        grid[ny][nx]=1
                        q[tmp]=ny
                        q[tmp+1]=nx
                        tmp+=2

        now,nex=nex,tmp
        cnt+=1

    return cnt-1

def main():
    
    readline=stdin.readline
    h,w=map(int,readline().split())
    grid=[readline().strip() for _ in range(h)]
    grid=np.array([[1 if grid[i][j]=="
    print(search(grid,h,w))

if __name__=="__main__":
    main()

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


def BFS():
    point=0
    color=[["white" for _ in range(w)] for _ in range(h)]
    D=[[0 for _ in range(w)] for _ in range(h)]

    queue=deque([])
    for i in range(h):
        for j in range(w):
            if A[i][j]=="
                color[i][j]="gray"
                queue.append([i,j])
                D[i][j]=0

    while len(queue)>0:
        x,y=queue.popleft()
        for a,b in [[1,0],[-1,0],[0,1],[0,-1]]:
            X,Y=x+a,y+b
            if 0<=X<h and 0<=Y<w:
                if color[X][Y]=="white":
                    color[X][Y]="gray"
                    D[X][Y]=D[x][y]+1
                    point=max(point,D[X][Y])
                    queue.append([X,Y])
        color[x][y]="black"

    return point


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

Solution (Python):
from collections import deque


def BFS():
    point=0
    color=[["white" for _ in range(w)] for _ in range(h)]
    D=[[0 for _ in range(w)] for _ in range(h)]

    queue=deque([])
    for i in range(h):
        for j in range(w):
            if A[i][j]=="
                color[i][j]="gray"
                queue.append([i,j])
                D[i][j]=0

    while len(queue)>0:
        x,y=queue.popleft()
        for a,b in [[1,0],[-1,0],[0,1],[0,-1]]:
            X,Y=x+a,y+b
            if 0<=X<h and 0<=Y<w:
                if color[X][Y]=="white":
                    color[X][Y]="gray"
                    D[X][Y]=D[x][y]+1
                    point=max(point,D[X][Y])
                    queue.append([X,Y])
        color[x][y]="black"

    return point


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

Solution (Python):
from collections import deque
inf=10**9


def BFS():
    point=0
    color=[["white" for _ in range(w)] for _ in range(h)]
    D=[[inf for _ in range(w)] for _ in range(h)]

    queue=deque([])
    for i in range(h):
        for j in range(w):
            if A[i][j]=="
                queue.append([i,j])
                D[i][j]=0

    while len(queue)>0:
        x,y=queue.popleft()
        for a,b in [[1,0],[-1,0],[0,1],[0,-1]]:
            X,Y=x+a,y+b
            if 0<=X<h and 0<=Y<w:
                if color[X][Y]!="black" and D[X][Y]>D[x][y]+1:
                    color[X][Y]="gray"
                    D[X][Y]=D[x][y]+1
                    point=max(point,D[X][Y])
                    queue.append([X,Y])
        color[x][y]="black"

    return point


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

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

H ,W = map(int, input().split())
List = [list(str(input())) for i in range(H)]



cheak=[['0' for i in range(W)] for j in range(H)]

q = deque([])

for h in range(H):
    for w in range(W):
        if List[h][w]=='
            cheak[h][w]='1'
            q.append([h,w])


move=[[1,0],[-1,0],[0,1],[0,-1]]
num=-1

while q:
    for rt in range(len(q)):
        i=q.popleft()
        for u in move:
            if 0<=i[0]+u[0]<=H-1 and 0<=i[1]+u[1]<=W-1 and cheak[i[0]+u[0]][i[1]+u[1]]=='0':
                q.append([i[0]+u[0],i[1]+u[1]])
                cheak[i[0]+u[0]][i[1]+u[1]]='1'
    
    num+=1
print(num)

Solution (Python):
import copy
from collections import deque

h, w = map(int, input().split())
grid = [list(map(lambda x: x == "

q = deque([])
for i in range(h):
    for j in range(w):
        if grid[i][j]:
            q.append([i, j, 0])

moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]

ans = 0
while q:
    curr = q.popleft()
    ans = max(ans, curr[2])
    for m in moves:
        nx, ny = curr[0] + m[0], curr[1] + m[1]
        if nx < 0 or ny < 0 or nx > h - 1 or ny > w - 1 or grid[nx][ny]:
            continue
        else:
            q.append([nx, ny, curr[2] + 1])
            grid[nx][ny] = True

print(ans)

Solution (Python):
from collections import deque
def solve():
    max_cost = 0
    stack = deque()
    used = [[False for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == '.':
                continue
            stack.append([(i, j), 0])
            used[i][j] = True
    while stack:
        (i, j), cost = stack.popleft()
        max_cost = max(max_cost, cost)
        for x, y in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
            if not 0 <= x < H or not 0 <= y < W or used[x][y]:
                continue
            stack.append([(x, y), cost + 1])
            used[x][y] = True
    print(max_cost)

if __name__ == "__main__":
    H, W = map(int, input().split())
    A = []
    for _ in range(H):
        A.append(list(input()))
    solve()

Solution (Python):
from collections import deque

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

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

q = deque()

for i in range(h):
    for j in range(w):
        if(a[i][j] == "
            b[i][j] = 0
            q.append(i)
            q.append(j)
kotae = 0
while(len(q)):
    x = int(q.popleft())
    y = int(q.popleft())
    dx = [0, 0, -1, 1]
    dy = [-1, 1, 0, 0]

    for i in range(4):
        if(0 <= x + dx[i] and x + dx[i] < h and 0 <= y + dy[i] and y + dy[i] < w):
            if(b[x+dx[i]][y+dy[i]] == -1):
                q.append(x + dx[i])
                q.append(y + dy[i])
                b[x+dx[i]][y+dy[i]] = b[x][y] + 1
                kotae = max(kotae, b[x+dx[i]][y+dy[i]])
print(kotae)

Solution (Python):
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '
print(depth)

Solution (Python):
from collections import deque
H, W = map(int, input().split())

area = [input() for _ in range(H)]

distance = [ [float('inf') for _ in range(W)] for _ in range(H)]
to_search = deque()

for i in range(H):
    for j in range(W):
        if area[i][j] == "
            to_search.appendleft((i, j, 0))
            distance[i][j] = 0

didj = [[1, 0], [0, 1], [-1, 0], [0, -1]]
ans = 0

while len(to_search) > 0:
    nowi, nowj, nowd = to_search.pop()
    for di, dj in didj:
        nexti = nowi + di
        nextj = nowj + dj
        if 0 <= nexti < H and 0 <= nextj < W:
            if distance[nexti][nextj] > nowd + 1:
                to_search.appendleft((nexti, nextj, nowd + 1))
                distance[nexti][nextj] = nowd + 1
                ans = max(ans, nowd + 1)

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')

from collections import deque




import copy

def grid_wall(grid, h, w, wall='
    G = [[wall]*(w+2) for _ in range(h+2)]
    for hi in range(h):
        G[hi+1][1:w+1] = grid[hi]
    return G

def solve():
    H, W = MI()
    g = [[] for _ in range(H)]
    start = []
    white_cnt = 0
    for i in range(H):
        line = LS()
        for j, l in enumerate(line):
            if l == '.': white_cnt = white_cnt + 1
            else: start.append((i+1, j+1))
        g[i] = line

    if white_cnt == 0:
        print(0)
        return 

    G = grid_wall(g, H, W, 'x')
    move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    ans = 0
    while 1:
        next = []
        for x, y in start:
            for dx, dy in move:
                nx, ny = x+dx, y+dy
                if G[nx][ny] == 'x': continue
                elif G[nx][ny] == '
                elif G[nx][ny] == '.':
                    white_cnt -= 1
                    G[nx][ny] = '
                    next.append((nx, ny))
        ans += 1
        
        if white_cnt <= 0:
            print(ans)
            return
        start = next[:]



if __name__ == '__main__':
    solve()

Solution (Python):
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Solution (Python):
h, w = map(int, input().split())
a = [list(input()) for i in range(h)]
que = []
v = [[True] * (w + 2)]
v += [[True] + [j == "
v.append([True] * (w + 2))
m = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if v[i][j]:
            que.append((i, j))
ans = 0
while que:
    p = []
    ans += 1
    for x, y in que:
        if not v[x - 1][y]:
            v[x - 1][y] = True
            p.append((x - 1, y))
        if not v[x + 1][y]:
            v[x + 1][y] = True
            p.append((x + 1, y))
        if not v[x][y - 1]:
            v[x][y - 1] = True
            p.append((x, y - 1))
        if not v[x][y + 1]:
            v[x][y + 1] = True
            p.append((x, y + 1))
    que = p
print(ans - 1)