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

H,W=map(int,input().split())
grid=[input() for _ in range(H)]
dist=[[-1]*W for _ in range(H)]

M=deque()
for i in range(W):
    for j in range(H):
        if grid[i][j]=="#":
            M.append((i,j))
            dist[i][j]=0

def bfs(M,dist):
    d=0
    while M:
        x,y=M.popleft()
        d=dist[x][y]
        for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
            new_h=x+dx
            new_w=y+dy
            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
                M.append((new_h,new_w))
    return d

d=bfs(M,dist)
print(d)

Submission status: Runtime Error

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import deque

H, W = map(int,input().split())
sth, stw = 0, 0
glh, glw = H-1, W-1

INF = -1
Gmap = [list(input()) for _ in range(H)]
Dist = [[INF]*W for _ in range(H)]
direc = {(1,0), (-1,0), (0,1), (0,-1)}
next_q = deque([])

def dfs_settings():
    #複数
    for h in range(H):
        for w in range(W):
            if Gmap[h][w]=='#':
                next_q.append([h,w])
                Dist[h][w]=0

def dfs():
    ret = 0
    while len(next_q)!=0:
        #スタック取り出し(先頭)幅優先
        h,w = next_q.popleft()
        hs, ws = h + 0 , w + 1
        if Gmap[hs][ws]=='.' and Dist[hs][ws]==INF and ws<W:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h + 1 , w + 0
        if Gmap[hs][ws]=='.' and Dist[hs][ws]==INF and hs<H:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h +0 , w - 1
        if Gmap[hs][ws]=='.' and Dist[hs][ws]==INF and ws>=0:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
        hs, ws = h - 1 , w + 0
        if Gmap[hs][ws]=='.' and Dist[hs][ws]==INF and hs>=0:
            next_q.append([hs,ws])
            Dist[hs][ws] = Dist[h][w] + 1
            ret = max(ret, Dist[hs][ws])
    return ret

