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:
h,w=map(int,input().split())
a=[list(input()) for i in range(h)]
brack1=[]
brack2=[]
for j in range(h):
    tmp=[i for i, x in enumerate(a[j]) if x == '#']
    [brack1.append(k+1+j+1) for k in tmp]
    [brack2.append(w-k + j + 1) for k in tmp]

brack1=sorted(list(set(brack1)))
brack2=sorted(list(set(brack2)))

if len(brack1)>=len(brack2):
    B=len(brack1)
else:
    B=len(brack2)

brack1.append(h+w+1)
brack2.append(h+w+1)

b=1
c=1
ans=0
for i in range(B):
    if brack1[i]-b-1>ans:
        ans=brack1[i]-b-1
    b=brack1[i]
    try:
        if brack2[i]-c-1>ans:
            ans=brack2[i]-c-1
    except:
        pass
    c=brack2[i]

print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
li=[input() for i in range(h)]
black=[]



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

count=0

for i in range(h):
    
    nearblack=20000
    nearpoint=[]
    
    
    for m in range(len(black)):     
  
            
        if nearblack>abs(black[m][0]-i) and abs((black[m][0]-i))>0:
          
            nearblack=abs(black[m][0]-i)
            nearpoint=[]
            nearpoint.append(black[m][1])
            
        elif nearblack==abs(black[m][0]-i) and abs((black[m][0]-i))>0:
            nearpoint.append(black[m][1])
    
    
    
    distance=[10000 for i in range(w)]
    
    for j  in range(w):
        
        
        if li[i][j]=="#":
            for p in range(w):
                distance[p]=min(abs(j-p),distance[p])
                
                
    for q in range(w):
        di=[abs(nearpoint[z]-q)+nearblack for z in range(len(nearpoint))]
        distance[q]=min(distance[q],(min(di)))
        

        

    
    count=max(max(distance),count)

            
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W= map(int,input().split())
A = []
for i in range(H):
    A.append(input())
    
black = [] # 初期黒マスの座標
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

b = np.array(black) # 2*len(black)の行列

ans = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '.':
            dis = []
            d = b - np.array([[i,j]])
            for l in range(len(black)):
                dis.append(abs(d[l,0])+abs(d[l,1]))
            ans.append(min(dis))

print(max(ans))

Submission status: Runtime Error

Solution code:
from sys import stdin

N = [int(x) for x in stdin.readline().rstrip().split()]
Input = [list(stdin.readline().rstrip()) for _ in range(N[0])]


Room = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]
Room_next = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]
count = 0


def is_darkroom(array):
    p = True
    for i in range(1, N[0] + 1):
        for j in range(1, N[1] + 1):
            if array[i][j] == 1:
                p = False
    return p

for i in range(N[0] + 2):
    for j in range(N[1] + 2):
        if i == 0 or i == N[0] + 1 or j == 0 or j == N[1] + 1:
            Room[i][j] = 1
        else:
            Room[i][j] = 1 if Input[i-1][j-1] == "." else 0

for k in range(N[0] + N[1] + 2):
    if is_darkroom(Room):
        count = k
        break
    for i in range(1, N[0] + 1):
        for j in range(1, N[1] + 1):
            Room_next[i][j] = Room[i][j] * Room[i - 1][j] * Room[i + 1][j] * Room[i][j - 1] * Room[i][j + 1]
                    
    for i in range(1, N[0] + 1):
        for j in range(1, N[1] + 1):
            Room[i][j] = Room_next[i][j]
    Room_next = [[1 for i in range(N[0] + 2)] for j in range(N[1] + 2)]


print(count)

Submission status: Runtime Error

Solution code:
hw = list(map(int, list(input().split())))
h = hw[0]
w = hw[1]

a = []
for i in range(h):
    apend = list(input())
    a.append(apend)

white = []
black = []

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

number1 = []
if i in range(h):
    number = []
    for j in range(w):
        if a[i][j] == ".":
            if i == 0:
                if j == 0:
                    number.append(0)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number1[i - 1][j] + 1)
                else:
                    number.append(min(number[j - 1], number1[i - 1][j]) + 1)
        else:
            number.append(0)
    number1.append(number)

