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



Sample input:
3 3
...
.#.
...


Sample output:
2

Submission status: Runtime Error

Solution code:
from collections import deque

def bfs():
    global ans
    que = deque([])
    black = 0
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
               black += 1
    if black == h*w:
        print(0)
        exit()
            
    white = True
    while white == True:
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for k in range(len(que)):
            i = 0
            while i < 4: 
                nx = que[k][0] + dx[i]
                ny = que[k][1] + dy[i]
                if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                    d[nx][ny] = d[que[k][0]][que[k][1]] + 1
                    A[nx][ny] = "#"
                i += 1
        que = deque([])
        black = 0
        for i in range(h):
            for j in range(w):
              if A[i][j] == "#":
                  que.append((i, j))
                  black += 1
                  ans = max(ans, d[i][j])
        if black == h * w:
            white = False
      
        


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
d = [[float("inf")] * w for _ in range(h)]
ans = 0
bfs()

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def bfs():
    global ans
    que = deque([])
    for i in range(h):
        for j in range(w):
           if A[i][j] == "#":
               que.append((i, j))
               d[i][j] = 0
               
    white = True
    while white == True:
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for k in range(len(que)):
            i = 0
            while i < 4: 
                nx = que[k][0] + dx[i]
                ny = que[k][1] + dy[i]
                if 0 <= nx < w and 0 <= ny < h and A[nx][ny] != "#" and d[nx][ny] == float("inf"):
                    d[nx][ny] = d[que[k][0]][que[k][1]] + 1
                    A[nx][ny] = "#"
                i += 1
        que = deque([])
        black = 0
        for i in range(h):
            for j in range(w):
              if A[i][j] == "#":
                  que.append((i, j))
                  black += 1
                  ans = max(ans, d[i][j])
        if black == h * w:
            white = False
      
        


h, w = map(int, input().split())
A = [list(input()) for _ in range(h)]
d = [[float("inf")] * w for _ in range(h)]
ans = 0
bfs()

print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import queue


nums=list(map(int,input().split()))
H = nums[0]
W = nums[1]

INF = 10**9
field = [[None for i in range(W)] for j in range(H)]
check = [[INF for i in range(W)] for j in range(H)]

black = 0

for i in range(H):
    str1 = input()
    for j in range(W):
        field[i][j] = str1[j]

ans = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            check[i][j] = 0
        elif j-1 >= 0 and check[i][j - 1] != INF:
            check[i][j] = check[i][j-1] + 1
            

for i in range(H):
    for j in range(W-1, -1, -1):
        if field[i][j] == "#":
            check[i][j] = 0
        elif j < W-1 and check[i][j + 1] != INF:
            check[i][j] = min(check[i][j], check[i][j + 1] + 1 )
            
            

for i in range(W):
    for j in range(H-1, -1, -1):
        if field[j][i] == "#":
            check[j][i] = 0
        elif j < H-1 and check[j + 1][i] != INF:
            check[j][i] = min(check[i][j],check[j + 1][i] + 1)
            
            
for i in range(W):
    for j in range(H):
        if field[j][i] == "#":
            check[j][i] = 0
        elif j-1>0 and check[j - 1][i] != INF:
            check[j][i] = min(check[i][j],check[j - 1][i] + 1)
            