def main():
    dfs_settings()
    ret = dfs()
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  if t[i] == 'L' and s[i] == 'R':
    sd['R'] -= 1
  if t[i] == 'R' and s[i] == 'L':
    sd['L'] -= 1
  if t[i] == 'D' and s[i] == 'U':
    sd['U'] -= 1
  if t[i] == 'U' and s[i] == 'D':
    sd['D'] -= 1
  else:
    td[t[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['D'])+sd['U'] > h or min(h,r+td['U'])-sd['D'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  td[t[i]] += 1
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import bisect
    import copy
    import heapq
    import math
    import sys
    from collections import *
    from itertools import accumulate, combinations, permutations, product
    # from math import gcd
    def input():
        return sys.stdin.readline()[:-1]
    def ruiseki(lst):
        return [0]+list(accumulate(lst))
    mod=pow(10,9)+7
    al=[chr(ord('a') + i) for i in range(26)]
    direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

    cnt=0

    itta=[[10**9]*w for i in range(h)]

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

    # print(d)
    # print(itta)

    while d:
        cnt+=1
        for l in range(len(d)):
            nh,nw=d.popleft()
            for i in range(4):
                d1,d2=direction[i]
                if 0<=nh+d1<h and 0<=nw+d2<w:
                    if itta[nh+d1][nw+d2]==10**9:
                        itta[nh+d1][nw+d2]=cnt
                        d.append((nh+d1,nw+d2))
        # print(d)

    # print(itta)
    print(cnt-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

cnt=0

itta=[[float('inf')]*w for i in range(h)]

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

# print(d)
# print(itta)

while d:
    cnt+=1
    for l in range(len(d)):
        nh,nw=d.popleft()
        for i in range(4):
            d1,d2=direction[i]
            if 0<=nh+d1<h and 0<=nw+d2<w:
                if itta[nh+d1][nw+d2]==float('inf'):
                    itta[nh+d1][nw+d2]=cnt
                    aaa([nh+d1,nw+d2])
    # print(d)

# print(itta)
print(cnt-1)

Submission status: Runtime Error

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

arr = ["0" *(w + 2)]
for i in range(h):
    arr.append("0" + input() + "0")
arr.append("0" *(w + 2))

flg = [[-1 for i in range(h+2)] for j in range(w+2)]

ima = []
for i in range(h+2):
    for j in range(w+2):
        if arr[i][j] == "#":
            ima.append([i,j])
            flg[i][j] = 0

while len(ima) > 0:
    tugi = []
    for x,y in ima:
        if arr[x+1][y] == "." and flg[x+1][y] == -1:
            tugi.append([x+1,y])         
            flg[x+1][y] = flg[x][y] + 1
        if arr[x-1][y] == "." and flg[x-1][y] == -1:
            tugi.append([x-1,y])
            flg[x-1][y] = flg[x][y] + 1
        if arr[x][y+1] == "." and flg[x][y+1] == -1:
            tugi.append([x,y+1])
            flg[x][y+1] = flg[x][y] + 1
        if arr[x][y-1] == "." and flg[x][y-1] == -1:
            tugi.append([x,y-1])
            flg[x][y-1] = flg[x][y] + 1
    ima = tugi

ans = -1
for i in flg:
    for j in i:
        if ans < j:
            ans = j
print(ans)

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
import numpy as np
from collections import deque

H,W = map(int,input().split())
B = np.full((H,W),-1,dtype=int)

queue = deque([])

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

while queue:
    I,J = queue.popleft()
    for di,dj in ((-1,0),(1,0),(0,-1),(0,1)):
        i,j = I+di,J+dj
        if 0<=i<H and 0<=j<W:
            if B[i][j]<0:
                B[i][j] = B[I][J]+1
                queue.append((i,j))

print(np.max(B))

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
import numpy as np
from collections import deque

H,W = map(int,input().split())
B = np.full((H,W),-1,dtype=int)

queue = deque([])

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

while queue:
    I,J = queue.popleft()
    for i,j in ((I-1,J),(I+1,J),(I,J-1),(I,J+1)):
        if 0<=i<H and 0<=j<W:
            if B[i][j]<0:
                B[i][j] = B[I][J]+1
                queue.append((i,j))

print(np.max(B))

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
import numpy as np
from collections import deque

H,W = map(int,input().split())
B = np.full((H,W),-1,dtype=int)

queue = deque([])

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

while queue:
    I,J = queue.popleft()
    for i,j in ((I-1,J),(I+1,J),(I,J-1),(I,J+1)):
        if 0<=i<H and 0<=j<W and B[i][j]<0:
            B[i][j] = B[I][J]+1
            queue.append((i,j))

print(np.max(B))

Submission status: Runtime Error

Solution code:
from collections import deque

h,w=map(int,input().split())
l=[input() for i in range(h)]
flag=[[False]*w for i in range(h)]
cost=[[0]*w for i in range(h)]
todo=deque([])
ans=0
for i in range(h):
    for j in range(w):
        if l[i][j]=="#":
            flag[i][j]=True
            todo.append([i,j])
while len(todo)>0:
    x,y=map(int,todo.popleft())
    if x==w-1:
        pass
    else:
        if(flag[x+1][y]):
            pass
        else:
            flag[x+1][y]=True
            todo.append([x+1,y])
            cost[x+1][y]=cost[x][y]+1
            ans=max(ans,cost[x+1][y])
    if x==0:
        pass
    else:
        if(flag[x-1][y]):
            pass
        else:
            flag[x-1][y]=True
            todo.append([x-1,y])
            cost[x-1][y]=cost[x][y]+1
            ans=max(ans,cost[x-1][y])
    if y==h-1:
        pass
    else:
        if(flag[x][y+1]):
            pass
        else:
            flag[x][y+1]=True
            todo.append([x,y+1])
            cost[x][y+1]=cost[x][y]+1
            ans=max(ans,cost[x][y+1])
    if y==0:
        if(flag[x][y+1]):
            pass
        else:
            flag[x][y-1]=True
            todo.append([x,y-1])
            cost[x][y-1]=cost[x][y]+1
            ans=max(ans,cost[x][y-1])
print(ans)

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
import numpy as np

H,W = map(int,input().split())
h,w = np.arange(H,dtype=int),np.arange(W,dtype=int)
# A = [list(input()) for _ in range(H)]

A = [0]*H
for i in h:
    A[i] = list(input())

queue,new_queue = [],[]

for i in h:
    for j in w:
        if A[i][j]=='#':
            queue.append((i,j))
            
def darker(i,j):
    if (i in h) and (j in w) and A[i][j]=='.':
        A[i][j]='#'
        new_queue.append((i,j))

ans = 0

while True:
    new_queue = []
    for i,j in queue:
        darker(i-1,j)
        darker(i+1,j)
        darker(i,j-1)
        darker(i,j+1)
    if new_queue==[]:
        break
    ans += 1
    queue = new_queue 

print(ans)

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
import numpy as np

H,W = map(int,input().split())
h,w = np.arange(H,dtype=int),np.arange(W,dtype=int)
A = [list(input()) for _ in range(H)]

queue,new_queue = [],[]

for i in h:
    for j in w:
        if A[i][j]=='#':
            queue.append((i,j))
            
def darker(i,j):
    if A[i][j]=='.' and (i in h) and (j in w):
        A[i][j]='#'
        new_queue.append((i,j))

ans = 0

while True:
    new_queue = []
    for i,j in queue:
        darker(i-1,j)
        darker(i+1,j)
        darker(i,j-1)
        darker(i,j+1)
    if new_queue==[]:
        break
    ans += 1
    queue = new_queue 

print(ans)

Submission status: Runtime Error

Solution code:
from collections import  deque
h,w = map(int,input().split())
mp = []
for _ in range(h):
    row = list(input())
    mp.append(row)

q = deque()
dist = [[-1] * w for _ in range(h)]

for col in range(h):
    for row in range(w):
        if mp[col][row] == '#':
            q.append((col,row))
            dist[col][row] = 0
ans = 0
while q:
    now_x,now_y = q.popleft()
    ans = dist[now_x][now_y]
    for x,y in ((1,0), (0,1), (-1,0), (0,-1)):
        next_x = now_x + x
        next_y = now_y + y
        if 0 <= next_x < h and 0 <= next_y < w :
            if dist[next_x][next_y] < 0:
                q.append((next_x, next_y))
                dist[next_x][next_y] = d + 1
                
print(ans)

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    H,W = LI()
    f = [[0] * W for _ in range(H)]
    q = collections.deque()
    for i in range(H):
        for j,x in enumerate(sys.stdin.readline().rstrip()):
            if x == '#':
                f[i][j] = 1
                q.append((i,j,0))
    ans = 0
    while q:
        y,x,c = q.popleft()
        ans = max(ans,c)
        for dy,dx in ((0,1),(1,0),(-1,0),(0,-1)):
            if 0 <= x+dx < W and 0 <= y+dy < H:
                if f[x+dx][y+dy] == 0:
                    f[x+dx][y+dy] = 1
                    q.append((y+dy,x+dx,c+1))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
H,W=map(int, input().split())
L=[]
for h in range(H):
    l=list(input())
    L.append(l)

vac=[]
wall=[]
for h in range(H):
    for w in range(W):
        l=L[h][w]
        if l=="#":
            wall.append((h,w))
        else:
            vac.append((h,w))
ans=[]
for vt in vac:
    h1,w1=vt
    now=10**5
    for wt in wall:
        h2,w2=wt
        now=min(now, abs(h2-h1)+abs(w2-w1))
    ans.append(now)

print(max(ans))

Submission status: Runtime Error

Solution code:
import copy
import itertools
    
h,w = (int(x) for x in input().split())
ab = []
step = []
count = 0
for i in range(h):
    a = list(input())
    ab.append(a+['#'])
    for j in range(w):
        if a[j] == '#':
            step.append([i,j])

ab.append(list('#'*w))           

dp = [[0 for a in range(w)] for b in range(h)]
 
while len(step) > 0:
    y, x = step.pop(0)
    if ab[y+1][x] == '.' and dp[y+1][x] == 0 :
        step.append([y+1, x])
        dp[y+1][x] = dp[y][x] + 1
    if ab[y][x+1] == '.' and dp[y][x+1] == 0 :
        step.append([y, x+1])
        dp[y][x+1] = dp[y][x] + 1
    if ab[y-1][x] == '.' and dp[y-1][x] == 0 :
        step.append([y-1, x])
        dp[y-1][x] = dp[y][x] + 1
    if ab[y][x-1] == '.' and dp[y][x-1] == 0 :
        step.append([y, x-1])
        dp[y][x-1] = dp[y][x] + 1
    print(step)

#mx = max(list(itertools.chain.from_iterable(dp)))
 
print(mx[0][0])

Submission status: Runtime Error

Solution code:
from collections import deque

h, w = map(int, input().split())
inf = 10 ** 6
a = [[0] * (w + 2), *[[0] + [inf] * w + [0] for _ in range(h)], [0] * (w + 2)]
d = deque()

for i in range(h):
    for j, k in enumerate(input()):
        if k == "#":
            a[i + 1][j + 1] = 0
            d.append((i + 1, j + 1))

stp = ((0, 1), (0, -1), (1, 0), (-1, 0))
res = 0

while d:
    x, y = d.popleft()
    for k, l in stp:
        nx, ny = x + k, y + l
        if a[x][y] + 1 < a[nx][ny]:
            a[nx][ny] = a[x][y] + 1
            res = max(res, a[nx][ny])
            d.append((nx, ny))
print(res)

Submission status: Runtime Error

Solution code:
import queue
from collections import deque

h,w = map(int,input().split())
city = [list(input()) for i in range(h)]
count = [[-1]*w for i in range(h)]
dx = [0,1,0,-1]
dy = [1,0,-1,0]
q = queue.Queue()
for i in range(h):
    for j in range(w):
      if city[i][j] == "#":
        q.put([i,j])
        count[i][j] = 0

def black(): 
  nums = deque()
  while not q.empty():
    p = q.get()
    for i in range(4):
      for j in range(4):
        ny = p[0] + dy[i]
        nx = p[1] + dx[i]
        if 0 <= ny < h and 0 <= nx < w and city[ny][nx] == "." and count[ny][nx] == -1:
          q.put([ny,nx])
          count[ny][nx] = count[p[0]][p[1]] + 1
          nums.append(count[ny][nx])
  return nums.pop()
print(black())

Submission status: Runtime Error

Solution code:
import queue

h,w = map(int,input().split())
city = [list(input()) for i in range(h)]
count = [[-1]*w for i in range(h)]
dx = [0,1,0,-1]
dy = [1,0,-1,0]
q = queue.Queue()
for i in range(h):
    for j in range(w):
      if city[i][j] == "#":
        q.put([i,j])
        count[i][j] = 0

def black(): 
  nums = []
  while not q.empty():
    p = q.get()
    for i in range(4):
      for j in range(4):
        ny = p[0] + dy[i]
        nx = p[1] + dx[i]
        if 0 <= ny < h and 0 <= nx < w and city[ny][nx] == "." and count[ny][nx] == -1:
          q.put([ny,nx])
          count[ny][nx] = count[p[0]][p[1]] + 1
          nums.append(count[ny][nx])
  return max(nums)
print(black())

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
a=[list(input()) for _ in range(h)]
chk=[]
v=[[True]+[False]*(w)+[True] for _ in range(h+2)]
for i in range(h+2):
    v[0][i]=v[w+1][i]=True
cnt=0
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            v[i+1][j+1]=True
            chk.append((i,j))
while chk:
    achk=[]
    cnt+=1
    for nx,ny in chk:
        if not v[nx+1+1][ny+1]:
            v[nx+1+1][ny+1]=True
            achk.append((nx+1,ny))
        if not v[nx+1][ny+1+1]:
            v[nx+1][ny+1+1]=True
            achk.append((nx,ny+1))
        if not v[nx-1+1][ny+1]:
            v[nx-1+1][ny+1]=True
            achk.append((nx-1,ny))
        if not v[nx+1][ny-1+1]:
            v[nx+1][ny-1+1]=True
            achk.append((nx,ny-1))
    chk=achk
print(cnt-1)

Submission status: Runtime Error

Solution code:
def f(q, t, count, i, j):
    if t[i][j] == '.':
        t[i][j] = '#'
        q.append((i, j))
        used[(i, j)] = count

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

t = [[-1] * (w+2)]
for row in range(1, w + 1):
    t.append([ -1] + list(input()) + [-1])
t.append([-1] * (w+2))

q = []
used = {}
for i, row in enumerate(t):
    for j, col in enumerate(row):
        if t[i][j] == '#':
            q.append((i, j))
            used[(i, j)] = 0

while len(q) != 0:
    i, j = q.pop(0)
    count = used[(i, j)]
    f(q, t, count+1, i-1, j)
    f(q, t, count+1, i+1, j)
    f(q, t, count+1, i, j-1)
    f(q, t, count+1, i, j+1)

print(max(used.values()))

Submission status: Runtime Error

Solution code:
def make(i, j):
    if a[i][j] == ".":
        next_queue.put([i, j])
        a[i][j] = "#"
        return 0
    return 0

h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
# print(a)
import queue
next_visit = queue.Queue()

for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            next_visit.put([i, j])
cnt = 0
while True:
    next_queue = queue.Queue()
    while not next_visit.empty():
        [i, j] = next_visit.get()
        if 0 < i:
            cnt += make(i-1, j)
        if 0 < j:
            cnt += make(i, j-1)
        if i < w-1:
            cnt += make(i+1, j)
        if j < h-1:
            cnt += make(i, j+1)
    next_visit = next_queue
    cnt += 1
    if next_queue.empty():
        break
print(cnt-1)

Submission status: Runtime Error

Solution code:
## coding: UTF-8
#from math import gcd 旧環境でこれを有効化するとREを吐きます
 
H,W = map(int,input().split())

c = []
#c.append(['w']*(W+2))
for i in range(H):
    c.append(list(input()))
#c.append(['w']*(W+2))
 
 
 
 
#queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 1
#mode = 0
#answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
#answer = -1
 
queue = []
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '#'):
            queue.append([i, j])

print(-1)

Submission status: Runtime Error

Solution code:
import queue
from collections import deque
h,w = map(int,input().split())

#aij = [input() for i in range(h)]

moji = [[0 for i in range(w)] for j in range(h)]

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

for i in range(h):
    aij = input()
    #print(aij)
    for j in range(w):
        moji[i][j] = aij[j]

#moji = aij
#print(moji,aij)
#exit()

#print(aij)
vx = [0,1,0,-1]
vy = [1,0,-1,0]

ans = 0
q = deque()

for i in range(h*w):
    y = i//w
    x = i%w
    #print(i)
    if moji[y][x] == '#':
        q.append(i)
        dist[y][x] = 0

#ans = -1

ans = 0

while q:
    #print(q.qsize())
    tmp = q.popleft()
    x = tmp//w
    y = tmp%w
    d = dist[y][x]
    #print(tmp,num)
    for j in range(4):
        nx = x+vx[j]
        ny = y+vy[j]
        #print(ny,nx)
        if nx < 0 or nx >= w or ny < 0 or ny >= h:
            continue
        if dist[ny][nx] == -1:
            dist[ny][nx] = d+1
            #moji[ny][nx] = '#'
            q.append(ny*w+nx)
    #ans += 1
    ans = max(d,ans)

print(ans)

Submission status: Runtime Error

Solution code:
import queue
h,w = map(int,input().split())

#aij = [input() for i in range(h)]

moji = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    aij = input()
    #print(aij)
    for j in range(w):
        moji[i][j] = aij[j]

#moji = aij
#print(moji,aij)
#exit()

#print(aij)
vx = [0,1,0,-1]
vy = [1,0,-1,0]

ans = 0
flag = False

for i in range(h*w):
    y = i//w
    x = i%w
    #print(i)
    if moji[y][x] == '#':
        continue
    else:
        q = queue.Queue()
        q.put(i,0)
        
        while flag == False:
            tmp,num = q.get()
            for j in range(4):
                nx = x+vx[j]
                ny = y+vy[j]
                #print(ny,nx)
                if nx < 0 or nx >= w or ny < 0 or ny >= h:
                    continue
                if moji[ny][nx] == "#":
                    flag = True
                    break
                else:
                    q.put([ny*w+nx,num+1])
                    
        ans = max(ans,num)

print(ans)

Submission status: Runtime Error

Solution code:
import math
def agc033_a():
  h, w = map(int, input().split())
  grid = [[s for s in str(input())] for _ in range(h)]

  # 黒マスからの距離(#は0)
  dist = [[math.inf] * w for _ in range(h)]
  for i in range(h):
    for j in range(w):
      if grid[i][j] == '#':
        dist[i][j] = 0

  def ask_dist(i, j):
    adj = math.inf
    if 0 < i: adj = min(adj, dist[i-1][j])
    if i < h-1: adj = min(adj, dist[i+1][j])
    if 0 < j: adj = min(adj, dist[i][j-1])
    if j < w-1: adj = min(adj, dist[i][j+1])
    if adj != math.inf:
      return adj + 1
    else:
      return math.inf

  # 1往復したら距離確定する？？ TLEするかも？？
  d_max = 0
  for i in range(h):
    for j in range(w):
      if dist[i][j] != math.inf: continue
      d_tmp = ask_dist(i, j)
      if d_tmp != math.inf:
        dist[i][j] = d_tmp
        d_max = max(d_max, d_tmp)
  for i in range(h-1, -1, -1):
    for j in range(w-1, -1, -1):
      if dist[i][j] != math.inf: continue
      d_tmp = ask_dist(i, j)
      if d_tmp != math.inf:
        dist[i][j] = d_tmp
        d_max = max(d_max, d_tmp)
  print(d_max)

agc033_a()

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
m = [['#'] * (w+2) for i in range(h+2)]
for j in range(h):
    m[j+1][1:h+1] = list(input())

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

dist = [[-1] * (w+2) for i in range(h+2)]

todo = []

for i in range(1, h+1):
    for j in range(1, w+1):
        if m[i][j] == '#':
            dist[i][j] = 0
            todo.append([i, j])

while(todo != []):
    v = todo.pop(0)
    for i in range(4):
        a, b = v[0]+dx[i], v[1]+dy[i]
        if dist[a][b] == -1 and m[a][b] == '.':
            m[a][b] = '#'
            todo.append([a, b])
            dist[a][b] = dist[v[0]][v[1]] + 1

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

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

H,W=map(int,input().split())
A=[input() for i in range(H)]
ans=0
V=[[0 for i in range(W)] for j in range(H)]

def bfs(q):
    tmp=deque(q)
    ch = [[False for i in range(W)] for j in range(H)]
    count = 0
    d=[[-1,0],[1,0],[0,-1],[0,1]]
    while tmp:
        x,y=tmp.popleft()
        for a,b in d:
            nx=x+a
            ny=y+b
            if 0<=nx<W and 0<=ny<H:
                if ch[nx][ny]!=True and A[nx][ny]==".":
                    V[nx][ny]=V[x][y]+1
                    ch[nx][ny]=True
                    tmp.append([nx,ny])
            else:
                continue

ju=[]

for i in range(W):
    for j in range(H):
        if A[i][j]=="#":
            ju.append([i,j])
bfs(ju)
for i in range(W):
    for j in range(H):
        if not A[i][j]=="#":
             ans=max(ans,V[i][j])

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

H, W = map(int, input().split())
que = deque()
A = [[0 for i in range(W)] for j in range(H)]
white_num = 0
for i in range(H):
    S = input()
    for j in range(W):
        if S[j] == '.':
            A[i][j] = -1
            white_num += 1
        elif S[j] == '#':
            A[i][j] = 0
            que.append([j, i])

ans = 0
while len(que) > 0:
    x, y = que.popleft()
    for k in range(4):
        nx = x + [1, 0, -1, 0][k]
        ny = y + [0, 1, 0, -1][k]
        if nx < 0 or ny < 0 or nx >= W or ny >= W:
            continue
        if A[ny][nx] == -1:
            que.append([nx, ny])
            A[ny][nx] = A[y][x] + 1
            ans = max(ans, A[ny][nx])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

H,W=map(int,input().split())
A=[input() for i in range(H)]
ans=0
V=[[float('inf') for i in range(W)] for j in range(H)]

def bfs(sx,sy):
    tmp=deque([[sx,sy]])
    ch=[[False for i in range(W)] for j in range(H)]
    P=[[0 for i in range(W)] for j in range(H)]
    d=[[-1,0],[1,0],[0,-1],[0,1]]
    while tmp:
        x,y=tmp.popleft()
        for a,b in d:
            nx=x+a
            ny=y+b
            if 0<=nx<W and 0<=ny<H:
                if ch[nx][ny]!=True and A[nx][ny]==".":
                    P[nx][ny]=P[x][y]+1
                    ch[nx][ny]=True
                    tmp.append([nx,ny])
    for i in range(W):
        for j in range(H):
            V[i][j]=min(P[i][j],V[i][j])

for i in range(W):
    for j in range(H):
        if A[i][j]=="#":
            bfs(i,j)

for i in range(W):
    for j in range(H):
        if not A[i][j]=="#":
             ans=max(ans,V[i][j])

print(ans)

Submission status: Runtime Error

Solution code:
import copy

H, W = map(int, input().split())
A = [[]for j in range(H)]
white_num = 0
for i in range(H):
    S = input()
    A[i] = S
    for j in range(W):
        if A[i][j] == '.':
            white_num += 1

ans = 0
while white_num > 0:
    A_ = copy.deepcopy(A)
    ans += 1
    for y in range(H):
        for x in range(W):
            if A[y][x] == "#":
                for k in range(4):
                    nx = x + [1, 0, -1, 0][k]
                    ny = y + [0, 1, 0, -1][k]
                    if nx < 0 or ny < 0 or nx >= W or ny >= W:
                        continue
                    if A[ny][nx] == "." and A_[ny][nx] == ".":
                        A_[ny] = A_[ny][:nx] + "#" + A_[ny][nx+1:]
                        white_num -= 1
    A = A_
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

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


from collections import deque
todo = deque()
seen = [[False] * W for _ in range(H)]
route= [[0] * W for _ in range(H)]
for y in range(H):
    for x in range(W):
        if A[y][x] == '#':
            todo.append((x, y))
            seen[y][x] = True



dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
cnt = 0
while todo:
    for _ in range(len(todo)):
        x, y = todo.popleft()
        #print(x, y)
        for i in range(4):
            if 0 <= x + dx[i] < W and 0 <= y + dy[i] < H:
                if seen[y + dy[i]][x + dx[i]] == False:
                    route[y + dy[i]][x + dx[i]] = route[y][x] + 1
                    seen[y + dy[i]][x + dx[i]] = True
                    todo.append((x + dx[i], y + dy[i]))
    # cnt += 1

#print(seen)
# print(route)
M = 0
for y in range(W):
    for x in range(H):
        M = max(M, route[y][x])
print(M)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

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

from collections import deque
todo = deque()
seen = [[False] * W for _ in range(H)]
route= [[0] * W for _ in range(H)]
for y in range(H):
    for x in range(W):
        if A[y][x] == '#':
            todo.append((x, y))
            seen[y][x] = True


seen = [[False] * W for _ in range(H)]
route= [[0] * W for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
cnt = 0
while todo:
    for _ in range(len(todo)):
        x, y = todo.popleft()
        #print(x, y)
        for i in range(4):
            if 0 <= x + dx[i] < W and 0 <= y + dy[i] < H:
                if seen[y + dy[i]][x + dx[i]] == False:
                    route[y + dy[i]][x + dx[i]] = route[y][x] + 1
                    seen[y + dy[i]][x + dx[i]] = True
                    todo.append((x + dx[i], y + dy[i]))
    # cnt += 1

#print(seen)
#print(route)
M = 0
for y in range(W):
    for x in range(H):
        M = max(M, route[y][x])
print(M)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
A = []
for _ in range(H):
    A.append(input())
import queue
todo = queue.Queue()
for y in range(H):
    for x in range(W):
        if A[y][x] == '#':
            exist = True
            todo.put((x, y))

seen = [[False] * W for _ in range(H)]
route= [[0] * W for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
def bfs(el):
    x, y = el
    #print(x, y)
    seen[y][x] = True
    for i in range(4):
        if 0 <= x + dx[i] < W and 0 <= y + dy[i] < H:
            if seen[y + dy[i]][x + dx[i]] == False and A[y + dy[i]][x + dx[i]] == '.':
                route[y + dy[i]][x + dx[i]] = route[y][x] + 1
                seen[y + dy[i]][x + dx[i]] = True
                todo.put((x + dx[i], y + dy[i]))
    if not todo.empty():
        bfs(todo.get())
    else:
        return None
bfs(todo.get())
#print(seen)
#print(route)

M = 0
for y in range(W):
    for x in range(H):
        M = max(M, route[y][x])
print(M)