number2 = []
if i in range(h):
    number = []
    for j in range(w):
        if a[i][- j - 1] == ".":
            if i == 0:
                if j == 0:
                    number.append(0)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number2[i - 1][- j - 1] + 1)
                else:
                    number.append(min(number[j - 1], number2[i - 1][- j - 1]) + 1)
        else:
            number.append(0)
    newnumber = []
    for i in number[::-1]:
        newnumber.append(number[i])
    number2.append(newnumber)


number3 = []
if i in range(h):
    number = []
    for j in range(w):
        if a[- i - 1][j] == ".":
            if i == 0:
                if j == 0:
                    number.append(0)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number3[i - 1][j] + 1)
                else:
                    number.append(min(number[j - 1], number3[i - 1][j]) + 1)
        else:
            number.append(0)
    number3.append(number)

    number6 = number3
    number3 = []
    for i in number6[::-1]:
        number3.append(number6[i])


number4 = []
if i in range(h):
    number = []
    for j in range(w):
        if a[- i - 1][- j - 1] == ".":
            if i == 0:
                if j == 0:
                    number.append(0)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number4[i - 1][- j - 1] + 1)
                else:
                    number.append(min(number[j - 1], number4[i - 1][- j - 1]) + 1)
        else:
            number.append(0)
    newnumber = []
    for i in number[::-1]:
        newnumber.append(number[i])
    number4.append(number)

number6 = number4
number4 = []
for i in number6[::-1]:
    number4.append(number6[i])

number5 = []

for i in range(len(white)):
    number5.append(min(number1[white[i][0]][white[i][0]], number2[white[i][0]][white[i][0]], number3[white[i][0]][white[i][0]], number4[white[i][0]][white[i][0]]))

print(max(number5))

Submission status: Runtime Error

Solution code:
for i in range(h):
    for j  in range(w):
        if li[i][j]=="#":
            black.append([i,j])  
    

count=0

for i in range(h):
    
    nearblack=20000
    nearpoint=[]
    
    
    for m in range(len(black)):     
  
            
        if nearblack>abs(black[m][0]-i) and abs((black[m][0]-i))>0:
          
            nearblack=abs(black[m][0]-i)
            nearpoint=[]
            nearpoint.append(black[m][1])
            
        elif nearblack==abs(black[m][0]-i) and abs((black[m][0]-i))>0:
            nearpoint.append(black[m][1])
    
    
    
    distance=[10000 for i in range(w)]
    
    for j  in range(w):
        
        
        if li[i][j]=="#":
            for p in range(w):
                distance[p]=min(abs(j-p),distance[p])
                
                
    for q in range(w):
        di=[abs(nearpoint[z]-q)+nearblack for z in range(len(nearpoint))]
        distance[q]=min(distance[q],(min(di)))
        

        
            
          
            
    print(distance)    
    
    count=max(max(distance),count)

            
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
import sys
import collections
import copy
deque=collections.deque
fastinput=sys.stdin.readline
whites=deque()
firstwhite=deque()
H,W=map(int,input().split())
A=[list(fastinput().rstrip()) for j in range(W)]
rinsetsu=np.zeros((H*W,H*W))
F=np.zeros((1,H*W))

def UDLR(i,j):#各フラグごとに、白なら隣接追加　黒ならfirstwhite追加
    ID=W*i+j
    next2black=0
    rinsetsu[ID][ID]=1
    if i!=0:
        if A[i-1][j]==".":
            rinsetsu[ID][ID-W]=1
        else:F[0][ID]=1
    if i!=H-1:
        if A[i+1][j]==".":
            rinsetsu[ID][ID+W]=1
        else:F[0][ID]=1
    if j!=0:
        if A[i][j-1]==".":
            rinsetsu[ID][ID-1]=1
        else:F[0][ID]=1
    if j!=W-1:
        if A[i][j+1]==".":
            rinsetsu[ID][ID+1]=1
        else:F[0][ID]=1      

for k,i in enumerate(A):
    for l,j in enumerate(i):
        if j==".":
            whites.append(W*k+l)
            UDLR(k,l)
whitess=len(whites)
rinsetsuMatrix=np.matrix(rinsetsu)
ct=1
while 1:
    ct+=1
    F=np.dot(F,rinsetsuMatrix)
    sums=np.array(np.sum(F, axis=0))
    if sum([bool(i) for i in sums[0]])==whitess:
        print(ct)
        break

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

