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:
class Solver(object):
    def recur(self, i, j, count):
        if i < 0 or j < 0 or i >= self.h or j >= self.w:
            return

        if self.a[i][j] == '#':
            return
        
        if ((i, j) in self.m) and self.m[(i, j)] < count:
            return
        
        self.m[(i, j)] = count
        self.recur(i - 1, j, count + 1)
        self.recur(i + 1, j, count + 1)
        self.recur(i, j - 1, count + 1)
        self.recur(i, j + 1, count + 1)

    def run(self):
        self.m = {}
        self.h, self.w = [int(x) for x in input().split(' ')]
        self.a = []
        for i in range(self.h):
            self.a.append(list(input().rstrip()))

        for i in range(self.h):
            for j in range(self.w):
                if self.a[i][j] == '#':
                    self.recur(i - 1, j, 1)
                    self.recur(i + 1, j, 1)
                    self.recur(i, j - 1, 1)
                    self.recur(i, j + 1, 1)

        max = 0
        for i in range(self.h):
            for j in range(self.w):
                if ((i, j) in self.m) and (max < self.m[(i, j)]):
                    max = self.m[(i, j)]
        print(max)

Solver().run()

Submission status: Runtime Error

Solution code:
q=[]
h,w=map(int,input().split())
arr=[[True]*(w+2)]
for i in range(h):
  tmp=[True]
  for j in input():
    if j=='#':
      tmp.append(True)
    else:
      tmp.append(False)
  tmp.append(True)
  arr.append(tmp)
arr.append([[True]*(w+2)])
for i in range(1,h+1):
  for j in range(1,w+1):
    if arr[i][j]:
      q.append([i,j])
ans=0
while 1:
  nq=[]
  for y,x in q:
    if not arr[y-1][x]:
      nq.append([y-1,x])
      arr[y-1][x]=True
    if not arr[y][x-1]:
      nq.append([y,x-1])
      arr[y][x-1]=True
    if not arr[y+1][x]:
      nq.append([y+1,x])
      arr[y+1][x]=True
    if not arr[y][x+1]:
      nq.append([y,x+1])
      arr[y][x+1]=True
  if len(nq)==0:
    print(ans)
    break
  else:
    q=nq
    ans+=1

Submission status: Runtime Error

Solution code:
import copy
 
H, W = map(int, input().split())
 
def fib(msLst, i, j):
    if i > 0:
        msLst[i - 1][j] = '#'
    if i < W - 1:
        msLst[i + 1][j] = '#'
    if j > 0:
        msLst[i][j - 1] = '#'
    if j < H - 1:
        msLst[i][j + 1] = '#'
 
ansCnt = 0
masLst = [['' for i in range(H)] for j in range(W)]
for i in range(W):
    tmp = input()
    for j in range(len(tmp)):
        masLst[i][j] = tmp[j]
 
loopBoean = True
 
while loopBoean:
    jloop = True
    tmp = 0
    msLst = copy.deepcopy(masLst)
    for i in range(W):
        for j in range(H):
            if masLst[i][j] == '#':
                fib(msLst, i, j)
            if masLst[i][j] == '.':
                if tmp == 0:
                    tmp = 1
            if msLst[i][j] != '#':
                jloop = False
    masLst = msLst
    if tmp == 1:
        ansCnt += tmp
 
    if jloop == True:
        loopBoean = False
 
print(ansCnt)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
pli = []
lens = [[2001 for i in range(w)] for j in range(h)]
for x in range(w):
    for y in range(h):
        if a[x][y] == '#':
            pli.append([x, y])
            lens[x][y] = 0
for y in range(h):
    for x in range(w):
        if [x, y] not in pli:
            for p in pli:
                tmp = abs(x - p[0]) + abs(y - p[1])
                if lens[x][y] > tmp:
                    lens[x][y] = tmp
print(np.max(lens))

