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 = [int(x) for x in input().split(' ')]

A = []
A.append([-1]*(W+2))

for i in range(H):
  b = [-1]
  a1 = input()
  for j in range(W):
    if a1[j] == '.':
      b.append(0)

    else:
      b.append(1)
      xx.append((i,j))
  b.append(-1)
  A.append(b)
A.append([-1]*(W+2))

zc = 0
while True:
  V = []
  for j in range(1,H+1):
    for i in range(1,W+1):
      vv = A[j][i]
      if vv == 0:
        v0 = (A[j-1][i] == 1)
        v1 = (A[j][i-1] == 1)
        v2 = (A[j][i+1] == 1)
        v3 = (A[j+1][i] == 1)
        vc = v0 or v1 or v2 or v3
        #print(i,j,vc,vv)
        if vc == True:
          V.append((j,i))
  if len(V) > 0:
    zc += 1
    for v in V:
      j,i = v
      A[j][i] = 1
  else:
    break
    
print(zc)

Submission status: Runtime Error

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

def dfs(i, j):
  if i < 0 or i > H - 1 or j < 0 or j > W - 1 or memo[i][j] == True:
    return float("inf")
  if A[i][j] == "#":
    return 1

  memo[i][j] = True
  return min(dfs(i - 1, j), dfs(i + 1, j), dfs(i, j - 1), dfs(i, j + 1)) + 1

ans = 0
for i in range(H):
  for j in range(W):
    if A[i][j] == ".":
      memo = [[False for _ in range(W)] for _ in range(H)]
      ans = max(ans, dfs(i, j))

print(max(ans - 1, 0))

Submission status: Runtime Error

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

def dfs(i, j):
  if i < 0 or i > H - 1 or j < 0 or j > W - 1 or memo[i][j] == True:
    return float("inf")
  if A[i][j] == "#":
    return 1

  memo[i][j] = True
  return min(dfs(i - 1, j), dfs(i + 1, j), dfs(i, j - 1), dfs(i, j + 1)) + 1

ans = 0
for i in range(H):
  for j in range(W):
    if A[i][j] == ".":
      memo = [[False for _ in range(W)] for _ in range(H)]
      ans = max(ans, dfs(i, j))