# 黒: #
# 白: .

H, W = input().split()

H = int(H)
W = int(W)

A = np.zeros([H,W])

for i in range(H):
    chars = np.array(list(input()))
    A[i,:] = np.where(chars=='.', 0, 1)

cnt = 0
w_num = np.sum(A == 0)

while w_num != 0:
    cnt += 1
    B = np.zeros([H,W])
    for i in range(W):
        for j in range(H):
            if A[i,j] == 1:
                B[i,j] = 1
                if i-1 >= 0:
                    B[i-1,j] = 1
                if i+1 < W:
                    B[i+1,j] = 1
                if j-1 >= 0:
                    B[i,j-1] = 1
                if j+1 < H:
                    B[i,j+1] = 1

    w_num = np.sum(B == 0)
    A = B

print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np


def main():
    H, W = [int(x) for x in input().split()]
    board = [[0 for i in range(H)] for j in range(W)]

    """ #は1, .は0 """
    for i in range(H):
        X = input()
        for j in range(len(X)):
            if X[j] == "#":
                board[i][j] = 1

    num_board = np.array(board)
    height, width = num_board.shape[:2]
    count = 0
    # (np.sum(num_board) != height*width)
    while True:
        num_board_ans = np.zeros((height, width))
        for i in range(height):
            for j in range(width):
                if num_board[i, j] == 1:
                    num_board_ans[i, j] = 1
                    a = max(i-1, 0)
                    num_board_ans[a, j] = 1
                    b = max(j-1, 0)
                    num_board_ans[i, b] = 1
                    c = min(i+1, height-1)
                    num_board_ans[c, j] = 1
                    d = min(j+1, width-1)
                    num_board_ans[i, d] = 1
        num_board = num_board_ans
        count += 1
        if (np.sum(num_board) == height*width):
            break

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
X = [list(map(str, list(input()))) for i in range(H)]
A = []
ANS = [[10000 for i in range(W)] for j in range(H)]
ans = 0


def dp(y, x, ANS, H, W):
    if x - 1 >= 0 and ANS[y][x - 1] > ANS[y][x] + 1:
        ANS[y][x - 1] = ANS[y][x] + 1
        dp(y, x - 1, ANS, H, W)
    if x + 1 < W and ANS[y][x + 1] > ANS[y][x] + 1:
        ANS[y][x + 1] = ANS[y][x] + 1
        dp(y, x + 1, ANS, H, W)
    if y - 1 >= 0 and ANS[y - 1][x] > ANS[y][x] + 1:
        ANS[y - 1][x] = ANS[y][x] + 1
        dp(y - 1, x, ANS, H, W)
    if y + 1 < H and ANS[y + 1][x] > ANS[y][x] + 1:
        ANS[y + 1][x] = ANS[y][x] + 1
        dp(y + 1, x, ANS, H, W)


for i in X:
    tmp = []
    for j in i:
        if j == "#":
            tmp.append(0)
        elif j == ".":
            tmp.append(1)
    else:
        A.append(tmp)

for y in range(H):
    for x in range(W):
        if A[y][x] == 0:
            ANS[y][x]=0;
            dp(y, x, ANS, H, W)

for i in ANS:
    ans = max([ans, max(i)])

print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
ul = 1
dl = H
rl = W
ll = 1
for s, t in zip(S[1::][::-1], T[::-1]):
    if t == 'U':
        dl = min(H, dl+1)
    elif t == 'D':
        ul = max(1, ul-1)
    elif t == 'L':
        rl = min(W, rl+1)
    else:
        ll = max(1, ll-1)
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 1
    if ul > dl or ll > rl or ul == H + 1 or dl == 0 or ll == W + 1 or rl == 0:
        print('NO')
        exit()
s = S[0]
if s == 'U':
    ul -= 1
elif s == 'D':
    dl += 1
elif s == 'L':
    ll -= 1
else:
    rl += 1

if ul <= sr <= dl and ll <= sc <= rl:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

Solution code:
import numpy as np
import sys
import collections
import copy
deque=collections.deque
fastinput=sys.stdin.readline
whites=deque()
firstwhite=deque()
H,W=map(int,input().split())
A=[list(fastinput().rstrip()) for j in range(W)]
rinsetsu=np.zeros((H*W,H*W))
F=np.zeros((1,H*W))