Submission status: Runtime Error

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def change(a, x, y, h, w):
    ct = 0
    if x > 0:
        if a[x - 1][y] == '.':
            a[x - 1][y] = '#'
            ct += 1
    if y > 0:
        if a[x][y - 1] == '.':
            a[x][y - 1] = '#'
            ct += 1
    if x < w - 1:
        if a[x + 1][y] == '.':
            a[x + 1][y] = '#'
            ct += 1
    if y < h - 1:
        if a[x][y + 1] == '.':
            a[x][y + 1] = '#'
            ct += 1
    return ct


h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
for y in range(h):
    for x in range(w):
        if a[x][y] == '#':
            ct += 1
#print(ct)
while ct != ds:
    li = []
    for y in range(h):
        for x in range(w):
            if a[x][y] == '#':
                li.append([x, y])
    for p in li:
        ct += change(a, p[0], p[1], h, w)
    #print(ct)
    trial += 1

print(trial)

Submission status: Runtime Error

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def change(a, x, y, h, w):
    ct = 0
    if x > 0:
        if a[x - 1][y] == '.':
            a[x - 1][y] = '#'
            ct += 1
    if y > 0:
        if a[x][y - 1] == '.':
            a[x][y - 1] = '#'
            ct += 1
    if x < w - 1:
        if a[x + 1][y] == '.':
            a[x + 1][y] = '#'
            ct += 1
    if y < h - 1:
        if a[x][y + 1] == '.':
            a[x][y + 1] = '#'
            ct += 1
    return ct


h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
for y in range(h):
    for x in range(w):
        if a[x][y] == '#':
            ct += 1

while ct != ds:
    li = []
    for y in range(h):
        for x in range(w):
            if a[x][y] == '#':
                li.append([x, y])
    for p in li:
        ct += change(a, p[0], p[1], h, w)
    trial += 1

print(trial)

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = in1.split(" ")
h = int(h_w[0])
w = int(h_w[1])

board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    for j in range(w):
        boardline = boardline.replace("#", "0")
        boardline = boardline.replace(".", "1")
        board[i][j] = boardline[j]

board = board.astype(np.int64)

num = 0
while(np.amax(board) == 1):
    num = num + 1

    board_hm1 = np.ones((h,w))
    board_hp1 = np.ones((h,w))
    board_wm1 = np.ones((h,w))
    board_wp1 = np.ones((h,w))

    board_hm1[:(h-1)] = board[1:]
    board_hp1[1:] = board[:(h-1)]
    board_wm1[:,:(w-1)] = board[:,1:]
    board_wp1[:,1:] = board[:,:(w-1)]

    board = board * board_hm1
    board = board * board_hp1
    board = board * board_wm1
    board = (board * board_wp1).astype(np.int64)
print(num)

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = in1.split(" ")
h = int(h_w[0])
w = int(h_w[1])


board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    for j in range(w):
        boardline = boardline.replace("#", "0")
        boardline = boardline.replace(".", "1")
        boardlinesplit = boardline.split(" ")
        board[i][j] = boardlinesplit[0][j]

board = board.astype(np.int64)

num = 0
while(np.amax(board) == 1):
    num = num + 1

    board_hm1 = np.ones((h,w))
    board_hp1 = np.ones((h,w))
    board_wm1 = np.ones((h,w))
    board_wp1 = np.ones((h,w))

    board_hm1[:(h-1)] = board[1:]
    board_hp1[1:] = board[:(h-1)]
    board_wm1[:,:(w-1)] = board[:,1:]
    board_wp1[:,1:] = board[:,:(w-1)]

    tmp = board
    tmp = tmp * board_hm1
    tmp = tmp * board_hp1
    tmp = tmp * board_wm1
    tmp = tmp * board_wp1
    board = tmp
    board = board.astype(np.int64)
print(num)

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = in1.split(" ")
h = int(h_w[0])
w = int(h_w[1])


board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    for j in range(w):
        boardline = boardline.replace("#", "0")
        boardline = boardline.replace(".", "1")
        boardlinesplit = boardline.split(" ")
        board[i][j] = boardlinesplit[0][j]