print(max(ans - 1, 0)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
alist = [list(".{}.".format(input())) for j in range(W)]

s = ['.' for i in range(W + 2)]
alist.insert(0, s)
alist.append(s)

alist = [[(1 if i == '#' else 0) for i in j] for j in alist]

import copy
adumy = copy.deepcopy(alist)
is_proc = True
c = 0
while is_proc:
  is_proc = False
  
  for i in range(1, H+1):
    for j in range(1, W+1):
      n = alist[i][j] + max(alist[i][j-1], alist[i-1][j], alist[i+1][j], alist[i][j+1])
      adumy[i][j] = n
      
      if n == 0:
        is_proc = True

  alist = copy.deepcopy(adumy)
  #[print(j) for j in adumy]
  #print()
  
  c += 1

print(c)
  
  

#[print(j) for j in alist]

Submission status: Runtime Error

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

L = [[0 for _ in range(W)] for _ in range(H)]

def search(i, j, l):
    if i == -1 or i == H or j == -1 or j == W:
        return
    if A[i][j] == '#':
        if l > 0:
            return
    elif L[i][j] == 0 or L[i][j] > l:
        L[i][j] = l
    else:
        return
    search(i - 1, j, l + 1)
    search(i + 1, j, l + 1)
    search(i, j - 1, l + 1)
    search(i, j + 1, l + 1)

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            search(i, j, 0)

max = 0
for i in range(H):
    for j in range(W):
        if L[i][j] > max:
            max = L[i][j]

print(max)

Submission status: Runtime Error

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

A=[]
for i in range(H):
    A.append(list(input()))
    
black=[]
white=[]

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


            
            
ans=[]
for w in white:
    mim=H+W
    for b in black:
        x=abs(b[0]-w[0])
        y=abs(b[1]-w[1])
        l=int(x+y)
        if l<mim:
            mim=l
    ans.append(mim)
    if max(ans)==H+W:
        ans2=0
    else:
        ans2=max(ans)
print(ans2)

Submission status: Runtime Error

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

white = []
black =[]
for i in range(H):
    for j in range(W):
        if A_input[i][0][j] =='.':
            white.append((i,j))
        else:
            black.append((i,j))

m = [100000000 for i in range(len(white))]
for i in range(len(white)):
    r = white[i]
    for B in black:
        d = abs(r[0]-B[0])+abs(r[1]-B[1])
        if d<m[i]:
            m[i] = d
ans = max(m)
print(ans)

Submission status: Runtime Error

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

def dfs(i, j):
  if i < 0 or i > H - 1 or j < 0 or j > W - 1 or memo[i][j] == True:
    return float("inf")
  if A[i][j] == "#":
    return 1

  memo[i][j] = True
  return min(dfs(i - 1, j), dfs(i + 1, j), dfs(i, j - 1), dfs(i, j + 1)) + 1

ans = 0
for i in range(H):
  for j in range(W):
    if A[i][j] == ".":
      memo = [[False for _ in range(W)] for _ in range(H)]
      ans = max(ans, dfs(i, j))

print(ans - 1)

Submission status: Runtime Error

Solution code:
dx = [-1,0,1,0]
dy = [0,-1,0,1]
acc = [['1']*W]*H
black = []
count = 0
for i in range(H):
    for j in range(W):
        if(A[i][j]=='#'):
            black.append([i,j])
            A[i][j] = '1'
while(1):
    if(acc==A):
        print(count)
        break
    else:
        for p in black:
            for i in range(4):
                nx,ny = p[0]+dx[i],p[1]+dy[i]
                if(0<=nx and nx<=H-1 and 0<=ny and ny<=W-1):
                    if(A[nx][ny]=='.'):
                        A[nx][ny] = '#'
        for i in range(H):
            for j in range(W):
                if(A[i][j]=='#'):
                    black.append([i,j])
                    A[i][j] = '1'
        count += 1

Submission status: Runtime Error

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

tile = []

for i in range(H):
    tile.append(list(input()))

ans = 0

def check_black(arr, H, W):
    for i in range(H):
        for j in range(W):
            if arr[i][j] != "#":
                return 0
    return 1

def black_list(arr, H, W):
    black_arr = []
    for i in range(H):
        for j in range(W):
            if arr[i][j] == "#":
                black_arr.append([i,j])
    return black_arr

while(check_black(tile,H,W) == 0):
    black_arr = black_list(tile, H, W)
    for i in range(len(black_arr)):
        if 0 < black_arr[i][0] and tile[black_arr[i][0]-1][black_arr[i][1]] != "#":
            tile[black_arr[i][0]-1][black_arr[i][1]] = "#"
        if black_arr[i][0] < H - 1 and tile[black_arr[i][0]+1][black_arr[i][1]] != "#":
            tile[black_arr[i][0]+1][black_arr[i][1]] = "#"
        if 0 < black_arr[i][1] and tile[black_arr[i][0]][black_arr[i][1]-1] != "#":
            tile[black_arr[i][0]][black_arr[i][1]-1] = "#"
        if black_arr[i][1] < H - 1 and tile[black_arr[i][0]][black_arr[i][1]+1] != "#":
            tile[black_arr[i][0]][black_arr[i][1]+1] = "#"
    ans+=1
    #print("rep = " + str(ans))
    #for x in range(H):
    #    for y in range(W):
    #        print(tile[x][y],end="")
    #    print("")

print(ans)

Submission status: Runtime Error

Solution code:

h,w=map(int,input().split())
a=[0]*h
for j in range(h):
    a[j]=input()
b=[0]*h*w
for j in range(h):
    for k in range(w):
        p=0
        q=0
        while a[j+p][k]== "."or a[j][k+q]=="."
            p+=1
            q+=1
        b[j*h+k]=min(p,q)
ans=min(b)
print(ans)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
l = []
a = [input() for j in range(w)]
for i in range(h):
    for j in range(w):
        if a[j][i] == '#':
            l.append((i,j))

answer = 0
for i in range(h):
    for j in range(w):
        if a[j][i] == '.':
            d = h*w
            for b in l:
                d1 = abs(j-b[0])+abs(i-b[1])
                d = min(d1, d)
                #print("d d1:",d,d1, "i j:",i,j,"l:",b)
            answer = max(answer, d)
print(answer)

Submission status: Runtime Error

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

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

#print (lst)
INF = 10 ** 9
#dp =[[INF] * W for _ in range(H)]

def bfs1(i, j): #右下向き
#    print ('i, j= ', i, j)
    if i < 0 or i == W or j < 0 or j == H:
        return 0
    elif lst[i][j] == '#':
        return 0
    else:
        return min(bfs1(i+1, j)+1, bfs1(i, j+1)+1)

def bfs2(i, j): #左上向き
#    print ('i, j= ', i, j)
    if i < 0 or i == W or j < 0 or j == H:
        return 0
    elif lst[i][j] == '#':
        return 0
    else:
        return min(bfs2(i-1, j)+1, bfs2(i, j-1)+1)


ans1 = 0
ans2 = 0
for x in range(W):
    for y in range(H):
        ans1 = max(ans1, bfs1(x,y))
#        print (ans1)
        ans2 = max(ans2, bfs2(W-1-x, H-1-y))

#print (ans1)
#print (ans2)

#print ('ans= ', min(ans1, ans2))
print (min(ans1, ans2))

Submission status: Runtime Error

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

bl = []
for i in range(h):
    s = input()
    for j in range(w):
        if s[j] == '#':
            bl.append([i,j])
mx = []
for i in h:
    for k in w:
        mn = [abs(j[0]-i)+abs(j[1]-k) for j in bl]
        mx.append(min(mn))
print(max(mx))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
a = [list(input()) for i in range(H)]
tmp = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            tmp.append(str(i) + str(j))
ans = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '.':
            tmp2 = []
            for k in tmp:
                tmp2.append(abs(int(k[0]) - i) + abs(int(k[1]) - j))
            ans.append(min(tmp2))
print(max(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
H,W = input().split()
H = int(H)
W = int(W)
s = [input() for i in range(H)]
text = s[1]
HW = np.zeros([H,W])
for i in range(H):
  for j in range(W):
    if s[i][j] == '.': #白
      HW[i,j] = 0
    else:
      HW[i,j] = 1 #黒

k = HW.sum()
HW_alpha = np.zeros([H+2,W+2])
HW_alpha[1:-1,1:-1]=HW
num = 0
while k != H*W:
  for i in range(H):
    for j in range(W):
      if HW[i,j] == 1:
        HW_alpha[i,j+1] = 1
        HW_alpha[i+2,j+1] = 1
        HW_alpha[i+1,j] = 1
        HW_alpha[i+1,j+2] = 1
  HW=HW_alpha[1:-1,1:-1]
  num = num +1
  k = HW.sum()
print(num)

Submission status: Runtime Error

Solution code:
from sys import stdin

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


Room_next = [["." for j in range(N[1])] for i in range(N[0])]
count = 0


def darkcount(array):
    p = 0
    for i in range(N[0]):
        for j in range(N[1]):
            if array[i][j] == "#":
                p += 1
    return p


for k in range(N[0] + N[1]):
    if darkcount(Room) >= N[0] * N[1]:
        count = k
        break
    for i in range(N[0]):
        for j in range(N[1]):
            if Room[i][j] == "#":
                Room_next[i][j] = "#"
                if i != 0:
                    Room_next[i - 1][j] = "#" 
                if i != N[0] - 1:
                    Room_next[i + 1][j] = "#" 
                if j != 0:
                    Room_next[i][j - 1] = "#" 
                if j != N[1] - 1:
                    Room_next[i][j + 1] = "#" 
                    
    for i in range(N[0]):
        for j in range(N[1]):
            Room[i][j] = Room_next[i][j]
    Room_next = [["." for i in range(N[0])] for j in range(N[1])]


print(count)

Submission status: Runtime Error

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

lists = []
iti_h = []
iti_w = []
cnt = 0

for i in range(h):
    s = list(input())
    lists.append(s)
    

while True:
    check = 0
    for i in range(h):
        for j in range(w):
            if lists[i][j] == "#":
                iti_h.append(i)
                iti_w.append(j)
                check += 1
    if check == h * w:
        break
    cnt += 1
    for i in range(check):
        if iti_h[i] - 1 >= 0:
            lists[iti_h[i] - 1][iti_w[i]] = "#"
        if iti_w[i] - 1 >= 0:
            lists[iti_h[i]][iti_w[i] - 1] = "#"
        if iti_w[i] + 1 <= w - 1:
            lists[iti_h[i]][iti_w[i] + 1] = "#"
        if iti_h[i] + 1 <= h - 1:
            lists[iti_h[i] + 1][iti_w[i]] = "#"
    iti_h.clear()
    iti_w.clear()
    


print(cnt)

Submission status: Runtime Error

Solution code:
import copy
import numpy as np
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 np.all(np.array(a)[1 : h + 1,1 : w + 1] == '#'):
    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(H):
    A.append(list(input()))
    
black=[]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            black.append([i,j])
            
white=[]
for i in range(H):
    for j in range(W):
        if A[i][j]==".":
            white.append([i,j])

ans=[]
for w in white:
    mim=H+W
    for b in black:
        x=abs(b[0]-w[0])
        y=abs(b[1]-w[1])
        l=int(x+y)
        if l<mim:
            mim=l
    ans.append(mim)
print(max(ans))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-                                                                                  
import sys, copy                                                                                         
from collections import deque                                                                            
# ----------------------------------------------------------------                                       
# Use Solve Function                                                                                     
                                                                                                         
def solve(lines):                                                                                        
    h,w = map(int, lines.pop(0).split(' '))                                                              
    NLIST = []                                                                                           
    for i in range(h):                                                                                   
        _w = list(lines.pop(0))                                                                          
        NLIST.append(_w)                                                                                 
                                                                                                         
    queue = deque([])                                                                                    
    next_queue = deque([])                                                                               
    Vector = [(1,0),(0,1),(-1,0),(0,-1)]                                                                 
                                                                                                         
    for i in range(h):                                                                                   
        for j in range(w):                                                                               
            c = NLIST[i][j]                                                                              
            if c == '#':                                                                                 
                next_queue.append( (i, j) )                                                              
    res = 0                                                                                              
    while( len(next_queue) > 0 ):                                                                        
        queue = copy.copy(next_queue)                                                                    
        next_queue = deque([])                                                                           
        flag = False                                                                                     
        while( len(queue) > 0 ):                                                                         
            p = queue.popleft()                                                                          
            for v in Vector:                                                                             
                nx, ny = [ x + y for (x, y) in zip(p, v)]                                                
                if (nx >= 0 and nx < w and ny >= 0 and ny < h):                                          
                    if ( NLIST[nx][ny] == '.' ):                                                         
                        next_queue.append( (nx, ny) )                                                    
                        NLIST[nx][ny] = '#'                                                              
                        flag = True                                                                      
        if flag:                                                                                         
            res += 1                                                                                     
    print(res)                                                                                           
                                                                                                         
lines = [x.strip() for x in sys.stdin.readlines()]                                                       
solve(lines)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
A = []
B_place = []
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=="#":
      B_place.append([i,w])
  A.append(a)

all_map = [[10**10 for i in range(W)] for i in range(H)]
def f(h,w,cou):
  global H,W,all_map
  if h<0 or H<=h or w<0 or W<=w:
    return
  if cou>=all_map[h][w]:
    return
  else:
    all_map[h][w]=cou
  f(h-1,w,cou+1)
  f(h+1,w,cou+1)
  f(h,w-1,cou+1)
  f(h,w+1,cou+1)

for B in B_place:
  f(B[0],B[1],0)

ans = 0
for i in range(H):
  ans = max(ans,max(all_map[i]))

print(ans)

Submission status: Runtime Error

Solution code:
a=input().split()
n = int(a[0])
m = int(a[1])
white=[]
black=[]
b = []
for i in range(n):
    b.append(input())

for i in range(n):
    for j in range(m):
        if b[i][j] == '.':
            white.append([i,j])
        else:
            black.append([i,j])

min_d=[]
max_d=[]
l_white =  len(white)
l_black = len(black)
for i in range(l_white):
    min_d=[]
    for j in range(l_black):
        min_d.append(abs(white[i][0] - black[j][0]) + abs(white[i][1] - black[j][1]))
    max_d.append(min(min_d))

print(max(max_d))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
#A
import numpy as np
import math
import re

a,b = map(int,input().split())

c = [list(map(str,input().split()))for i in range(a)]
d = [[0 for j in range(b)]  for i in range(a)]
e = [[0 for j in range(b)]  for i in range(a)]#コピー用
#print(c,d,e)

for i in range(a):#初期のリスト設定
    for j in range(b):
        if c[i][0][j] == '#':
            d[i][j] = 1
            e[i][j] = 1
#print(c,d,e)

def check(list,a,b):#全て1であるかの判定
    for i in range(a):
        for j in range(b):
            if list[i][j] == 0:
                return False
    return True

def copy(list1,list2,a,b):
    for i in range(a):
        for j in range(b):
            if list2[i][j] == 1:
                list1[i][j] = 1


count = 0
while not check(d,a,b):
    count += 1
    for i in range(a):
        for j in range(b):
            if d[i][j] == 1:
                if i != 0:#上があるなら
                    e[i-1][j] = 1
                if j != 0:#左があるなら
                    e[i][j-1] = 1
                if i != a-1:#下があるなら
                    e[i+1][j] = 1
                if j != a-1:#右があるなら
                    e[i][j+1] = 1
    copy(d,e,a,b)
    check(d,a,b)
print(count)

Submission status: Runtime Error

Solution code:

h,w=map(int,input().split())
a=[0]*h
for j in range(h):
a[j]=input()
b=[0]*h*w
for j in range(h):
for k in range(w):
p=0
q=0
while a[j+p][k]==“. “ or a[j][k+q]==“.”
p+=1
q+=1
b[j*h+k]=min(p,q)
ans=min(b)
print(ans)

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split()))
A = [input() for i in range(H)]
INF = 10**20
board = [[0]*(W+2)] + [[0]+[INF for i in range(H)]+[0] for j in range(W)] + [[0]*(W+2)]
queue = []
ans = 0

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

while len(queue):
    i, j = queue.pop(0)
    n = board[i][j]+1
    for i_, j_ in [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]:
        if board[i_][j_] == INF and (i_, j_) not in queue:
            board[i_][j_] = n
            queue.append((i_, j_))
            ans = max(ans, n)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

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


Room_next = [["." for i in range(N[0])] for j in range(N[1])]
count = 0


def darkcount(array):
    p = 0
    for i in range(N[0]):
        for j in range(N[1]):
            if array[i][j] == "#":
                p += 1
    return p


for k in range(N[0] + N[1]):
    if darkcount(Room) >= N[0] * N[1]:
        count = k
        break
    for i in range(N[0]):
        for j in range(N[1]):
            if Room[i][j] == "#":
                Room_next[i][j] = "#"
                if i != 0:
                    Room_next[i - 1][j] = "#" 
                if i != N[0] - 1:
                    Room_next[i + 1][j] = "#" 
                if j != 0:
                    Room_next[i][j - 1] = "#" 
                if j != N[1] - 1:
                    Room_next[i][j + 1] = "#" 
                    
    for i in range(N[0]):
        for j in range(N[1]):
            Room[i][j] = Room_next[i][j]
    Room_next = [["." for i in range(N[0])] for j in range(N[1])]


print(count)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
lst = []
aaa = True
for h in range(H):
    l = [s for s in input()]
    if '.' in l:
        aaa = False
    lst.append(l)

if aaa:
    print(0)
    exit(0)

def hoge(i, j):
    if i > 0 and lst[i - 1][j] != '#':
        lst[i - 1][j] = '##'
    if i + 1 < W and lst[i + 1][j]  != '#':
        lst[i + 1][j] = '##'
    if j > 0 and lst[i][j - 1] != '#':
        lst[i][j - 1] = '##'
    if j + 1 < H and lst[i][j + 1] != '#':
        lst[i][j + 1] = '##'

def hoge2():
    cnt = 0
    for j, h in enumerate(lst):
        for i, w in enumerate(h):
            if lst[i][j] == '##':
                lst[i][j] = '#'
            elif lst[i][j] == '.':
                cnt += 1
    return cnt

cnt = 100
ret = 0
while cnt > 0:
    for j, h in enumerate(lst):
        for i, w in enumerate(h):
            if lst[i][j] == '#':
                hoge(i, j)
    ret += 1
    cnt = hoge2()

print(ret)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def is_full(square):
    return all(all(map(lambda x: x == '#', line)) for line in square)

def main():
    h, w = map(int, input().split())
    square = [input().split() for i in range(h)]

    count = 0
    while not is_full(square):
        loop_list = [(i, j,) for i in range(h) for j in range(w) if square[i][j] == '#']
        count += 1
        for i, j in loop_list:
            # 上
            if i > 0 and square[i-1][j] == '.':
                square[i-1][j] = '#'
            # 左
            if j > 0 and square[i][j-1] == '.':
                square[i][j-1] = '#'
            # 下
            if i < h-1 and square[i+1][j] == '.':
                square[i+1][j] = '#'
            # 右
            if j < w-1 and square[i][j+1] == '.':
                square[i][j+1] = '#'
    print(count)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from heapq import*
import sys;sys.setrecursionlimit(200000)
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
for x in range(1,1+h):
    for y in range(1,1+w):
        if grid[y][x]=="#":
            heappush(lock,(y,x))
#print(lock)


while 1:
    locked=[]
    for y,x in lock:
        if grid[y][x]=='#':
            for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                if grid[y+i][x+j]!="#":
                    if (y+i,x+j)in locked:
                        continue
                    else:
                        grid[y+i][x+j]="#"
                        heappush(locked,(y+i,x+j))
    g+=1
    lock=locked
#    print(lock)
#    for i in grid:
#        print(*i)
#    print()
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

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


def fillDistance(d, h, w, cd):
    if h < 0 or H <= h or w < 0 or W <= w:
        return
    if cd < d[h][w]:
        d[h][w] = cd
        fillDistance(d, h-1, w, cd+1)
        fillDistance(d, h+1, w, cd+1)
        fillDistance(d, h, w-1, cd+1)
        fillDistance(d, h, w+1, cd+1)



#d = [[10000 for j in range(W)] for i in range(H)]
d = []
for h in range(H):
    d.append([])
    for w in range(W):
        d[h].append(10000)
#print(d)
for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            fillDistance(d, h, w, 0)
#print(d)
ans = -1
for h in range(H):
    for w in range(W):
        if d[h][w] > ans:
            ans = d[h][w]
print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
a = [list(input()) for i in range(H)]
tmp = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            tmp.append(str(i) + str(j))
ans = []
for i in range(H):
    for j in range(W):
        if a[i][j] == '.':
            tmp2 = [[]]
            for k in tmp:
                tmp2[0].append(abs(int(k[0]) - i) + abs(int(k[1]) - j))
            ans.extend(tmp2)
ans2 = []
for i in ans:
    ans2.append(min(i))
print(max(ans2))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

h,w = map(int,input().split())
maps  =[ 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)
            #print(x,y,dis)
            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)]
        dis = 0
        a = []
        f(i,j,i,j,a)
        a = min(a)
        #print(a,i,j)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

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

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

    index_i=[]
    index_j=[]
    for i in range(0,a):
        for j in range(0,b):
            if(matrix[i][j]=="#"):
                index_i.append(i)
                index_j.append(j)
    
    for i in range(0,len(index_i)):
        
        x=index_i[i]
        y=index_j[i]
        new[x][y]="#"
        if(x==0 and y==0):
            new[x+1][y]="#"
            new[x][y+1]="#"
        elif(x==a-1 and y==b-1):
            new[x-1][y]="#"
            new[x][y-1]="#"
        elif(y==b-1):
            new[x-1][y]="#"
            new[x+1][y]="#"
            new[x][y-1]="#"
        elif(x==0):
            new[x-1][y]="#"
            new[x+1][y]="#"
            new[x][y+1]="#"
        elif(y==0):
            new[x-1][y]="#"
            new[x][y-1]="#"
            new[x][y+1]="#"
        elif(x==a-1):
            new[x-1][y]="#"
            new[x][y-1]="#"
            new[x][y+1]="#"
        else:
            new[x-1][y]="#"
            new[x+1][y]="#"
            new[x][y-1]="#"
            new[x][y+1]="#"
    return new



a,b=(int(x) for x in input().split())
matrix=[]
for i in range(0,b):
    x=input()
    col=[]
    for j in x:
        col.append(j)
    matrix.append(col)  
count=0
while(check(a,b,matrix)!=1):
    matrix=new_matrix(a,b,matrix)
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
import copy

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))

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

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

print(cnt)