def printsix(l):
    for i in range(5):
        print(l[6*i:6*i+6])

def UDLR(i,j):#各フラグごとに、白なら隣接追加　黒ならfirstwhite追加
    ID=W*i+j
    next2black=0
    rinsetsu[ID][ID]=1
    if i!=0:
        if A[i-1][j]==".":
            rinsetsu[ID][ID-W]=1
        else:F[0][ID]=1
    if i!=H-1:
        if A[i+1][j]==".":
            rinsetsu[ID][ID+W]=1
        else:F[0][ID]=1
    if j!=0:
        if A[i][j-1]==".":
            rinsetsu[ID][ID-1]=1
        else:F[0][ID]=1
    if j!=W-1:
        if A[i][j+1]==".":
            rinsetsu[ID][ID+1]=1
        else:F[0][ID]=1      

for k,i in enumerate(A):
    for l,j in enumerate(i):
        if j==".":
            whites.append(W*k+l)
            UDLR(k,l)
whitess=len(whites)
rinsetsuMatrix=np.matrix(rinsetsu)
ct=1
for i in range(5):
    ct+=1
    F=np.dot(F,rinsetsuMatrix)
    sums=np.array(np.sum(F, axis=0))
    if sum([bool(i) for i in sums[0]])==whitess:
        print(ct)
        break

Submission status: Runtime Error

Solution code:
import queue
h,w=list(map(int,input().split()))
a=[]
for i in range(h):
  a.append(input())

#a[たて][よこ]
ma=0
li=[[0]*w for _ in range(h)]
li2=[[True]*w for _ in range(h)]
#そのマスが何手目で黒くなるかを調べる
#隣に黒がなければ、上下左右の一番小さいserch+1
#隣に黒があれば、1
def search(i,j):
  li2[i][j]= False
  if li[i][j]!= 0:
    return li[i][j]
  u=9999
  d=9999
  r=9999
  l=9999
  if i-1>=0:
    if a[i-1][j]=="#":
      li[i][j]=1
      return 1
    elif li2[i-1][j]:
      u=search(i-1,j)
  if i+1<h:
    if a[i+1][j]=="#":
      li[i][j]=1
      return 1
    elif li2[i+1][j]:
      d=search(i+1,j)
  if j-1>=0:
    if a[i][j-1]=="#":
      li[i][j]=1
      return 1
    elif li2[i][j-1]:
      l=search(i,j-1)
  if j+1<w:
    if a[i][j+1]=="#":
      li[i][j]=1
      return 1
    elif li2[i][j+1]:
      r=search(i,j+1)
  li[i][j]=min(u,d,r,l)+1
  return min(u,d,r,l)+1

ans=0
for hi in range(h):
  for wi in range(w):
    if a[hi][wi]==".":
      li2=[[True]*w for _ in range(h)]
      tmp= search(hi,wi)
      if tmp>ans:
        ans=tmp
print(ans)

Submission status: Runtime Error

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

ans = 0
while True:
  status = []
  for i in range(W):
    if "." in A[i]:
      status.append(False)
  if False not in status:
    break
  else:
    check_list = []
    for i in range(W):
      for j in range(H):
        if A[i][j] == "#":
          if (i-1)>=0:
            if A[i-1][j] != "#":
              check_list.append([i-1,j])
          if (j-1)>=0:
            if A[i][j-1] != "#":
              check_list.append([i,j-1])
          if (i+1)<=(W-1):
            if A[i+1][j] != "#":
              check_list.append([i+1,j])
          if (j+1)<=(H-1):
            if A[i][j+1] != "#":
              check_list.append([i,j+1])

    for check in check_list:
      A[check[0]][check[1]] = "#"
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import copy
h, w = map(int, input().split())
direction = [[1,0],[0,1],[-1,0],[0,-1]]
a = [['.' for j in range(h + 2)] for i in range(h + 2)]
for i in range(h):
    line = list(input())
    for j, c in enumerate(line):
        a[i+1][j+1] = c