num = 0
while(np.amax(board) == 1):
    board = board.astype(np.int64)
    board_hm1 = np.ones((h,w))
    board_hp1 = np.ones((h,w))
    board_wm1 = np.ones((h,w))
    board_wp1 = np.ones((h,w))
    
    board_hm1[:(h-1)] = board[1:]
    board_hp1[1:] = board[:(h-1)]
    board_wm1[:,:(w-1)] = board[:,1:]
    board_wp1[:,1:] = board[:,:(w-1)]

    num = num + 1
    tmp = board
    tmp = tmp * board_hm1
    tmp = tmp * board_hp1
    tmp = tmp * board_wm1
    tmp = tmp * board_wp1
    board = tmp
print(num)

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = in1.split(" ")
h = int(h_w[0])
w = int(h_w[1])


board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    for j in range(w):
        boardline = boardline.replace("#", "0")
        boardline = boardline.replace(".", "1")
        boardlinesplit = boardline.split(" ")
        board[i][j] = boardlinesplit[0][j]

board = board.astype(np.int64)
board_hm1 = np.ones((h,w))
board_hp1 = np.ones((h,w))
board_wm1 = np.ones((h,w))
board_wp1 = np.ones((h,w))


num = 0
while(np.amax(board) == 1):
    board_hm1[:(h-1)] = board[1:]
    board_hp1[1:] = board[:(h-1)]
    board_wm1[:,:(w-1)] = board[:,1:]
    board_wp1[:,1:] = board[:,:(w-1)]

    num = num + 1
    tmp = board
    tmp = tmp * board_hm1
    tmp = tmp * board_hp1
    tmp = tmp * board_wm1
    tmp = tmp * board_wp1
    board = tmp
print(num)

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = in1.split(" ")
h = int(h_w[0])
w = int(h_w[1])


board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    boardline = boardline.replace("#", "0")
    boardline = boardline.replace(".", "1")
    boardlinesplit = boardline.split(" ")
    board[i] = boardlinesplit

board = board.astype(np.int64)
board_hm1 = np.vstack((board[len(board.shape[1]-1)],np.ones((1,len(board.shape[1])))))
board_hp1 = np.vstack((np.ones((1,len(board.shape[1]))),board[1:]))
board_wm1 = np.hstack((board[:,len(board[0]-1)], np.ones((len(board.shape[0]),1))))
board_wp1 = np.hstack((np.ones((len(board.shape[0]),1)), board[:,1:]))

num = 0
while(max(board) == 1):
    num = num + 1
    tmp = board
    tmp = tmp * board_hm1
    tmp = tmp * board_hp1
    tmp = tmp * board_wm1
    tmp = tmp * board_wp1
    board = tmp
    board = board.astype(np.int64)
    board_hm1 = np.vstack((board[len(board.shape[1]-1)],np.ones((1,len(board.shape[1])))))
    board_hp1 = np.vstack((np.ones((1,len(board.shape[1]))),board[1:]))
    board_wm1 = np.hstack((board[:,len(board[0]-1)], np.ones((len(board.shape[0]),1))))
    board_wp1 = np.hstack((np.ones((len(board.shape[0]),1)), board[:,1:]))


print(num)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
L = []
for i in range(h):
  L.append(list(input()))
B = []
W = []
for i in range(h):
  for j in range(w):
    if L[i][j] == '.':
      W.append([i,j])
    else:
      B.append([i,j])
anslist = []
for i in range(len(W)):
  ans = float('inf')
  for j in range(len(B)):
    if ans >= abs(W[i][0]-B[j][0])+abs(W[i][1]-B[j][1]):
      ans = abs(W[i][0]-B[j][0])+abs(W[i][1]-B[j][1])
  anslist.append(ans)
print(max(anslist))

Submission status: Runtime Error

Solution code:
import numpy as np

in1 = input()
h_w = h_w.split(" ")
h = int(h_w[0])
w = int(h_w[1])


