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:
r, c = map(int, input().strip().split())
maze = [[0] * (c + 2)] + [[0] + [0 if c == "#" else 2001 for c in input().strip()] + [0] for _ in range(r)] + [[0] * (c + 2)]
queue = [(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 = []

    while queue:
        depth += 1
        for pos in queue:
            posr, posc = pos
            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.clear()

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

Submission status: Runtime Error

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    
    h, w = map(int, input().split())
    a = [input() for i in range(h)]
    queue = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                queue.append([i, j])

    visited = [[-1 for i in range(w)] for j in range(h)]
    ans = 0
    nqueue = []
    while queue:
        x, y = queue.pop(0)
        visited[x][y] = 1
        for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            nx = x + i
            ny = y + j
            if nx >= 0 and ny >= 0 and nx < w and ny < h:
                if visited[nx][ny] == -1:
                    nqueue.append([nx, ny])
                    visited[nx][ny] = 1
        
        if len(queue) == 0:
            queue = nqueue
            nqueue = []
            ans += 1
    print(ans-1)


        
                





if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7

import bz2

def main():
    
    h, w = map(int, input().split())
    a = [input() for i in range(h)]
    queue = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                queue.append([i, j])

    visited = [[-1 for i in range(w)] for j in range(h)]
    ans = 0
    nqueue = []
    while queue:
        x, y = queue.pop(0)
        visited[x][y] = 1
        for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            nx = x + i
            ny = y + j
            if nx >= 0 and ny >= 0 and nx < w and ny < h:
                if visited[nx][ny] == -1:
                    nqueue.append([nx, ny])
                    visited[nx][ny] = 1
        
        if len(queue) == 0:
            queue = nqueue
            nqueue = []
            ans += 1
    print(ans-1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy
h,w=map(int,input().split())
a=[list("."*(w+2))]+[list("."+input()+".") for i in range(h)]+[list("."*(w+2))]
b=copy.deepcopy(a) 
#print(a,b)
for k in range(h+w-2):
    for i in range(1,h+1):
        for j in range(1,w+1):
            if a[i][j]=='#':
                b[i-1][j]='#'
                b[i+1][j]='#'
                b[i][j-1]='#'
                b[i][j+1]='#'
    #print(a,b)
    if a==b:
        break
    else:
        a=copy.deepcopy(b) 
print(k-1)

Submission status: Runtime Error

Solution code:
import collections
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]

dis = [[-1]*W for i in range(H)]
black = set()
white = set()

start = [-1, -1]

for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            start[0] = i
            start[1] = j

            break
    if start[0] != -1:
        break


dp = [[H+W]*W for i in range(H)]


q = collections.deque([start])
dp[start[0]][start[1]] = 0
count = 0
while(q):
    x, y = q.popleft()
    if x > 0:
        if dp[x-1][y] > dp[x][y]+1:
            if A[x-1][y] == ".":
                dp[x-1][y] = dp[x][y]+1
            else:
                dp[x-1][y] = 0
            q.append([x-1, y])
    if x < W-1:
        if dp[x+1][y] > dp[x][y]+1:
            if A[x+1][y] == ".":
                dp[x+1][y] = dp[x][y]+1
            else:
                dp[x+1][y] = 0
            q.append([x+1, y])
    if y > 0:
        if dp[x][y-1] > dp[x][y]+1:
            if A[x][y-1] == ".":
                dp[x][y-1] = dp[x][y]+1
            else:
                dp[x][y-1] = 0
            q.append([x, y-1])
    if y < H-1:
        if dp[x][y+1] > dp[x][y]+1:
            if A[x][y+1] == ".":
                dp[x][y+1] = dp[x][y]+1
            else:
                dp[x][y+1] = 0
            q.append([x, y+1])
ans = 0
for i in range(H):
    ans = max(ans, max(dp[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import collections
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]

dis = [[-1]*W for i in range(H)]
black = set()
white = set()

start = [-1, -1]

for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            start[0] = i
            start[1] = j

            break
    if start[0] != -1:
        break


dp = [[10**9]*W for i in range(H)]


q = collections.deque([start])
dp[start[0]][start[1]] = 0
count = 0
while(q):
    x, y = q.popleft()
    if x > 0:
        if dp[x-1][y] > dp[x][y]+1:
            if A[x-1][y] == ".":
                dp[x-1][y] = dp[x][y]+1
            else:
                dp[x-1][y] = 0
            q.append([x-1, y])
    if x < W-1:
        if dp[x+1][y] > dp[x][y]+1:
            if A[x+1][y] == ".":
                dp[x+1][y] = dp[x][y]+1
            else:
                dp[x+1][y] = 0
            q.append([x+1, y])
    if y > 0:
        if dp[x][y-1] > dp[x][y]+1:
            if A[x][y-1] == ".":
                dp[x][y-1] = dp[x][y]+1
            else:
                dp[x][y-1] = 0
            q.append([x, y-1])
    if y < H-1:
        if dp[x][y+1] > dp[x][y]+1:
            if A[x][y+1] == ".":
                dp[x][y+1] = dp[x][y]+1
            else:
                dp[x][y+1] = 0
            q.append([x, y+1])
ans = 0
for i in range(H):
    ans = max(ans, max(dp[i]))
print(ans)

Submission status: Runtime Error

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

dis = [[-1]*W for i in range(H)]
black = set()
white = set()

start = [-1, -1]

for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            start[0] = i
            start[1] = j

            break
    if start[0] != -1:
        break


dp = [[10**9]*W for i in range(H)]


q = collections.deque([start])
dp[start[0]][start[1]] = 0
while(q):
    x, y = q.popleft()
    if x > 0:
        if dp[x-1][y] > dp[x][y]+1:
            if A[x-1][y] == ".":
                dp[x-1][y] = dp[x][y]+1
            else:
                dp[x-1][y] = 0
            q.append([x-1, y])
    if x < W-1:
        if dp[x+1][y] > dp[x][y]+1:
            if A[x+1][y] == ".":
                dp[x+1][y] = dp[x][y]+1
            else:
                dp[x+1][y] = 0
            q.append([x+1, y])
    if y > 0:
        if dp[x][y-1] > dp[x][y]+1:
            if A[x][y-1] == ".":
                dp[x][y-1] = dp[x][y]+1
            else:
                dp[x][y-1] = 0
            q.append([x, y-1])
    if y < H-1:
        if dp[x][y+1] > dp[x][y]+1:
            if A[x][y+1] == ".":
                dp[x][y+1] = dp[x][y]+1
            else:
                dp[x][y+1] = 0
            q.append([x, y+1])
ans = 0
for i in range(H):
    ans = max(ans, max(dp[i]))

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    h,w=map(int,input().split())
    maze=[list(input()) for i in range(h)]
    d=[[float("inf")]*w for i in range(h)]
    que=deque([])
    dx=[1,0,-1,0]
    dy=[0,1,0,-1]
    for i in range(h):
        for j in range(w):
            if maze[i][j]=="#":
                que.append((i,j))           
                d[i][j]=0
    while que:
        p=que.popleft()
        for i in range(4):
            nx=p[0]+dx[i]
            ny=p[1]+dy[i]
            if 0<=nx and nx<h and 0<=ny and ny<w and maze[nx][ny]!="#" and d[nx][ny]==float("inf"):
                d[nx][ny]=d[p[0]][p[1]]+1
                que.append((nx,ny))  
    ans=0
    for i in range(h):
        ans=max(ans,d[i])
    return ans
if __name__=='__main__':
    print(main())

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    h,w=map(int,input().split())
    maze=[list(input()) for i in range(h)]
    d=[[float("inf")]*w for i in range(h)]
    que=deque([])
    dx=[1,0,-1,0]
    dy=[0,1,0,-1]
    for i in range(h):
        for j in range(w):
            if maze[i][j]=="#":
                que.append((i,j))           
                d[i][j]=0
    while que:
        p=que.popleft()
        for i in range(4):
            nx=p[0]+dx[i]
            ny=p[1]+dy[i]
            if 0<=nx and nx<h and 0<=ny and ny<w and maze[nx][ny]!="#" and d[nx][ny]==float("inf"):
                d[nx][ny]=d[p[0]][p[1]]+1
                que.append((nx,ny))  
    ans=0
    for i in range(h):
        ans=max(ans,d[i])
if __name__=='__main__':
    print(main())

Submission status: Runtime Error

Solution code:
from collections import deque

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

# メモ配列
INF = 10**7
# memo = [[INF]*w for i in range(h)]

# 探索用ベクトル
vx = (0, -1, 0, 1)
vy = (-1, 0, 1, 0)

# 幅探索用キューの準備
# 白マスをカウント
q = deque()
bcnt = 0
for i, vi in enumerate(p):
    for j, vj in enumerate(vi):
        if vj == '#':
            q.appendleft((j, i))
            bcnt+=1
            
qans = deque()
            
def bfs(q):
    for i in range(INF):
        # キューが空なら終了
        if len(q) == 0:
            return qans

        # キューから一つ取り出す
        x, y = map(int, q.pop())
#         print('xy', x, y)

        # 4方向のいずれかに白があれば、黒にする
        for j in range(4):
            nx, ny = x+vx[j], y+vy[j]

            # エリア外 or 黒色なら探索しない
            if nx < 0 or w <= nx or ny < 0 or h <= ny or p[ny][nx] == '#':
                continue

            p[ny][nx] = '#'
            memo[ny][nx] = 1
            qans.appendleft((nx, ny))

def printmap(a):
    for i in a:
        print(i)
        
# print(q)
# printmap(p)
for i in range(INF):
    qans = deque()
    q = bfs(q)
#     print(q)
#     printmap(p)
    # キューが空なら終了
    if len(q) == 0:
        ans = i
        break
    
print(ans)
# print(q)

Submission status: Runtime Error

Solution code:
h,w=map(int,input())
grid=[None]*h
INF=10**6
stack=[]
for i in range(h):
  s=list(input())
  for j in range(len(s)):
    if s[j]=="#":
      stack.append([i,j,0])
    s[j]=INF
  grid[i]=s
      
while stack:
  v=stack.pop()
  y=v[0]
  x=v[1]
  p=v[2]
  if p<grid[y][x]:
    grid[y][x]=p
  else:
    continue
  if y-1>=0:
    stack.append([y-1,x,p+1])
  if y+1<h:
    stack.append([y+1,x,p+1])
  if x-1>=0:
    stack.append([y,x-1,p+1])
  if x+1<w:
    stack.append([y,x+1,p+1])
    
ans=0
for i in range(len(grid)):
  ans=max(ans,max(grid[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
H, W = map(int, input().split())
gridgraph = []
for line in sys.stdin.readlines():
    line = line.rstrip()
    gridgraph.append(list(line))

visited = [[False]*W for _ in range(H)]
ans = [[1e+9]*W for _ in range(H)]
# for i in range(H):
#     for j in range(W):
#         if gridgraph[i][j] == "#":
#             q.append((i, j))
#             visited[i][j] = True
#             ans[i][j] = 0
a = [[j == "#" for j in input()] for _ in range(H)]
que = deque([(i, j) for i in range(H) for j in range(W) if a[i][j]])
# for a, b in q:
#     print(a, b)
# exit()
while len(q):
    cy, cx = q.popleft()
    # print(cy, cx)
    for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        if not (cy+dy >= 0 and H-1 >= cy+dy and cx+dx >= 0 and W-1 >= cx+dx):
            continue
        if visited[cy+dy][cx+dx]:
            continue
        visited[cy+dy][cx+dx] = True
        ans[cy+dy][cx+dx] = min(ans[cy+dy][cx+dx], ans[cy][cx]+1)
        q.append((cy+dy, cx+dx))
    # print(ans)
    # exit()

min_value = 0
for a in ans:
    min_value = max(min_value, max(a))
print(min_value)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
H, W = map(int, input().split())
gridgraph = []
for line in sys.stdin.readlines():
    line = line.rstrip()
    gridgraph.append(list(line))

q = deque()
visited = [[False]*W for _ in range(H)]
ans = [[1e+9]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if gridgraph[i][j] == "#":
            q.append((i, j))
            visited[i][j] = True
            ans[i][j] = 0
# for a, b in q:
#     print(a, b)
# exit()
while len(q):
    cy, cx = q.popleft()
    # print(cy, cx)
    for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        if not (cy+dy >= 0 and H-1 >= cy+dy and cx+dx >= 0 and W-1 >= cx+dx):
            continue
        if visited[cy+dy][cx+dx]:
            continue
        visited[cy+dy][cx+dx] = True
        ans[cy+dy][cx+dx] = min(ans[cy+dy][cx+dx], ans[cy][cx]+1)
        q.put((cy+dy, cx+dx))
    # print(ans)
    # exit()

min_value = 0
for a in ans:
    min_value = max(min_value, max(a))
print(min_value)

Submission status: Runtime Error

Solution code:
from collections import deque
import numpy as np


def solve():
    H, W = map(int, input().split())
    maze = ['' for _ in range(H)]
    for i in range(H):
        maze[i] = list(input())
    maze = np.array(maze)
    black_l = list(zip(*np.where(maze=='#')))
    que = deque([])
    for x, y in black_l:
        que.append([y, x])
    ans = 0
    
    while len(que) > 0:
        a = len(que)
        for _ in range(a):
            target = que.pop()
            if target[0] != 0:
                if maze[target[0]-1][target[1]] == '.':
                    maze[target[0]-1][target[1]] = '#'
                    que.append([target[0]-1, target[1]])
            if target[0] != H - 1:
                if maze[target[0]+1][target[1]] == '.':
                    maze[target[0]+1][target[1]] = '#'
                    que.append([target[0]+1, target[1]])
            if target[1] != 0:
                if maze[target[0]][target[1]-1] == '.':
                    maze[target[0]][target[1]-1] = '#'
                    que.append([target[0], target[1]-1])
            if target[1] != W - 1:
                if maze[target[0]][target[1]+1] == '.':
                    maze[target[0]][target[1]+1] = '#'
                    que.append([target[0], target[1]+1])
        ans += 1
    
    print(ans-1)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
from collections import deque
import numpy as np


def solve():
    H, W = map(int, input().split())
    maze = ['' for _ in range(H)]
    for i in range(H):
        maze[i] = list(input())
    maze = np.array(maze)
    black_l = list(zip(*np.where(maze=='#')))
    que = deque([])
    for x, y in black_l:
        que.append([y, x])
    ans = 0
    
    while len(que) > 0:
        a = len(que)
        for _ in range(a):
            target = que.pop()
            if target[0] != 0 and maze[target[0]-1][target[1]] == '.':
                maze[target[0]-1][target[1]] = '#'
                que.append([target[0]-1, target[1]])
            if target[0] != H - 1 and maze[target[0]+1][target[1]] == '.':
                maze[target[0]+1][target[1]] = '#'
                que.append([target[0]+1, target[1]])
            if target[1] != 0 and maze[target[0]][target[1]-1] == '.':
                maze[target[0]][target[1]-1] = '#'
                que.append([target[0], target[1]-1])
            if target[1] != W - 1 and maze[target[0]][target[1]+1] == '.':
                maze[target[0]][target[1]+1] = '#'
                que.append([target[0], target[1]+1])
        ans += 1
    
    print(ans-1)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


def bfs():
    q = deque()
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                q.append((i, j))
                d[j][i] = 0

    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    while q:
        ux, uy = q.popleft()
        for dx, dy in dxy:
            vx = ux + dx
            vy = uy + dy
            if 0 <= vx <= w - 1 and 0 <= vy <= h - 1 and a[vy][vx] == "." and d[vy][vx] == -1:
                d[vy][vx] = d[uy][ux] + 1
                q.append((vx, vy))


bfs()

ans = 0
for row in d:
    for e in row:
        ans = max(ans, e)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


def bfs():
    q = deque()
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                q.append((j, i))
                d[j][i] = 0

    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    while q:
        ux, uy = q.popleft()
        for dx, dy in dxy:
            vx = ux + dx
            vy = uy + dy
            if 0 <= vx <= w - 1 and 0 <= vy <= h - 1 and a[vy][vx] == "." and d[vy][vx] == -1:
                d[vy][vx] = d[uy][ux] + 1
                q.append((vx, vy))


bfs()

ans = 0
for row in d:
    for e in row:
        ans = max(ans, e)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


def bfs():
    s = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                s.append([i, j])

    q = deque()
    for sx, sy in s:
        q.append([sx, sy])
        d[sy][sx] = 0
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    while q:
        ux, uy = q.popleft()
        for dx, dy in dxy:
            vx = ux + dx
            vy = uy + dy
            if 0 <= vx <= w - 1 and 0 <= vy <= h - 1 and a[vy][vx] == "." and d[vy][vx] == -1:
                d[vy][vx] = d[uy][ux] + 1
                q.append([vx, vy])


bfs()

ans = 0
for row in d:
    for e in row:
        ans = max(ans, e)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


def bfs():
    s = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                s.append([i, j])

    q = deque()
    for sx, sy in s:
        q.append([sx, sy])
        d[sy][sx] = 0
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    while q:
        ux, uy = q.popleft()
        for dx, dy in dxy:
            vx = ux + dx
            vy = uy + dy
            if 0 <= vx <= w - 1 and 0 <= vy <= h - 1 and \
                    a[vy][vx] == "." and d[vy][vx] == -1:
                d[vy][vx] = d[uy][ux] + 1
                q.append([vx, vy])


bfs()

ans = 0
for row in d:
    for e in row:
        ans = max(ans, e)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


def bfs(s):
    q = deque()
    for sx, sy in s:
        q.append([sx, sy])
        d[sy][sx] = 0
    dxy = [[1, 0], [-1, 0], [0, 1], [0, -1]]

    while q:
        ux, uy = q.popleft()
        for dx, dy in dxy:
            vx = ux + dx
            vy = uy + dy
            if 0 <= vx <= w - 1 and 0 <= vy <= h - 1 and\
                    a[vy][vx] == "." and d[vy][vx] == -1:
                d[vy][vx] = d[uy][ux] + 1
                q.append([vx, vy])


s = []
for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            s.append([i, j])

bfs(s)

ans = 0
for row in d:
    for e in row:
        ans = max(ans, e)

print(ans)

Submission status: Runtime Error

Solution code:

import sys
from collections import deque

# input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


def main():
    BLACK = '#'
    WHITE = '.'
    H, W = map(int, input().split())
    graph = [None] * H
    # w_score = [[0 for i in range(W)] for i in range(H)]
    # h_score = [[0 for i in range(W)] for i in range(H)]
    options = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    visited = [[False for i in range(W)] for i in range(H)]
    initial = []

    for i in range(H):
        graph[i] = list(input())

        for j in range(W):
            if graph[i][j] == BLACK:
                initial.append((i, j))
                visited[i][j] = True

    q = deque()
    q.append(initial)
    ans = 0
    while len(q) != 0:
        n = q.popleft()
        # print(ans, n)

        items = set()
        for p in n:
            for d in options:
                i = p[0] + d[0]
                j = p[1] + d[1]

                if i >= 0 and i < W and j >= 0 and j < H and not visited[i][j] and graph[i][j] == WHITE:
                    visited[i][j] = True
                    graph[i][j] = BLACK
                    items.add((i, j))
                # else:
                #     print(i, j)

        if len(items) > 0:
            ans += 1
            q.append(items)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

#####################################


#1:43
h,w = list(map(int, input().split()))
queue=[]
mp=[[-1 for i in range(h)]for j in range(w)]
for i in range(h):
    temp = input()
    for j in range(len(temp)):
        if(temp[j]=='#'):
            queue.append([j,i,0])
            mp[j][i]=0
            
nowq = 0
while(True):
    x,y,times = queue[nowq]
    if(mp[min(x+1,w-1)][y]==-1):
        mp[min(x+1,w-1)][y]=times+1
        queue.append([x+1,y,times+1])
    if(mp[max(x-1,0)][y]==-1):
        mp[max(x-1,0)][y]=times+1
        queue.append([x-1,y,times+1])
    if(mp[x][min(y+1,h-1)]==-1):
        mp[x][min(y+1,h-1)]=times+1
        queue.append([x,y+1,times+1])
    if(mp[x][max(y-1,0)]==-1):
        mp[x][max(y-1,0)]=times+1
        queue.append([x,y-1,times+1])
    nowq+=1
    if(len(queue)==nowq):
        break
ans=0
for i in range(h):
    ans=max(ans,max(mp[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

#####################################


#1:43
h,w = list(map(int, input().split()))
queue=[]
mp=[[-1 for i in range(w)]for j in range(h)]
for i in range(h):
    temp = input()
    for j in range(len(temp)):
        if(temp[j]=='#'):
            queue.append([i,j,0])
            mp[i][j]=0
            
nowq = 0
while(True):
    x,y,times = queue[nowq]
    if(mp[min(x+1,w-1)][y]==-1):
        mp[min(x+1,w-1)][y]=times+1
        queue.append([x+1,y,times+1])
    if(mp[max(x-1,0)][y]==-1):
        mp[max(x-1,0)][y]=times+1
        queue.append([x-1,y,times+1])
    if(mp[x][min(y+1,h-1)]==-1):
        mp[x][min(y+1,h-1)]=times+1
        queue.append([x,y+1,times+1])
    if(mp[x][max(y-1,0)]==-1):
        mp[x][max(y-1,0)]=times+1
        queue.append([x,y-1,times+1])
    nowq+=1
    if(len(queue)==nowq):
        break
ans=0
for i in range(h):
    ans=max(ans,max(mp[i]))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(1000000)
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 

def bfs(sy, sx):
  q = deque([[sy, sx]])
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])

import numpy as np
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used = [[0]*w for _ in range(h)]
      bfs(i, j)

print(np.max(np.array(cnt)))

Submission status: Runtime Error

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    a = [["."] * w for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    fq = collections.defaultdict(list)
    for i in range(h):
        s = str(readline().rstrip().decode('utf-8'))
        for j in range(w):
            if s[j] == "#":
                a[i][j] = "#"
                ql.append([0, i, j])
                fq[i, j]

    mt = 0
    while True:
        if len(ql) != 0:
            cost, x, y = ql.popleft()
            mt = max(mt, cost)
            for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if (xv, yv) not in fq and a[xv][yv] == ".":
                        ql.append([cost + 1, xv, yv])
                        fq[xv, yv]
        else:
            break
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    a = [["."] * w for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    fq = collections.defaultdict(list)
    for i in range(h):
        s = str(readline().rstrip().decode('utf-8'))
        for j in range(w):
            if s[i][j] == "#":
                a[i][j] = "#"
                ql.append([0, i, j])
                fq[i, j]

    mt = 0
    while True:
        if len(ql) != 0:
            cost, x, y = ql.popleft()
            mt = max(mt, cost)
            for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if (xv, yv) not in fq and a[xv][yv] == ".":
                        ql.append([cost + 1, xv, yv])
                        fq[xv, yv]
        else:
            break
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
rom collections import deque
import copy

def main():
    h,w = map(int, input().split())
    a = [[j == "#" for j in input()] for _ in range(h)]
    que = deque([(i, j) for i in range(h) for j in range(w) if a[i][j]])

    count = 0
    while True:
        count += 1
        nque = deque()
        while que:
            y,x = que.popleft()
            if x >0 and not a[y][x-1]:
                a[y][x-1] = True
                nque.append([y,x-1])
            if x <w-1 and not a[y][x+1]:
                a[y][x+1] = True
                nque.append([y,x+1])
            if y >0 and not a[y-1][x]:
                a[y-1][x] = True
                nque.append([y-1,x])
            if y < h-1 and not a[y+1][x]:
                a[y+1][x] = True
                nque.append([y+1,x])
        if nque:
            que = copy.copy(nque)
        else:
            break
    print(count-1)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque

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

# 初期の黒('#')の位置からの距離
dist = [[-1]*H for _ in range(W)]

# '#'の位置をキューに追加（＝今回はこれが複数あるので、複数スタートとなる）
black_cells = deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            black_cells.append((h, w))
            dist[h][w] = 0

d = 0
while black_cells:
    # キューとして使うのでpop()ではなくpopleft()
    h, w = black_cells.popleft()  # 黒いセルの座標
    d = dist[h][w]
    for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        new_h = h + dy
        new_w = w + dx
        if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
            continue
        if dist[new_h][new_w] == -1:  # セルが白('.')であるのと同義
            dist[new_h][new_w] = d+1
            black_cells.append((new_h, new_w))

print(d)

Submission status: Runtime Error

Solution code:
from collections import deque
import copy

def main():
    h,w = map(int, input().split())
    a = [[j == "#" for j in input()] for _ in range(h)]
    # dy = [1,-1,0,0]
    # dx = [0,0,1,-1]
    que = deque([(i, j) for i in range(h) for j in range(w) if a[i][j]])

    count = 0
    while True:
        count += 1
        nque = deque()
        while que:
            y,x = que.popleft()
            if x >0 and not a[y][x-1]:
                a[y][x-1] = True
                nque.append([y,x-1])
            if x <w-1 and not field[y][x+1]:
                a[y][x+1] = True
                nque.append([y,x+1])
            if y >0 and not a[y-1][x]:
                a[y-1][x] = True
                nque.append([y-1,x])
            if y < h-1 and not a[y+1][x]:
                a[y+1][x] = True
                nque.append([y+1,x])
            # for i in range(4):
            #     nh = h + dy[i]
            #     nw = w + dx[i]
            #     if (0<=nh<H) and (0<=nw<W):
            #         if field[nh][nw] == False:
            #             field[nh][nw] = True
            #             nqueue.append((nh,nw))
        if nque:
            que = copy.copy(nque)
        else:
            break
    print(count-1)
if __name__ == '__main__':
    main()