ans = 0
while not sum([line.count('#') for line in a]) == h * w:
    tem = copy.deepcopy(a)
    for i, line in enumerate(a):
        for j, c in enumerate(line):
            if c == '#':
                for dire in direction:
                    x, y = dire
                    if h+1 > i + x > 0 and w+1 > j + y > 0:
                        tem[i + x][j + y] = '#'
    ans += 1
    a = tem
print(ans)

Submission status: Runtime Error

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

 ans = 0
 while True:
   status = []
   for i in range(W):
     if "." in A[i]:
       status.append(False)
   if False not in status:
     break
   else:
     check_list = []
     for i in range(W):
       for j in range(H):
         if A[i][j] == "#":
           if (i-1)>=0:
             if A[i-1][j] != "#":
               check_list.append([i-1,j])
           if (j-1)>=0:
             if A[i][j-1] != "#":
               check_list.append([i,j-1])
           if (i+1)<=(W-1):
             if A[i+1][j] != "#":
               check_list.append([i+1,j])
           if (j+1)<=(H-1):
             if A[i][j+1] != "#":
               check_list.append([i,j+1])

     for check in check_list:
       A[check[0]][check[1]] = "#"
     ans += 1

 print(ans)

Submission status: Runtime Error

Solution code:
def check(a,b,matrix):
    length=a*b
    for i in range(0,length):
            if(matrix[i]=="."):
                return 0
    
    return 1

def new_matrix(a,b,matrix):
    new=[]
    for i in range(0,a):
        for j in range(0,b):
            new.append(".")

    index_i=[]
    for i in range(0,len(new)):
            if(matrix[i]=="#"):
                index_i.append(i)
    
    for i in index_i:
    
        new[i]="#"
        if(i==0):
           
            new[i+1]="#"
            new[i+b]="#"
        elif(i==b-1):
          
            new[i-1]="#"
            new[i-b]="#"
        elif(i==len(new)-b):
         
            new[i-b]="#"
            new[i+1]="#"
        elif(i==len(new)-1):
           
            new[i-1]="#"
            new[i-b]="#"
        elif(i%b==0):
         
            new[i+1]="#"
            new[i+b]="#"
            new[i-b]="#"
        elif(i+1%b==0):
            new[i-1]="#"
            new[i+1]="#"
            new[i-b]="#"
        elif(i>len(new)-b):
            new[i-1]="#"
            new[i+1]="#"
            new[i-b]="#"
        elif(i<b):
            new[i-1]="#"
            new[i+1]="#"
            new[i+b]="#"
        else:
            new[i-1]="#"
            new[i+1]="#"
            new[i+b]="#"
            new[i-b]="#"

    return new



a,b=(int(x) for x in input().split())
matrix=[]
for i in range(0,b):
    x=input()
    for j in x:
        matrix.append(j)

count=0
while(check(a,b,matrix)!=1):
    matrix=new_matrix(a,b,matrix)
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
A = []
for i in range(W):
    A.extend(list(input()))
count = 0
up_down_left_right = [-W, W, -1, 1]

while True:
    changelist = []
    if all([i == "#" for i in A]):
        print(count)
        break
    for i in range(len(A)):
        if A[i] == "#":
            for m in range(4):
                if i % W == 0 and m == 2:
                    continue
                if i % W == 2 and m == 3:
                    continue
                point = i + up_down_left_right[m]
                if point < 0 or point > W * H - 1:
                    continue
                changelist.append(point)
    for i in changelist:
        A[i] = "#"
    count += 1

Submission status: Runtime Error

Solution code:
# coding: utf-8
#-------------------------------------------------------------------
import sys
def p(*a):
  #s=" ".join(map(str,a))
  ##print(s)
  #sys.stderr.write(s+"\n")
#-------------------------------------------------------------------

H,W=map(int, input().split())
M = list( list(input()) for _ in range(H) )

p(H,W)
p(M)

for h in range(H):
	for w in range(W):
		c=M[h][w]
		if c==".": M[h][w]=0
		if c=="#": M[h][w]=1

p(M)


for h in range(H):
	for w in range(W-1):
		i=M[h][0+w]
		j=M[h][1+w]
		
		if i==0: continue
		if j==0: M[h][1+w]=i+1
		if j>i+1:M[h][1+w]=i+1

	for w in range(W-1):
		i=M[h][-1-w]
		j=M[h][-2-w]

		if i==0: continue
		if j==0: M[h][-2-w]=i+1
		if j>i+1:M[h][-2-w]=i+1