board = np.zeros((w,h))

for i in range(h):
    boardline = input()
    boardline = boardline.replace("#", "0")
    boardline = boardline.replace(".", "1")
    boardlinesplit = boardline.split(" ")
    board[i] = boardlinesplit

board = board.astype(np.int64)
board_hm1 = np.vstack((board[len(board.shape[1]-1)],np.ones((1,len(board.shape[1])))))
board_hp1 = np.vstack((np.ones((1,len(board.shape[1]))),board[1:]))
board_wm1 = np.hstack((board[:,len(board[0]-1)], np.ones((len(board.shape[0]),1))))
board_wp1 = np.hstack((np.ones((len(board.shape[0]),1)), board[:,1:]))

num = 0
while(max(board) == 1):
    num = num + 1
    tmp = board
    tmp = tmp * board_hm1
    tmp = tmp * board_hp1
    tmp = tmp * board_wm1
    tmp = tmp * board_wp1
    board = tmp
    board = board.astype(np.int64)
    board_hm1 = np.vstack((board[len(board.shape[1]-1)],np.ones((1,len(board.shape[1])))))
    board_hp1 = np.vstack((np.ones((1,len(board.shape[1]))),board[1:]))
    board_wm1 = np.hstack((board[:,len(board[0]-1)], np.ones((len(board.shape[0]),1))))
    board_wp1 = np.hstack((np.ones((len(board.shape[0]),1)), board[:,1:]))


print(num)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
fuchi = {}
kuro = {}
for i in range(h):
    Ai = input()
    for j, Aij in enumerate(Ai):
        if Aij == "#":
            fuchi[(i, j)] = 1
            kuro[(i, j)] = 1

def add(poj):
    res = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    if poj[0] == 0:
        res[0] = 0
    if poj[0] == h - 1:
        res[1] = 0
    if poj[1] == 0:
        res[2] = 0
    if poj[1] == w - 1:
        res[3] = 0
    return res

c = 0
size = 0