for i in range(H):
    for j in range(W):
        if check[i][j] != INF:
            ans = max(ans, check[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import queue


nums=list(map(int,input().split()))
H = nums[0]
W = nums[1]

INF = 10**9
field = [[None for i in range(W)] for j in range(H)]
check = [[INF for i in range(W)] for j in range(H)]

black = 0

for i in range(H):
    str1 = input()
    for j in range(W):
        field[i][j] = str1[j]

ans = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            check[i][j] = 0
        elif j-1 >= 0 and check[i][j - 1] != INF:
            check[i][j] = check[i][j-1] + 1
            

for i in range(H):
    for j in range(W-1, -1, -1):
        if field[i][j] == "#":
            check[i][j] = 0
        elif j < W-1 and check[i][j + 1] != INF:
            check[i][j] = min(check[i][j], check[i][j + 1] + 1 )
            
            

for i in range(W):
    for j in range(H-1, -1, -1):
        if field[j][i] == "#":
            check[j][i] = 0
        elif j < H-1 and check[j + 1][i] != INF:
            check[j][i] = min(check[i][j],check[j + 1][i] + 1)
            
            
for i in range(W):
    for j in range(H):
        if field[j][i] == "#":
            check[j][i] = 0
        elif j-1>0 and check[j - 1][i] != INF:
            check[j][i] = min(check[i][j],check[j - 1][i] + 1)
            
for i in range(H):
    for j in range(W):
        ans = max(ans, check[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

H, W = rl()
A = [list(rs()) for _ in range(H)]
answer = 0
que = deque()
data = [[-1] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            data[i][j] = 0
            que.append((i, j))
while que:
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    p = que.popleft()
    for i in range(4):
        nx = p[0] + dx[i]
        ny = p[1] + dy[i]
        if 0 <= ny <= H-1 and 0 <= nx <= W-1:
            if data[ny][nx] == -1:
                data[ny][nx] = data[p[1]][p[0]] + 1
                que.append((nx, ny))
answer = max([max(z) for z in data])
print(answer)
# 54

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

H, W = rl()
A = [list(rs()) for _ in range(H)]
answer = 0
que = deque()
data = [[-1] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            data[i][j] = 0
            que.append((i, j))
while que:
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    p = que.popleft()
    for i in range(4):
        nx = p[0] + dx[i]
        ny = p[1] + dy[i]
        if 0 <= ny <= H-1 and 0 <= nx <= W-1:
            if data[ny][nx] == -1:
                data[ny][nx] = data[p[1][0]] + 1
                que.append((nx, ny))
answer = max([max(z) for z in data])
print(answer)
# 54

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
A = [input() for _ in range(H)]

INF = 10 ** 10
B = [[INF for _ in range(W)] for _ in range(W)]

for h in range(H):
  for w in range(W):
    if A[h][w] == "#":
      B[h][w] = 0

for h in range(H):
  for w in range(W):
    if B[h][w] == 0:
      stack = [(h, w)]
      while stack:
        hh, ww = stack.pop()
        if hh != 0:
          if B[hh][ww] + 1 < B[hh - 1][ww]:
            B[hh - 1][ww] = B[hh][ww] + 1
            stack.append((hh - 1, ww))

        if hh != H - 1:
          if B[hh][ww] + 1 < B[hh + 1][ww]:
            B[hh + 1][ww] = B[hh][ww] + 1
            stack.append((hh + 1, ww))


        if ww != 0:
          if B[hh][ww] + 1 < B[hh][ww - 1]:
            B[hh][ww - 1] = B[hh][ww] + 1
            stack.append((hh, ww - 1))

        if ww != W - 1:
          if B[hh][ww] + 1 < B[hh][ww + 1]:
            B[hh][ww + 1] = B[hh][ww] + 1
            stack.append((hh, ww + 1))
answer = 0
for h in range(H):
  for w in range(W):
    answer = max(answer, B[h][w])
print(answer)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, A: "List[str]"):
    isVisited = [[-1]*W for _ in range(H)]
    from collections import deque

    queue = deque()

    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                queue.append((i,j,0))

    while queue:
        i,j,count = queue.popleft()

        if 0<=i<H and 0<=j<W and isVisited[i][j] == -1:
            isVisited[i][j] = count
            for ix,iy in [(0,1),(1,0),(-1,0),(0,-1)]:
               queue.append((i+ix,j+iy,count+1))
 
    answer = 0
    for i in range(H):
        answer = max(answer,max(isVisited[i]))
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())

MAP=[["." for i in range (W)]for i in range(H)]
LIST=[(0,0,0)]*10**6

l=0
r=0
for i in range(H):
    A=list(input())
    for j in range(W):
        if(A[j]=="#"):
            LIST[r]=(i,j,0)
            r+=1

while(True):
    T=LIST[l]
    i=T[0]
    j=T[1]
    if(i>0):
        if(MAP[i-1][j]=="."):
            MAP[i-1][j]="#"
            LIST[r]=(i-1,j,T[2]+1)
            r+=1  
    if(i+1<H):
        if(MAP[i+1][j]=="."):
            MAP[i+1][j]="#"
            LIST[r]=(i+1,j,T[2]+1)
            r+=1  
    if(j>0):
        if(MAP[i][j-1]=="."):
            MAP[i][j-1]="#"
            LIST[r]=(i,j-1,T[2]+1)
            r+=1  
    if(j+1<W):
        if(MAP[i][j+1]=="."):
            MAP[i][j+1]="#"
            LIST[r]=(i,j+1,T[2]+1)
            r+=1
    l+=1
    if(r<=l):
        print(LIST[r-1][2])
        exit()

Submission status: Runtime Error

Solution code:
import copy
H,W=map(int,input().split())


MAP=[["." for i in range (W)]for i in range(H)]
LIST=[0]*10**6

l=0
r=0
for i in range(H):
    A=list(input())
    for j in range(W):
        if(A[j]=="#"):
            LIST[r]=(i,j,0)
            r+=1

while(True):
    T=LIST[l]
    i=T[0]
    j=T[1]
    if(i>0):
        if(MAP[i-1][j]=="."):
            MAP[i-1][j]="#"
            LIST[r]=(i-1,j,T[2]+1)
            r+=1  
    if(i+1<H):
        if(MAP[i+1][j]=="."):
            MAP[i+1][j]="#"
            LIST[r]=(i+1,j,T[2]+1)
            r+=1  
    if(j>0):
        if(MAP[i][j-1]=="."):
            MAP[i][j-1]="#"
            LIST[r]=(i,j-1,T[2]+1)
            r+=1  
    if(j+1<W):
        if(MAP[i][j+1]=="."):
            MAP[i][j+1]="#"
            LIST[r]=(i,j+1,T[2]+1)
            r+=1
    l+=1
    if(r<=l):
        print(LIST[r-1][2])
        exit()

Submission status: Runtime Error

Solution code:
from collections import deque

H,W=map(int,input().split())
if H==1 and W==1:
    print(0)
    exit()

A=[]
B=deque([])
C=[[-1]*W for i in range(H)]

for h in range(H):
    temp=list(input())
    A.append(temp)
    for w in range(W):
        if A[h][w]=="#":
            B.append([h,w,0])
            C[h][w]=0



step=[[1,0],[-1,0],[0,1],[0,-1]]
ans=[]
while B:
    P=B.popleft()
    for x,y in step:
        if 0<=P[0]+y<=H-1 and 0<=P[1]+x<=W-1:
            if C[P[0]+y][P[1]+x]==-1:
                B.append([P[0]+y,P[1]+x,P[2]+1])
                C[P[0]+y][P[1]+x]=1
                ans.append(P[2]+1)


print(max(ans))

Submission status: Runtime Error

Solution code:
from collections import deque

H,W=map(int,input().split())
A=[]
B=deque([])
C=[[-1]*W for i in range(H)]
for h in range(H):
    temp=list(input())
    A.append(temp)
    for w in range(W):
        if A[h][w]=="#":
            B.append([h,w,0])
            C[h][w]=0


step=[[1,0],[-1,0],[0,1],[0,-1]]
ans=[]
while B:
    P=B.popleft()
    for x,y in step:
        if 0<=P[0]+y<=H-1 and 0<=P[1]+x<=W-1:
            if C[P[0]+y][P[1]+x]==-1:
                B.append([P[0]+y,P[1]+x,P[2]+1])
                C[P[0]+y][P[1]+x]=1
                ans.append(P[2]+1)

print(max(ans))

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
h,w=map(int, input().split(' '))
a = [list(input()) for i in range(h)]
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            a[i][j] = 0
            q.append([i,j,0])
        else:
            a[i][j] = -1
while q:
    [s,t,cnt] = q.popleft()
    if s>0 and a[s-1][t] == -1:
        q.append([s-1,t,cnt+1])
        a[s-1][t] = cnt+1
    if s<h-1 and a[s+1][t] == -1:
        q.append([s+1,t,cnt+1])
        a[s+1][t] = cnt+1
    if t>0 and a[s][t-1] == -1:
        q.append([s,t-1,cnt+1])
        a[s][t-1] = cnt+1
    if t<w-1 and a[s][t+1] == -1:
        q.append([s,t+1,cnt+1])
        a[s][t+1] = cnt+1
ans = 0
for i in range(h):
    ans = max(ans,max(a[i]))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
h,w=map(int, input().split(' '))
a = [list(input()) for i in range(h)]
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            a[i][j] = 0
            q.append([i,j,0])
        else:
            a[i][j] = -1
while q:
    [s,t,cnt] = q.popleft()
    if s>0 and nl[s-1][t] == -1:
        q.append([s-1,t,cnt+1])
        a[s-1][t] = cnt+1
    if s<h-1 and nl[s+1][t] == -1:
        q.append([s+1,t,cnt+1])
        a[s+1][t] = cnt+1
    if t>0 and nl[s][t-1] == -1:
        q.append([s,t-1,cnt+1])
        a[s][t-1] = cnt+1
    if t<w-1 and nl[s][t+1] == -1:
        q.append([s,t+1,cnt+1])
        a[s][t+1] = cnt+1
ans = 0
for i in range(h):
    ans = max(ans,max(a[i]))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
h,w=map(int, input().split(' '))
a = [input() for i in range(h)]
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            a[i][j] = 0
            q.append([i,j,0])
        else:
            a[i][j] = -1
while q:
    [s,t,cnt] = q.popleft()
    if s>0 and nl[s-1][t] == -1:
        q.append([s-1,t,cnt+1])
        a[s-1][t] = cnt+1
    if s<h-1 and nl[s+1][t] == -1:
        q.append([s+1,t,cnt+1])
        a[s+1][t] = cnt+1
    if t>0 and nl[s][t-1] == -1:
        q.append([s,t-1,cnt+1])
        a[s][t-1] = cnt+1
    if t<w-1 and nl[s][t+1] == -1:
        q.append([s,t+1,cnt+1])
        a[s][t+1] = cnt+1
ans = 0
for i in range(h):
    ans = max(ans,max(a[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

lst = []
H, W = map(int, input().split())
for _ in range(H):
    l = list(input())
    lst.append(l)

from collections import deque
yx =[[1,0], [-1, 0], [0,1], [0,-1]]

def bfs(sy, sx, gy, gx):
    queue = deque()
    visited = [[-1] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if lst[h][w] == '#':
                visited[h][w] = 0
                queue.append([h, w])            
    while queue:
        now = queue.popleft()
        if all([ not -1 in _ for _ in visited]):
            return max(sum(visited, []))
        for _ in yx:
            ny = now[0] + _[0]
            nx = now[1] + _[1]
            if 0 <= nx < W and 0 <= ny < H:
                if visited[ny][nx] == -1:
                    visited[ny][nx] = visited[now[0]][now[1]] +1
                    queue.append([ny, nx])
    return -1

step = bfs()
print(step)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

lst = []
H, W = map(int, input().split())
for _ in range(H):
    l = list(input())
    lst.append(l)

from collections import deque
xy =[[1,0], [-1, 0], [0,1], [0,-1]]

def bfs(sy, sx, gy, gx):
    queue = deque()
    visited = [[-1] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if lst[h][w] == '#':
                visited[h][w] = 0
                queue.append([h, w])            
    print(visited)
    while queue:
        now = queue.popleft()
        if all([ not -1 in _ for _ in visited]):
            print(visited)
            return max(sum(visited, []))
        for _ in xy:
            ny = now[0] + _[1]
            nx = now[1] + _[0]
            if 0 <= nx < W and 0 <= ny < H:
                if visited[ny][nx] == -1:
                    visited[ny][nx] = visited[now[0]][now[1]] +1
                    queue.append([ny, nx])
    return -1

step = bfs()
print(step)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

lst = []
H, W = map(int, input().split())
for _ in range(H):
    l = list(input())
    lst.append(l)

from collections import deque
x = [1, -1, 0, 0]
y = [0, 0, 1, -1]

def bfs(sy, sx, gy, gx):
    queue = deque()
    visited = [[-1] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if lst[h][w] == '#':
                visited[h][w] = 0
                queue.append([h, w])            
    print(visited)
    while queue:
        now = queue.popleft()
        if all([ not -1 in _ for _ in visited]):
            print(visited)
            return max(sum(visited, []))
        for _ in xy:
            ny = now[0] + _[1]
            nx = now[1] + _[0]
            if 0 <= nx < W and 0 <= ny < H:
                if visited[ny][nx] == -1:
                    visited[ny][nx] = visited[now[0]][now[1]] +1
                    queue.append([ny, nx])
    return -1

step = bfs()
print(step)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
B = []
b = 0
dp = [[False for i in range(W)]for j in range(H)]
for i in range(H):
    A = input()
    for j in range(W):
        if A[j] == '.':
            dp[i][j] = True
        else:
            B.append((i,j))
            b += 1
ans = 0
D = [(-1,0),(1,0),(0,1),(0,-1)]
import copy
while b != H*W:
    ans += 1
    C = copy.deepcopy(B)
    B = []
    for k in range(len(C)):
        x,y = C[k]
        z = (x,y)
        for d in D:
            i,j = d
            X = x + i
            Y = y + j
            if 0 <= X < W:
                if 0 <= Y < H:
                    z = (X,Y)
                else:
                    continue
            else:
                continue
            if dp[X][Y]:
                dp[X][Y] = False
                B.append(z)
                b += 1
print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
w = []
B = []
dp = [[False for i in range(W)]for j in range(H)]
for i in range(H):
    A = input()
    for j in range(W):
        if A[j] == '.':
            w.append((i,j))
            dp[i][j] = True
        else:
            B.append((i,j))
ans = 0
D = [(-1,0),(1,0),(0,1),(0,-1)]
F = []
import copy
while len(F) + len(B) != H*W:
    ans += 1
    C = copy.deepcopy(B)
    B = []
    for k in range(len(C)):
        x,y = C[k]
        z = (x,y)
        F.append(z)
        for d in D:
            i,j = d
            X = x + i
            Y = y + j
            if 0 <= X < W:
                if 0 <= Y < H:
                    z = (X,Y)
                else:
                    continue
            else:
                continue
            if dp[X][Y]:
                dp[X][Y] = False
                B.append(z)
print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
W = []
B = []
for i in range(1,H+1):
    A = list(map(int,input().split()))
    for j in range(1,W+1):
        if A[j-1] == '.':
            W.append((i,j))
        else:
            B.append((i,j))
ans = 0
import copy
while len(W) == 0:
    ans += 1
    C = copy.deepcopy(B)
    B = []
    while len(C) == 0:
        x,y = C.pop()
        for i in range(-1,2):
            for j in range(-1,2):
                X = x + i
                Y = y + j
                z = (X,Y)
                B.append(z)
                if z in W:
                    W.remove(z)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = map(int, input().split())
grid = (np.array([list(input())
                  for _ in range(h)]) == "#")
cost = np.full((h, w), 1000, dtype=np.int64)
cost[grid[:, :]] = 0

for j in range(2):
    for i in range(h - 1):
        cost[i] = np.minimum(cost[i], cost[i + 1] + 1)
    for i in range(1, h)[::-1]:
        cost[i] = np.minimum(cost[i], cost[i - 1] + 1)
    for i in range(w - 1):
        cost[i] = np.minimum(cost[:, i], cost[:, i + 1] + 1)
    for n in range(1, w)[::-1]:
        cost[i] = np.minimum(cost[:, i], cost[:, i - 1] + 1)

print(np.max(cost))

Submission status: Runtime Error

Solution code:
import numpy as np
temp=list(map(int, input().split()))
H=temp[0]
W=temp[1]



S=[]
for i in range(H):
  temp=input()
  a=np.zeros(W).astype(int)
  for j in range(W):
      if temp[j]=='#':
        #a[j]=1
        S.append((i,j))
      #else:
        #a[j]=0
  #c[i]=a
def Adj(u):
  adj=[]
  for v in [(u[0]-1,u[1]),(u[0]+1,u[1]),(u[0],u[1]-1),(u[0],u[1]+1)]:
      if v[0]>=0 and v[0]<H and v[1]>=0 and v[1]<W:
        adj.append(v)
  return adj  

d=np.zeros((H,W)).astype(int)+H*W

def BFS(s):
  c=np.zeros((H,W)).astype(int)
  for o in S:
    c[o[0],o[1]]=1
  d[s[0],s[1]]=0
  Q=[s]
  while Q !=[]:
    u=Q[0]
    del Q[0]
    for v in Adj(u):  
      if c[v[0],v[1]]==0 and d[u[0],u[1]]+1<d[v[0],v[1]]:
        c[v[0],v[1]]=1
        d[v[0],v[1]]=d[u[0],u[1]]+1
        Q.append(v)
  return
def main():
  table=np.zeros((len(S),H,W)).astype(int)
  for i in range(len(S)):
    BFS(S[i])
  print(int(np.max(d)))
main()

Submission status: Runtime Error

Solution code:
import numpy as np
temp=list(map(int, input().split()))
H=temp[0]
W=temp[1]



S=[]
for i in range(H):
  temp=input()
  a=np.zeros(W).astype(int)
  for j in range(W):
      if temp[j]=='#':
        #a[j]=1
        S.append((i,j))
      #else:
        #a[j]=0
  #c[i]=a
def Adj(u):
  adj=[]
  for v in [(u[0]-1,u[1]),(u[0]+1,u[1]),(u[0],u[1]-1),(u[0],u[1]+1)]:
      if v[0]>=0 and v[0]<H and v[1]>=0 and v[1]<W:
        adj.append(v)
  return adj  


def BFS(s):
  d=np.zeros((H,W)).astype(int)-1
  c=np.zeros((H,W)).astype(int)
  for o in S:
    c[o[0],o[1]]=1
  d[s[0],s[1]]=0
  Q=[s]
  while Q !=[]:
    u=Q[0]
    del Q[0]
    for v in Adj(u):  
      if c[v[0],v[1]]==0:
        c[v[0],v[1]]=1
        d[v[0],v[1]]=d[u[0],u[1]]+1
        Q.append(v)
  return d
def main():
  table=np.zeros((len(S),H,W)).astype(int)
  for i in range(len(S)):
    table[i]=BFS(S[i])
  ans=np.max(np.min(table,axis=0))
  print(ans)
main()

Submission status: Runtime Error

Solution code:
import numpy as np
temp=list(map(int, input().split()))
H=temp[0]
W=temp[1]



S=[]
for i in range(H):
  temp=input()
  a=np.zeros(W).astype(int)
  for j in range(W):
      if temp[j]=='#':
        #a[j]=1
        S.append((i,j))
      #else:
        #a[j]=0
  #c[i]=a
def Adj(u):
  adj=[]
  for v in [(u[0]-1,u[1]),(u[0]+1,u[1]),(u[0],u[1]-1),(u[0],u[1]+1)]:
      if v[0]>=0 and v[0]<H and v[1]>=0 and v[1]<W:
        adj.append(v)
  return adj  


def BFS(s):
  d=np.zeros((H,W)).astype(int)-1
  c=np.zeros((H,W)).astype(int)
  c[s[0],s[1]]=1
  d[s[0],s[1]]=0
  Q=[s]
  while Q !=[]:
    u=Q[0]
    del Q[0]
    for v in Adj(u):  
      if c[v[0],v[1]]==0:
        c[v[0],v[1]]=1
        d[v[0],v[1]]=d[u[0],u[1]]+1
        Q.append(v)
  return d
def main():
  table=np.zeros((len(S),H,W)).astype(int)
  for i in range(len(S)):
    table[i]=BFS(S[i])
  ans=np.max(np.min(table,axis=0))
  print(ans)
main()

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
area = []
blacks = []

for i in range(h):
    array = input()
    for j in range(len(array)):
        if array[j] == '#':
            blacks.append([i,j])
    area.append(array)

def check(i, j):
    min_dist = 10000
    for l, m in blacks:
        dist = abs(i - l) + abs(j - m)
        if dist < min_dist:
            min_dist = dist
    return min_dist

max_dist = 0

for i in range(h):
    for j in range(w):
        if area[i][j] != '#':
            dist = check(i, j)
        if dist > max_dist:
            max_dist = dist

print(max_dist)

Submission status: Runtime Error

Solution code:
from collections import deque

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

draw = ((1, 0), (-1, 0), (0, 1), (0, -1))
todo = deque([])
done = [[False for _ in range(w)] for _ in range(h)]
grid = []
for i in range(h):
    s = input()
    for j, k in enumerate(s):
        if k == "#":
            todo.append((i, j))
            done[i][j] = True
    grid.append(list(s))

cnt = -1
while todo:
    for i in range(len(todo)):
        n, m = todo.popleft()
        for dn, dm in draw:
            if not 0 <= n + dn < w or not 0 <= m + dm < h:
                pass
            elif done[n + dn][m + dm]:
                pass
            elif grid[n + dn][m + dm] == "#":
                pass
            elif grid[n + dn][m + dm] == ".":
                todo.append((n + dn, m + dm))
                done[n + dn][m + dm] = True
                grid[n + dn][m + dm] = "#"
    cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque

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

draw = ((1, 0), (-1, 0), (0, 1), (0, -1))
todo = deque([])
done = [[False for _ in range(w)] for _ in range(h)]
grid = []
for i in range(h):
    s = input()
    for j, k in enumerate(s):
        if k == "#":
            todo.append((i, j))
            done[i][j] = True
    grid.append(list(s))

cnt = 0
while todo:
    tmp = deque([])
    while todo:
        n, m = todo.popleft()
        for dn, dm in draw:
            if not 0 <= n + dn < w or not 0 <= m + dm < h:
                pass
            elif done[n + dn][m + dm]:
                pass
            elif grid[n + dn][m + dm] == "#":
                pass
            elif grid[n + dn][m + dm] == ".":
                tmp.append((n + dn, m + dm))
                done[n + dn][m + dm] = True
                grid[n + dn][m + dm] = "#"
    todo = tmp
    cnt += 1

print(cnt - 1)

Submission status: Runtime Error

Solution code:
from collections import deque
from copy import copy

r, c = map(int, input().split())
map_ = [[c == "#" for c in input().strip()] for _ in range(r)]

queue = deque([(i, j) for j in range(r) for i in range(c) if map_[i][j]])

def bfs(maze, queue):
    depth = 0
    while True:
        depth += 1
        nextqueue = deque()
        while queue:
            posr, posc = queue.popleft()
            for dr, dc in zip([1, -1, 0, 0], [0, 0, 1, -1]):
                nextr = posr + dr
                nextc = posc + dc
                if 0 <= nextr < r and 0 <= nextc < c and not maze[nextr][nextc]:
                    maze[nextr][nextc] = True
                    nextqueue.append((nextr, nextc))
        if nextqueue:
            queue = copy(nextqueue)
        else:
            return depth - 1


print(bfs(map_, queue))

Submission status: Runtime Error

Solution code:
from collections import deque
from copy import copy

r, c = map(int, input().split())
maze = [[True] * (c + 2)] + [[True] + [c == "#" for c in input().strip()] + [True] for _ in range(r)] + [[True] * (c + 2)]

queue = deque([(i, j) for j in range(1, r + 1) for i in range(1, c + 1) if maze[i][j]])

def BFS(maze, queue):
    depth = 0

    while True:
        depth += 1
        nextqueue = deque()
        while queue:
            posr, posc = queue.popleft()
            if not maze[posr + 1][posc]:
                maze[posr + 1][posc] = True
                nextqueue.append((posr + 1, posc))
            if not maze[posr - 1][posc]:
                maze[posr - 1][posc] = True
                nextqueue.append((posr - 1, posc))
            if not maze[posr][posc + 1]:
                maze[posr][posc + 1] = True
                nextqueue.append((posr, posc + 1))
            if not maze[posr][posc - 1]:
                maze[posr][posc - 1] = True
                nextqueue.append((posr, posc - 1))

        if not nextqueue:
            return depth - 1
        else:
            queue = copy(nextqueue)


print(BFS(maze, queue))

Submission status: Runtime Error

Solution code:
from collections import deque
from copy import copy

r, c = map(int, input().strip().split())
maze = [["#"] * (c + 2)] + [["#"] + list(input().strip()) + ["#"] for _ in range(r)] + [["#"] * (c + 2)]

queue = deque([(i, j) for j in range(1, r + 1) for i in range(1, c + 1) if maze[i][j] == "#"])

def BFS(maze, queue):
    depth = 0
    nextqueue = deque()

    while queue:
        depth += 1
        while queue:
            posr, posc = queue.popleft()
            for r, c in zip([1, -1, 0, 0], [0, 0, 1, -1]):
                nextr = posr + r
                nextc = posc + c
                terrein = maze[nextr][nextc]
                if terrein == ".":
                    maze[nextr][nextc] = "#"
                    nextqueue.append((nextr, nextc))
        queue = copy(nextqueue)
        nextqueue.clear()

    return depth - 1

print(BFS(maze, queue))

Submission status: Runtime Error

Solution code:

from collections import deque

r, c = map(int, input().strip().split())
maze = [[0] * (c + 2)] + [[0] + [0 if c == "#" else float("inf") for c in input().strip()] + [0] for _ in range(r)] + [[0] * (c + 2)]

queue = deque([(i, j) for j in range(1, r + 1) for i in range(1, c + 1) if maze[i][j] == 0])

def BFS_maze(maze, queue):
    depth = 0
    nextqueue = deque()

    while queue:
        depth += 1
        while queue:
            posr, posc = queue.popleft()
            for r, c in zip([1, -1, 0, 0], [0, 0, 1, -1]):
                nextr = posr + r
                nextc = posc + c
                terrein = maze[nextr][nextc]
                if terrein > depth:
                    maze[nextr][nextc] = depth
                    nextqueue.append((nextr, nextc))
        queue = nextqueue
        nextqueue = deque()

BFS_maze(maze, queue)
print(max(inner for outer in maze for inner in outer))