p(M)

for w in range(W):
	for h in range(H-1):
		i=M[0+h][w]
		j=M[1+h][w]
		
		if i==0: continue
		if j==0: M[1+h][w]=i+1
		if j>i+1:M[1+h][w]=i+1
	
	for h in range(H-1):
		i=M[-1-h][w]
		j=M[-2-h][w]
	
		if i==0: continue
		if j==0: M[-2-h][w]=i+1
		if j>i+1:M[-2-h][w]=i+1

p(M)

ans=0
for h in range(H):
	ans=max(ans, max(M[h]))

print( max(0,ans-1) )

Submission status: Runtime Error

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


import queue
q = queue.Queue() 
for i in range(W):
    for j in range(H):
        if A[i][j] == '#': q.put( [i,j,0] )

ans = 0
while not q.empty():
    tmp = q.get()
    i, j, level = tmp[0], tmp[1], tmp[2]
    ans = level
    if i+1 <  H and A[i+1][j] == '.':
        A[i+1][j] = '#'
        q.put( [i+1,j,level+1] )
    if i-1 >= 0 and A[i-1][j] == '.': 
        A[i-1][j] = '#'
        q.put( [i-1,j,level+1] )
    if j+1 <  W and A[i][j+1] == '.': 
        A[i][j+1] = '#'
        q.put( [i,j+1,level+1] )
    if j-1 >= 0 and A[i][j-1] == '.': 
        A[i][j-1] = '#'
        q.put( [i,j-1,level+1] )

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

b, c = map(int, input().split())
K = np.zeros((b,c))
for i in range(b):
    s = input()
    for j in range(c):
        if s[j] == "#" :
            K[i,j] =1

count = 0
if np.all(K>0):
    print(0)
else:
    while(1):
        count =count+1
        S = copy.deepcopy(K)
        S1 = np.vstack((S,np.zeros(b).reshape(1,-1)))
        S1 = S1[1:,:]
        S2 = np.vstack((np.zeros(b).reshape(1,-1),S))
        S2 = S2[0:-1,:]
        S3 = np.hstack((S,np.zeros(c).reshape(-1,1)))
        S3 = S3[:,1:]
        S4 = np.hstack((np.zeros(c).reshape(-1,1),S))
        S4 = S4[:,0:-1]
        K = S+S1+S2+S3+S4
        if np.all(K>0):
            break
        

    print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

import sys
sys.setrecursionlimit(1000000)

h,w = map(int,input().split())
maps  = np.array([ list(str(input())) for _ in range(h)])


def f(sx,sy,x,y,a):
    if 0 <= x < w and 0 <= y < h and d[x][y]:
        if maps[x][y] == "#":
            dis = abs(x - sx) + abs(y - sy)
            a.append(dis)
        else:
            d[x][y] = 0
            f(sx,sy,x+1,y,a)
            f(sx,sy,x,y+1,a)
            f(sx,sy,x-1,y,a)
            f(sx,sy,x,y-1,a)

ans = 0
for i in range(h):
    for j in range(w):
        #d = [[1] * w for _ in range(h)]
        d = np.ones((h,w))
        a = []
        f(i,j,i,j,a)
        a = min(a)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

Solution code:
import sys

if __name__ == '__main__':
    H, W = map(lambda x:int(x), sys.stdin.readline().split())
    # array = [[0 for x in range(W)] for y in range(H)]
    array = []
    for i in range(H):
        array += [list(sys.stdin.readline().rstrip())]
    # print(array)
    new_array = [[0 for x in range(W)] for y in range(W)]
    count = 0
    checked = {}
    while True:
        # print(array)
        # new_array = [arr[:] for arr in array]
        # new_array = [[a for a in arr] for arr in array]
        for y in range(H):
            for x in range(W):
                new_array[x][y] = array[x][y]

        flag = True
        # print(array)
        for y in range(H):
            for x in range(W):
                if y*H+x in checked:
                    continue
                if array[x][y] == '#':
                    if x - 1 >= 0:
                        new_array[x-1][y] = '#'
                    if x + 1 < W:
                        new_array[x+1][y] = '#'
                    if y - 1 >= 0:
                        new_array[x][y-1] = '#'
                    if y + 1 < H:
                        new_array[x][y+1] = '#'
                    checked[y*H+x] = 1
                else:
                    if flag:
                        flag = False
                        count += 1
        if flag:
            break

        for y in range(H):
            for x in range(W):
                array[x][y] = new_array[x][y]
    print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

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