while(True):
    pojs = list(fuchi.keys())
    if len(

Submission status: Runtime Error

Solution code:
delta=[(1,0),(-1,0),(0,1),(0,-1)]

def dig(tag,end):
    h=tag[0]
    w=tag[1]
    next=set()
    for y, x in delta:
      	h2 = h + y
        w2 = w + x
        if not h2<0 and not h2>=H and not w2<0 and not w2>=W and tag not in end:
            next.add((h2,w2))
    return next

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

for h in range(H):
    line=input()
    for w in range(W):
        if line[w] == '#':
            black_mass.add((h,w))

count=-1
end=set()
targets=black_mass
while True:
    count+=1
    next_que=set()
    for tag in targets:
        next_que.update(dig(tag,end))
        end.add(tag)
    targets=next_que
    if len(end) == H*W:
        print(count)
        exit(0)

Submission status: Runtime Error

Solution code:
import queue

H, W = map(int, input().split())
m = [""] * H
n = [[0]*W for i in range(H)]
q = queue.Queue()

for h in range(H):
    m[h] = input()
    for w in range(W):
        if m[h][w] == '#':
            n[h][w] = 1
            q.put([h, w])

mx = 1
while 1:
    mx += 1
    nq = queue.Queue()
    for h, w in q:
        if h > 0 and n[h - 1][w] == 0:
            n[h - 1][w] = mx
            nq.put([h-1, w])
        if w > 0 and n[h][w - 1] == 0:
            n[h][w - 1] = mx
            nq.put([h, w-1])
        if h + 1 < H and n[h + 1][w] == 0:
            n[h + 1][w] = mx
            nq.put([h+1, w])
        if w + 1 < W and n[h][w + 1] == 0:
            n[h][w + 1] = mx
            nq.put([h, w + 1])
    if nq.empty():
        break
    else:
        q = nq

print(mx-1)

Submission status: Runtime Error

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
sl_move, sr_move, su_move, sd_move = [], [], [], []
tl_move, tr_move, tu_move, td_move = [], [], [], []
for i in s:
    if i == 'L':
        sl_move += [-1]
        sr_move += [0]
        su_move += [0]
        sd_move += [0]
    elif i == 'R':
        sl_move += [0]
        sr_move += [1]
        su_move += [0]
        sd_move += [0]
    elif i == 'U':
        sl_move += [0]
        sr_move += [0]
        su_move += [-1]
        sd_move += [0]
    elif i == 'D':
        sl_move += [0]
        sr_move += [0]
        su_move += [0]
        sd_move += [1]
for i in t:
    if i == 'L':
        tl_move += [-1]
        tr_move += [0]
        tu_move += [0]
        td_move += [0]
    elif i == 'R':
        tl_move += [0]
        tr_move += [1]
        tu_move += [0]
        td_move += [0]
    elif i == 'U':
        tl_move += [0]
        tr_move += [0]
        tu_move += [-1]
        td_move += [0]
    elif i == 'D':
        tl_move += [0]
        tr_move += [0]
        tu_move += [0]
        td_move += [1]
ru, rd, cr, cl = sr, sr, sc, sc
for i in range(n):
    ru += su_move[i]
    if ru < 1:
        ans = 'NO'
        break
    ru += td_move[i]
    if ru > h:
        ru = h
    rd += sd_move[i]
    if rd > h:
        ans = 'NO'
        break
    rd += tu_move[i]
    if rd < 1:
        rd = 1
    cr += sr_move[i]
    if cr > w:
        ans = 'NO'
        break
    cr += tl_move[i]
    if cr < 1:
        cr = 1
    cl += sl_move[i]
    if cl < 1:
        ans = 'NO'
        break
    cl += tr_move[i]
    if cl > w:
        cl = w
    ans = 'YES'
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

W, H = map(int, input().split(' '))
A = []
for j in range(H):
  a = input()
  A.append(a)

B = [[-1] * W for j in range(H)]

dq = deque()
for j in range(H):
  for i in range(W):
    if A[j][i] == '#':
      dq.append((j, i, 0))
      B[j][i] = 0

while(True):
  if len(dq) <= 0:
    break
  q = dq.popleft()
  y = q[0]
  x = q[1]
  d = q[2]
  
  if y-1 >= 0 and B[y-1][x] == -1:
    dq.append((y-1, x, d+1))
    B[y-1][x] = d+1
  if y+1 < H and B[y+1][x] == -1:
    dq.append((y+1, x, d+1))
    B[y+1][x] = d+1
  if x-1 >= 0 and B[y][x-1] == -1:
    dq.append((y, x-1, d+1))
    B[y][x-1] = d+1
  if x+1 < W and B[y][x+1] == -1:
    dq.append((y, x+1, d+1))
    B[y][x+1] = d+1

maxval = 0
for j in range(H):
  for i in range(W):
    if B[j][i] > maxval:
      maxval = B[j][i]
print(maxval)

Submission status: Runtime Error

Solution code:
import queue

H, W = map(int, input().split())
m = [""]*H
q = queue.Queue()

for h in range(H):
    m[i] = input()
    for w in range(W):
        if m[h][w] == '#':
            m[h][w] = 1
            q.put([h, w])

while not q.empty():
    h, w = q.get()
    if h - 1 >= 0 and m[h - 1][w] == '.':
        m[h - 1][w] = m[h][w] + 1
        q.put([h-1, w])
    if w - 1 >= 0 and m[h][w - 1] == '.':
        m[h][w - 1] = m[h][w] + 1
        q.put([h, w-1])
    if h + 1 < H and m[h + 1][w] == '.':
        m[h + 1][w] = m[h][w] + 1
        q.put([h+1, w])
    if w + 1 < W and m[h][w + 1] == '.':
        m[h][w + 1] = m[h][w] + 1
        q.put([h, w+1])

mx = 0
for h in range(H):
    mx = max(max(m[h]), mx)
print(mx-1)

Submission status: Runtime Error

Solution code:
def print_field( field):
	for i in field:
		print(''. join(list(map(str,i))))
	print()

debug=False

q=[]
h,w=map(int,input().split())
field=[[-1 for i in range( w)] for j in range( h)]
for i in range( h):
	buf=input()
	for j,c in enumerate(buf):
		if c=='#':
			field[ i][j]=0
			q. append([ i,j])

max_distance= 0
ddx,ddy=[1,-1,0,0],[0,0,1,-1]
while len(q):
	tx,ty=q.pop(0)
	distance= field[ tx][ty]+1
	for dx,dy in zip(ddx,ddy):
		x,y=tx+dx,ty+dy
		if 0<=x<h and 0<=y<w and field[x][y]==-1:
			field[x][y]= distance
			max_distance= max(distance,max_distance)
			q.append([x,y])
			if debug:
				print(f'x:{x} y:{y}')
				print_field(field)
			
			
print(max_distance)
if debug:
	print_field(field)

Submission status: Runtime Error

Solution code:
def print_field( field):
	for i in field:
		print(''. join(list(map(str,i))))
	print()

debug=False

q=[]
h,w=map(int,input().split())
field=[[-1 for i in range( w)] for j in range( h)]
for i in range( h):
	buf=input()
	for j,c in enumerate(buf):
		if c=='#':
			field[ i][j]=0
			q. append([ i,j])

max_distance= 0
ddx,ddy=[1,-1,0,0],[0,0,1,-1]
while len(q):
	tx,ty=q.pop(0)
	distance= field[ tx][ty]+1
	for dx,dy in zip(ddx,ddy):
		x,y=tx+dx,ty+dy
		if 0<=x<h and 0<=y<w and field[x][y]==-1:
			field[x][y]= distance
			max_distance= max(distance,max_distance)
			q.append([x,y])
			if debug:
				print(f'x:{x} y:{y}')
				print_field(field)
			
			
print(max_distance)
#print_field(field)

Submission status: Runtime Error

Solution code:
def print_field( field):
	for i in field:
		print(''. join(list(map(str,i))))
	print()

q=[]
h,w=map(int,input().split())
field=[[-1 for i in range( w)] for j in range( h)]
for i in range( h):
	buf=input()
	for j,c in enumerate(buf):
		if c=='#':
			field[ i][j]=0
			q. append([ i,j])

max_distance= 0
ddx,ddy=[1,-1,0,0],[0,0,1,-1]
while len(q):
	tx,ty=q.pop(0)
	distance= field[ tx][ty]+1
	for dx,dy in zip(ddx,ddy):
		x,y=tx+dx,ty+dy
		if 0<=x<w and 0<=y<h and field[x][y]==-1:
			field[x][y]= distance
			max_distance= max(distance,max_distance)
			q.append([x,y])
			#print(f'x:{x} y:{y}')
			#print_field(field)
			
			
print(max_distance)
#print_field(field)

Submission status: Runtime Error

Solution code:
from queue import Queue

def print_field( field):
	for i in field:
		print(''. join(list(map(str,i))))
	print()

q=Queue()
h,w=map(int,input().split())
field=[[-1 for i in range( w)] for j in range( h)]
for i in range( h):
	buf=input()
	for j,c in enumerate(buf):
		if c=='#':
			field[ i][j]=0
			q.put([i,j])

max_distance= 0
ddx,ddy=[1,-1,0,0],[0,0,1,-1]
while q.qsize():
	tx,ty=q.get()
	distance= field[ tx][ty]+1
	for dx,dy in zip(ddx,ddy):
		x,y=tx+dx,ty+dy
		if 0<=x<w and 0<=y<h and field[x][y]==-1:
			field[x][y]= distance
			max_distance= max(distance,max_distance)
			q.put([x,y])
			#print(f'x:{x} y:{y}')
			#print_field(field)
			
			
print(max_distance)
#print_field(field)

Submission status: Runtime Error

Solution code:
from queue import Queue
q=Queue()
h,w=map(int,input().split())
field=[[-1 for i in range( w)] for j in range( h)]
for i in range( h):
	buf=input()
	for j,c in enumerate(buf):
		if c=='#':
			field[ i][j]=0
			q.put([i,j])

max_distance= 0
ddx,ddy=[1,-1,0,0],[0,0,1,-1]
while q:
	tx,ty=q.get()
	distance= field[ i][j]+1
	for dx,dy in zip(ddx,ddy):
		x,y=tx+dx,ty+dy
		if 0<=x<w and 0<=y<h and field[x][y]==-1:
			field[x][y]= distance
			max_distance= max(distance,max_distance)
			q.put([x,y])

print(max_distance)

Submission status: Runtime Error

Solution code:
from functools import reduce
from bisect import bisect_left, insort
from collections import deque

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

lines = [input() for s in range(H)]
distance = [k for k in range(H*W) if lines[k//W][k%W] == '#']
nb = len(distance)
if(nb == H*W):
  print(0)
  exit()
number = 0
new_distance = distance[:]
while(nb <= H*W):
  tmp = []
  for k in new_distance:
    index_l = bisect_left(distance, k-1)
    index_r = bisect_left(distance, k+1)
    index_t = bisect_left(distance, k-W)
    index_b = bisect_left(distance, k+W)
    if k%W-1 >= 0:
      if distance[index_l] != k-1:
        tmp.append(k-1)
#        print('l', index_l)
    if k%W+1 <= W-1:
      if distance[index_r] != k+1:
        tmp.append(k+1)
#        print('r', index_r)
    if k-W >= 0:
      if distance[index_t] != k-W:
        tmp.append(k-W)
#        print('t', index_t)
    if k+W <= (H-1)*W:
      if distance[index_b] != k-W:
        tmp.append(k+W)
#        print('b', index_b)
  tmp = list(set(tmp))
  new_distance = tmp[:]
  for k in tmp:
    insort(distance, k)
  number += 1
  nb = len(distance)
#  print(nb)
print(number)

Submission status: Runtime Error

Solution code:
from functools import reduce
from bisect import bisect_left, insort
from collections import deque

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

lines = [input() for s in range(H)]
distance = [k for k in range(H*W) if lines[k//W][k%W] == '#']
nb = len(distance)
if(nb == H*W):
  print(0)
  exit()
number = 0
new_distance = distance[:]
while(nb <= H*W):
  tmp = []
  for k in new_distance:
    index_l = bisect_left(distance, k-1)
    index_r = bisect_left(distance, k+1)
    index_t = bisect_left(distance, k-W)
    index_b = bisect_left(distance, k+W)
    if k%W-1 >= 0:
      if distance[index_l] != k-1:
        tmp.append(k-1)
        print('l', index_l)
    if k%W+1 <= W-1:
      if distance[index_r] != k+1:
        tmp.append(k+1)
        print('r', index_r)
    if k-W >= 0:
      if distance[index_t] != k-W:
        tmp.append(k-W)
        print('t', index_t)
    if k+W <= (H-1)*W:
      if distance[index_b] != k-W:
        tmp.append(k+W)
        print('b', index_b)
  tmp = list(set(tmp))
  new_distance = tmp[:]
  for k in tmp:
    insort(distance, k)
  number += 1
  nb = len(distance)
  print(nb)
print(number)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
A - Darker and Darker
https://atcoder.jp/contests/agc033/tasks/agc033_a

"""
import sys
from typing import List


def solve(H, W, grid):
    seen = set()
    stack = []
    for y, row in enumerate(grid):
        for x, ch in enumerate(row):
            if ch == '#':
                stack.append((x, y))
                seen.add((x, y))
    ans = 0
    while len(seen) < H*W:
        darken = []
        while stack:
            sx, sy = stack.pop()
            for nx, ny in ((sx, sy-1), (sx, sy+1), (sx-1, sy), (sx+1, sy)):
                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] == '.' and (nx, ny) not in seen:
                    darken.append((nx, ny))
                    grid[ny][nx] = '#'
                    seen.add((nx, ny))
        ans += 1
        stack = darken
    return ans


def main(args):
    H, W = map(int, input().split())
    grid = []
    for _ in range(H):
        grid.append(list(input()))
    ans = solve(H, W, grid)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
from scipy.ndimage import distance_transform_cdt


H,W = map(int, input().split())
a = [list(input()) for _ in range(H)]
n = [[1 if x == "." else 0 for x in row] for row in a]

cnt = distance_transform_cdt(n, metric='taxicab')
print(cnt.max())

Submission status: Runtime Error

Solution code:
import numpy as np
from queue import Queue
 
H, W = map(int,input().split())
arr = np.zeros(shape=(H, W))
p = Queue()
q = Queue()
 
for i in range(H):
    A = input()
    for j in range(W):
        if A[j] == '#':
            arr[i, j] = 1
            p.put((i, j))
 
arr_temp = arr.copy()
cnt = 0
while True:
    if np.count_nonzero(arr) == H*W:
        break
    cnt += 1
    while not p.empty():
        i, j = p.get()
        if i != 0 and i != H-1 and j != 0 and j != W-1:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif i == 0 and j != 0 and j != W-1:
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif i == H-1 and j != 0 and j != W-1:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif j == 0 and i != 0 and i != H-1:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif j == W-1 and i != 0 and i != H-1:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
        elif i == 0 and j == 0:
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif i == 0 and j == W-1:
            if arr_temp[i+1, j] == 0:
                arr_temp[i+1, j] = 1
                q.put((i+1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
        elif i == H-1 and j == 0:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i, j+1] == 0:
                arr_temp[i, j+1] = 1
                q.put((i, j+1))
        elif i == H-1 and j == W-1:
            if arr_temp[i-1, j] == 0:
                arr_temp[i-1, j] = 1
                q.put((i-1, j))
            if arr_temp[i, j-1] == 0:
                arr_temp[i, j-1] = 1
                q.put((i, j-1))
    while not q.empty():
        p.put(q.get())
    arr = arr_temp.copy()
 
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque


h, w = map(int, input().split())
hs, ws = set(range(h)), set(range(w))
a = []
b = []
d = deque([])
memo = [[None] * w for j in hs]
move = [[1, 0], [0, 1], [-1, 0], [0, -1]]
a = tuple(input() for _ in hs)
b = [[i, j] for i in hs for j in ws if a[i][j] == '#']
for i, j in b:
    d.append([i, j])
    memo[i][j] = 0


def d_list_update():
    dh, dw = d.popleft()
    for i, j in move:
        if not(memo[dh + i][dw + j]):
            if (0 <= dh + i < h) and (0 <= dw + j < w):
                memo[dh + i][dw + j] = memo[dh][dw] + 1
                d.append([dh + i, dw + j])
    return d


while d:
    d_list_update()
ans = max([max(e) for e in memo])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque


h, w = map(int, input().split())
hs, ws = set(range(h)), set(range(w))
a = []
d = deque([])
memo = [[None] * w for j in hs]
move = [[1, 0], [0, 1], [-1, 0], [0, -1]]
for i in hs:
    a.append(list(input()))
for i in hs:
    for j in ws:
        if a[i][j] == '#':
            d.append([i, j])
            memo[i][j] = 0


def d_list_update(d, hmax, wmax):
    dh, dw = d.popleft()
    for i, j in move:
        if (0 <= dh + i < hmax) and (0 <= dw + j < wmax) and not(memo[dh + i][dw + j]):
            memo[dh + i][dw + j] = memo[dh][dw] + 1
            d.append([dh + i, dw + j])
    return d


hw = set(range(h*w))
for i in hw:
    d = d_list_update(d, h, w)
    if not d:
        break
ans = max([max(e) for e in memo])
print(ans)