grid = [[False] * (w + 2)]
for _ in range(w):
    grid.append([False] + [x == '#' for x in input()] + [False])
grid.append([False] * (w + 2))

grid = np.array(grid)

cnt = 0
while True:
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            print('#' if grid[i, j] else '.', end='')
        print()
    if np.all(grid[1:-1, 1:-1]):
        break

    new_grid = grid.copy()
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if grid[i][j] == 0:
                if grid[i + 1, j] or grid[i, j + 1] or grid[i - 1, j] or grid[i, j - 1]:
                    new_grid[i][j] = 1
    grid = new_grid
    cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np

import sys
sys.setrecursionlimit(1000000)

h,w = map(int,input().split())
maps  = np.array([ list(str(input())) for _ in range(h)])


def f(sx,sy,x,y,a):
    if 0 <= x < w and 0 <= y < h and d[x][y]:
        if maps[x][y] == "#":
            dis = abs(x - sx) + abs(y - sy)
            a.append(dis)
        else:
            d[x][y] = 0
            f(sx,sy,x+1,y,a)
            f(sx,sy,x,y+1,a)
            f(sx,sy,x-1,y,a)
            f(sx,sy,x,y-1,a)

ans = 0
for i in range(h):
    for j in range(w):
        #d = [[1] * w for _ in range(h)]
        d = np.ones((h,w))
        a = []
        f(i,j,i,j,a)
        a = min(a)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np


def main():
    H, W = [int(x) for x in input().split()]
    board = [[0 for i in range(H)] for j in range(W)]

    """ #は1, .は0 """
    for i in range(H):
        X = input()
        for j in range(len(X)):
            if X[j] == "#":
                board[i][j] = 1

    num_board = np.array(board)
    height, width = num_board.shape[:2]
    count = 0
    while (np.sum(num_board) != height*width):
        num_board_ans = np.zeros((height, width))
        for i in range(height):
            for j in range(width):
                if num_board[i, j] == 1:
                    num_board_ans[i, j] = 1
                    a = max(i-1, 0)
                    num_board_ans[a, j] = 1
                    b = max(j-1, 0)
                    num_board_ans[i, b] = 1
                    c = min(i+1, height-1)
                    num_board_ans[c, j] = 1
                    d = min(j+1, width-1)
                    num_board_ans[i, d] = 1
        num_board = num_board_ans
        count += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def manhattand(arg1,arg2):
    x1 = arg1[0]
    y1 = arg1[1]
    x2 = arg2[0]
    y2 = arg2[1]
    return (abs(x2-x1)+abs(y1-y2))


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

list1 = []
listk = []

for i in range(H):
    a = input()
    list2 = []
    for j in range(W):
        list2.append(a[j])
        if a[j] == "#":
             listk.append((i,j))
    list1.append(list2)

kld = []
for k in range(H):
    for l in range(W):
        if (list1[k][l]) == ".":
            maxd = manhattand(listk[0], (k,l))
            for m in range(len(listk)):
                maxd = min(maxd,manhattand(listk[m], (k,l)))

        

print(max(kld))

Submission status: Runtime Error

Solution code:

H,W=map(int,input().split())
a1=[]
b1=[]
a2=[]
b2=[]
for i in range(H):
    S=str(input())
    for j in range(W):
        if S[j]=="#":

            a1.append(i+j)
            b1.append(i-j)
        else:
            a2.append(i+j)
            b2.append(i-j)

t=[]
for i in range(len(a2)):
    m=[]
    for j in range(len(a1)):
        m.append(max(abs(a1[j]-a2[i]),abs(b1[j]-b2[i])))

    p=min(m)

    t.append(p)
print(max(t))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
A=[list(input()) for i in range(h)]
B=[]
for i in range(w):
  for j in range(h):
    if A[i][j]=="#":
      B.append([i,j])
ans=0
for i in range(w):
  for j in range(h):
    d=min(abs(i-b[0])+abs(j-b[1]) for b in B)     
    ans=max(d,ans)
print(ans)