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 = []
posi=[]
cout=0
for i in range(H):
    road=input()
    #a.append(list(road))
    for k in range(len(road)):
      a.append(road[k])
      if road[k]=='#':
            cout+=1
            position=(i,k)
            posi.append(position)


def sharp_checker(x_posi,y_posi):
    if a[x_posi][y_posi]=='.':
        index=(x_posi,y_posi)
        list_indexs.append(index)
        a[x_posi][y_posi]='#'
        #print(list_indexs)
        return 1
    else:
        return 0


def change():
    add_count=0
    for index in range(len(posi)):
        #print("hello")
        x=posi[index][0]
        y=posi[index][1]
        if x==0:
            if y==0:
                add_count+=sharp_checker(x+1,y)
                add_count+=sharp_checker(x,y+1)
            elif y==W-1:
                add_count+=sharp_checker(x+1,y)
                add_count+=sharp_checker(x,y-1)
            else:
                add_count+=sharp_checker(x+1,y)
                add_count+=sharp_checker(x,y-1)
                add_count+=sharp_checker(x,y+1)
        elif x==H-1:
            if y==0:
                add_count+=sharp_checker(x-1,y)
                add_count+=sharp_checker(x,y+1)
            elif y==W-1:
                add_count+=sharp_checker(x-1,y)
                add_count+=sharp_checker(x,y-1)
            else:
                add_count+=sharp_checker(x-1,y)
                add_count+=sharp_checker(x,y+1)
                add_count+=sharp_checker(x,y-1)
        elif y==0:
            add_count+=sharp_checker(x-1,y)
            add_count+=sharp_checker(x+1,y)
            add_count+=sharp_checker(x,y+1)
        elif y==W-1:
            add_count+=sharp_checker(x-1,y)
            add_count+=sharp_checker(x+1,y)
            add_count+=sharp_checker(x,y-1)
        else:
            add_count+=sharp_checker(x-1,y)
            add_count+=sharp_checker(x,y+1)
            add_count+=sharp_checker(x+1,y)
            add_count+=sharp_checker(x,y-1)
    return(add_count,list_indexs)

ans=0
while cout< H*W:
    list_indexs=[]
    add,posi=change()
    cout+=add
    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
h ,w = map(int, input().split())
Ahw = []
for i in range(h):
    Ahw.append([str(x) for x in input().split()])
ahwnm = np.array(Ahw)

count = 0
for col in range(h-1):
    for line in range(w-1):
        if ahwnm[col,line] == "#" and (ahwnm[col+1,line] or ahwnm[col-1,line] or ahwnm[col,line+1] or ahwnm[col,line-1] == "."):
            ahwnm[col+1,line] and ahwnm[col-1,line] and ahwnm[col,line+1] and ahwnm[col,line-1] == "#"
            #ahwnm[col+1,line] == "#"
            print(col, line)
        else:
            pass
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
A=[]
B=[0]*(H*W)
C=[0]*(H*W)
n=0
for _ in range(H):
  A.append(list(input()))
  for __ in range(W):
    if A[_][__]=="#":
      B[n]= _
      C[n]= __
      n+=1
del B[n:]
del C[n:]
ANS = []
for _ in range(H):
  for __ in range(W):
    temp = H*W
    for ___ in range(n):
      if A[_][__]==".":
        if temp>abs(B[___]-_)+abs(C[___]-__):
          temp = abs(B[___]-_)+abs(C[___]-__)
        if ___ == n-1:
          ANS.append(temp)
print(max(ANS))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[190]:


import numpy as np
import copy


# In[196]:


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


# In[197]:


B = np.full((H+2, W+2), '.')
B[1:-1, 1:-1] = A
#C = copy.deepcopy(B)
count = 0

while np.all(B[1:-1, 1:-1] == '#') == False:
    count += 1
    for index in np.stack([np.where(B == '#')[0], np.where(B == '#')[1]]).T:
        B[index[0]-1, index[1]] = '#'
        B[index[0]+1, index[1]] = '#'
        B[index[0], index[1]-1] = '#'
        B[index[0], index[1]+1] = '#'     
        #print(C)
    B[0,:] = '.'
    B[-1,:] = '.'
    B[:,0] = '.'
    B[:,-1] = '.'
    #print(C)
    #B = C[:,:]
    #print(count)
print(count)


# In[ ]:

Submission status: Runtime Error

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

A = [str(input()) for i in range(H)]

def next_black(D, i, j, c):
    if c >= D[i][j]:
        return D
    else:
        D[i][j] = c
        if i + 1 < H:
            next_black(D, i + 1, j, c + 1)
        if i - 1 >= 0:
            next_black(D, i - 1, j, c + 1)
        if j + 1 < W:
            next_black(D, i, j + 1, c + 1)
        if j - 1 >= 0:
            next_black(D, i, j - 1, c + 1)
    
    return D

max_count = H + W -2
B = [[max_count for i in range(W)] for i in range(H)]

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

ans = 0
for i in B:
    if max(i) >= ans:
        ans = max(i)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

inf = float('inf')
H, W = map(int, input().split())
hashmap = defaultdict(set)
for i in range(H):
  line = input()
  for j in range(len(line)):
    if line[j] == '#':
      hashmap[0].add((i, j))
    else:
      hashmap[inf].add((i, j))

print(max([min([abs(i-h)+abs(w-j) for h, w in hashmap[0]]) for i,j in hashmap[inf]]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
A=[list(input()) for i in range(h)]
B=set()
for i in range(w):
  for j in range(h):
    if A[i][j]=="#":
      B.add((i,j))
count=0
while len(B)<h*w:
  C=set()
  for b in B:
    if b[0]>0:
      C.add((b[0]-1,b[1]))
    if b[1]<w-1:
      C.add((b[0],b[1]+1))
    if b[0]<h-1:
      C.add((b[0]+1,b[1]))
    if b[1]>0:
      C.add((b[0],b[1]-1))
  B=B.union(C)
  count+=1
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

import sys
sys.setrecursionlimit(1000000000)

h,w = map(int,input().split())
maps  = np.array([ 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.add(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 = set()
        f(i,j,i,j,a)
        a = min(a)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
h,w=map(int,input().split())
L=[]
for i in range(h):
    L.append(list(input()))
dy_dx=[[0,1],[0,-1],[1,0],[-1,0]]
que=deque([])
for i in range(h):
    for j in range(w):
        if L[i][j]=="#":
            L[i][j]=0
        else:
            L[i][j]=-1
        if L[i][j]!=-1:
            que.append([i,j])
arg_j=0
cnt=0
while len(que)>0:
    cnt+=1
    for j in range(h):
        for k in range(w):
            if L[j][k]==cnt-1:
                for i in range(4):
                    if 0<=j+dy_dx[i][0]<=h-1 and 0<=k+dy_dx[i][1]<=w-1:
                        if L[j+dy_dx[i][0]][k+dy_dx[i][1]]==-1:
                            L[j+dy_dx[i][0]][k+dy_dx[i][1]]=cnt
                            if not [j+dy_dx[i][0],k+dy_dx[i][1]] in que:
                                que.append([j+dy_dx[i][0],k+dy_dx[i][1]])
                                cur=que.popleft()
                                arg_i=j+dy_dx[i][0]
                                arg_j=k+dy_dx[i][1]
    if cnt>=1001:
        break


print(L[arg_i][arg_j])

Submission status: Runtime Error

Solution code:
# input
h, w = [int(i) for i in input().split()]
ary = []
for y in range(h):
    line = input()
    ary.append(line)

pass


max_val = pow(10, 6)

def convert_ary(ary):
    # #. -> 0, 1, 2
    tmp = [[] for i in range(w)]
    for y in range(h):
        for x in range(w):
            tmp[y].append(None if ary[y][x] == '.' else 0)

    return tmp

ary = convert_ary(ary)

def upd_left(ary, y, x):
    if x == 0:
        return ary
    if ary[y][x-1] is None:
        ary[y][x-1] = ary[y][x] + 1
    else:
        ary[y][x-1] = min(ary[y][x-1], ary[y][x] + 1)
    return ary

def upd_right(ary, y, x):
    if x == w - 1:
        return ary
    if ary[y][x+1] is None:
        ary[y][x+1] = ary[y][x] + 1
    else:
        ary[y][x+1] = min(ary[y][x+1], ary[y][x] + 1)
    return ary

def upd_up(ary, y, x):
    if y == 0:
        return ary
    if ary[y-1][x] is None:
        ary[y-1][x] = ary[y][x] + 1
    else:
        ary[y-1][x] = min(ary[y-1][x], ary[y][x] + 1)
    return ary

def upd_down(ary, y, x):
    if y == h - 1:
        return ary
    if ary[y+1][x] is None:
        ary[y+1][x] = ary[y][x] + 1
    else:
        ary[y+1][x] = min(ary[y+1][x], ary[y][x] + 1)
    return ary

# from copy import deepcopy

def update_ary(ary):
    # tmp = deepcopy(ary)
    for y in range(h):
        for x in range(w):
            if ary[y][x] is None:
                continue
            ary = upd_left(ary, y, x)
            ary = upd_right(ary, y, x)
            ary = upd_up(ary, y, x)
            ary = upd_down(ary, y, x)
    return ary

def is_complete(ary):
    for line in ary:
        if None in line:
            return False
    return True

while not is_complete(ary):
    ary = update_ary(ary)

print(max(elem for line in ary for elem in line))

Submission status: Runtime Error

Solution code:
import copy

h, w = map(int, input().split())
a = [h+1]
ax = [h+2]

check_lis = [h+1]
check=['#' for i in range(w)]

for i in range(h):
    a.append(list(input()))
    check_lis.append(check)

for i in range(h+1):
    ax.append(a[i])

# print(ax)
ax = copy.deepcopy(a)

a.pop(0)
ax.pop(0)
check_lis.pop(0)

flag=0

#print(a)
#print(ax)
#print(check_lis)


while True:
    if ax != check_lis:
        for i in range(h):
            for j in range(w):
                if a[i][j]=="#":
                    if i-1>=0:
                        ax[i-1][j]='#'
                    if i+1<=w-1:
                        ax[i+1][j]='#'
                    if j-1>=0:
                        ax[i][j-1]='#'
                    if j+1<=h-1:
                        ax[i][j+1]='#'
        a = copy.deepcopy(ax)
        #print('ax')
        #print(ax)
        flag = flag + 1
    else:
        break

print(flag)

Submission status: Runtime Error

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

for i in range(h):
    check1.append([])
    A = list(input())
    for j in range(w):
        a = A[j]
        if a == "#":
            check1[i].append(1)
        else:
            check1[i].append(pow(10, 5))



#print(check1)
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
h ,w = map(int, input().split())
Ahw = []
for i in range(h):
    Ahw.append([str(x) for x in input().split()])
    
ahwnm = np.array(Ahw)
count = 0
for col in range(h-1):
    for line in range(w-1):
        if ahwnm[col,line] == "#":
            ahwnm[col+1,line] and ahwnm[col-1,line] and ahwnm[col,line+1] and ahwnm[col,line-1] == "#"

        else:
            pass
    count+=1
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([ 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.add(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 = set()
        f(i,j,i,j,a)
        a = min(a)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
H, W = tuple(map(int, input().split()))
P = [[-int(i) for i in input().rstrip().replace("#", "0").replace(".", "1")]
     for i in range(H)]

queue_set = set()
for h in range(H):
    for w in range(W):
        if P[h][w] == 0:
            for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if 0 <= h+dh < H and 0 <= w+dw < W and P[h+dh][w+dw] == -1:
                    queue_set.add((h+dh)*W+w+dw)
        elif h == 0 or h == H-1 or w == 0 or w == W-1:
            queue_set.add(h*W+w)
m = 0

queue = [[q//W, q % W, 0] for q in queue_set]
idx = 0
while len(queue) != idx+1:
    h, w, cnt = queue[idx]
    idx += 1
    if P[h][w] == -1:
        P[h][w] = cnt+1
        a = 0
        for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            if 0 <= h+dh < H and 0 <= w+dw < W:
                a = P[h+dh][w+dw] if P[h+dh][w+dw] > a else a
                if P[h+dh][w+dw] == -1:
                    queue += [[h+dh, w+dw, cnt+1]]

        m = m if cnt+1 + a < m else cnt+1 + a

print(m)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

import copy

"""
    O(max_count*H*W)
    1 ≦ H , W ≦ 1000

    (1000+1000) * 10^3 * 10^3 
    = 10^9
    = 非常にシンプルな実装でなくては厳しい
"""

X = [1,  0, -1,  0]
Y = [0,  1,  0, -1]

# input
l = list(map(int, input().split()))
H, W = l[0], l[1]

grid = list()
for i in range(H):
    l = input()
    grid.append(list(l))


# Process
def dfs(x, y, next_grid, change):
    for ix, iy in zip( X, Y ):
        if x + ix >= 0 and y + iy >= 0 and x + ix < W and y + iy < H:
            if grid[x + ix][y + iy] == '.':
                change = True
                next_grid[x + ix][y + iy] = '#'
    return change


# main
count = 0
is_change = bool()

while(True):
    is_change = False
    next_grid = copy.deepcopy(grid)
    #print(next_grid)

    # Renew next_grid
    for ix in range(W):
        for iy in range(H):
            if grid[ix][iy] == '#':
                #print(grid)
                is_change = dfs(ix, iy, next_grid, is_change)

    #print("is_change", is_change)
    # Check
    if is_change == True:
        grid = copy.deepcopy(next_grid)
        count += 1
    else:
        break

    if count > 10:
        break

print(count)

Submission status: Runtime Error

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

A = [str(input()) for i in range(H)]

def next_black(D, i, j, c):
    if c >= D[i][j]:
        return D
    else:
        D[i][j] = c
        if i + 1 < H:
            next_black(D, i + 1, j, c + 1)
        if i - 1 >= 0:
            next_black(D, i - 1, j, c + 1)
        if j + 1 < W:
            next_black(D, i, j + 1, c + 1)
        if j - 1 >= 0:
            next_black(D, i, j - 1, c + 1)
    
    return D

max_count = H + W -2
B = [[max_count for i in range(W)] for i in range(H)]

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

ans = 0
for i in B:
    if max(i) >= ans:
        ans = max(i)

print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
black = []
white = []

for i in range(H):
  a = list(input())
  for j in range(W):
    if (a[j] == '#'):
      black.append([i, j])
    else:
      white.append([i, j])


ans = []

for w in white:
  m = 10 ** 6
  for b in black:
    m = min(m, abs(b[0] - w[0]) + abs(b[1] - w[1]))
  ans.append(m)

print(max(ans))

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.add(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 = set()
        f(i,j,i,j,a)
        a = min(a)
        ans = max(a,ans)

print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
dp = [[10**8]*W+[0] for _ in range(H)]+[[0]*(W+1)]
pre = []
rest = [[i,j] for i in range(H) for j in range(W)]
for i in range(H):
    for j,st in enumerate(input()):
        if st == '#':
            dp[i][j]=0
            pre.append([i,j])
ans = 0
while len(pre) > 0:
    ans+=1
    for i in range(len(pre)):
        p = pre.pop()
        if dp[p[0]+1][p[1]] < dp[p[0]][p[1]]+1:
            pre.append([p[0]+1,p[1]])
            dp[p[0]+1][p[1]] = dp[p[0]][p[1]]+1
        if dp[p[0]-1][p[1]] < dp[p[0]][p[1]]+1:
            pre.append([p[0]-1,p[1]])
            dp[p[0]-1][p[1]] = dp[p[0]][p[1]]+1
        if dp[p[0]][p[1]+1] < dp[p[0]][p[1]]+1:
            pre.append([p[0],p[1]+1])
            dp[p[0]][p[1]+1] = dp[p[0]][p[1]]+1
        if dp[p[0]][p[1]-1] < dp[p[0]][p[1]]+1:
            pre.append([p[0],p[1]-1])
            dp[p[0]][p[1]-1] = dp[p[0]][p[1]]+1
print(ans)

Submission status: Runtime Error

Solution code:

def darker(ary, i, j):
    ary[i][j] = '#'
    if i > 0:
        ary[i - 1][j] = '#'
    if i < W-1:
        ary[i + 1][j] = '#'
    if j > 0:
        ary[i][j - 1] = '#'
    if j < H-1:
        ary[i][j + 1] = '#'
    return ary

def clock():
    new_A = [['.' for i in range(W)] for j in range(H)]
    for i in range(W):
        for j in range(H):
            if A[i][j] == '#':
                new_A = darker(new_A, i, j)
    return new_A

def is_all_hash():
    for i in range(W):
        for j in range(H):
            if A[i][j] == ".":
                return False
    return True


H, W = map(int, input().split())
A = [ list(input()) for i in range(H)]
count = 0

while not is_all_hash():
    A = clock()
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
import copy
h,w=map(int,input().split())
a=[list(map(lambda x:0 if x=="#" else 1,input())) for i in range(h)]
b=[[0 for i in range(w)] for j in range(h)]
count=0
while a!=b:
  z=copy.deepcopy(a)
  for i in range(h):
    for j in range(w):
      if a[i][j]==1:
        a[i][j]=z[i][j]*(z[i-1][j] if i!=0 else 1)*(z[i+1][j] if i!=h-1 else 1)*(z[i][j-1] if j!=0 else 1)*(z[i][j+1] if j!=h-1 else 1)
  count+=1
print(count)

Submission status: Runtime Error

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

for i in range(H):
    word = a[i][0]
    for j in range(W):
        if word[j] == "#":
            A.append([i,j])
        else:
            B.append([i,j])

max = 0
for i in range(len(B)):
    x = B[i][0]
    y = B[i][1]
    min_temp = 0
    min = H * W
    for j in range(len(A)):
        min_temp = abs(x - A[k][0]) + abs(y - A[k][1])
        if min_temp <= min:
            min = min_temp
    if min >= max:
        max = min
print(max)

Submission status: Runtime Error

Solution code:

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

m = []
l = []
end = 0
for i in range(H):
    h = list(input())
    m.append(h)
    for j, w in enumerate(h):
        if w == '#':
            l.append([j, i])
            end += 1
HW = H * W
ans = 0
while (end < HW):
    nl = []
    for ll in l:
        if (0 < ll[0]):
            if (m[ll[0] - 1][ll[1]] == '.'):
                m[ll[0] - 1][ll[1]] = '#'
                end += 1
                nl.append([ll[0] - 1, ll[1]])

        if (ll[0] < W - 1):
            if (m[ll[0] + 1][ll[1]] == '.'):
                m[ll[0] + 1][ll[1]] = '#'
                end += 1
                nl.append([ll[0] + 1, ll[1]])

        if (0 < ll[1]):
            if (m[ll[0]][ll[1] - 1] == '.'):
                m[ll[0]][ll[1] - 1] = '#'
                end += 1
                nl.append([ll[0], ll[1] - 1])

        if (ll[1] < H - 1):
            if (m[ll[0]][ll[1] + 1] == '.'):
                m[ll[0]][ll[1] + 1] = '#'
                end += 1
                nl.append([ll[0], ll[1] + 1])
        l = nl
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import copy

h, w = map(int, input().split())
a = [h+1]
ax = [h+2]

check_lis = [h+1]
check=['#' for i in range(w)]

for i in range(h):
    a.append(list(input()))
    check_lis.append(check)

for i in range(h+1):
    ax.append(a[i])

# print(ax)
ax = copy.deepcopy(a)

a.pop(0)
ax.pop(0)
check_lis.pop(0)

flag=0
ax_flag=0

#print(a)
#print(ax)
#print(check_lis)


while True:
    if ax_flag==0:
        if ax != check_lis:
           for i in range(h):
                for j in range(w):
                    if ax[i][j]=="#":
                        if i-1>=0:
                            a[i-1][j]='#'
                        if i+1<=w-1:
                            a[i+1][j]='#'
                        if j-1>=0:
                            a[i][j-1]='#'
                        if j+1<=h-1:
                            a[i][j+1]='#'
           ax = copy.deepcopy(a)
           ax_flag=1
           #print('a')
           #print(a)
           flag = flag + 1
        else:
            break
    if ax_flag==1:
        if a != check_lis:
           for i in range(h):
                for j in range(w):
                    if a[i][j]=="#":
                        if i-1>=0:
                            ax[i-1][j]='#'
                        if i+1<=w-1:
                            ax[i+1][j]='#'
                        if j-1>=0:
                            ax[i][j-1]='#'
                        if j+1<=h-1:
                            ax[i][j+1]='#'
           a = copy.deepcopy(ax)
           ax_flag=0
           #print('ax')
           #print(a)
           flag = flag + 1
        else:
            break

print(flag)

Submission status: Runtime Error

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

for j in range(H):
    for i in range(W):
        if A[j][i].count("#"):
            B.append([i, j])

for j in range(H):
    for i in range(W):
        if A[j][i].count("."):
            near = 2000
            for b in range(len(B)):
                c = abs(j - B[b][0]) + abs(i - B[b][1])
                if c < near:
                    near = c
            D.append(near)

print(max(D))

Submission status: Runtime Error

Solution code:

def main():
    h, w = map(int, input().rstrip().split(' '))
    a = [None] * h
    b = set()
    whites = set()
    for i in range(h):
        a[i] = [None] * w
        for j, c in enumerate(input().rstrip()):
            if c == '#':
                a[i][j] = True
                b.add((i, j))
            else:
                a[i][j] = False
                whites.add((i, j))
    
    max_dist = 0

    while len(whites) > 0:
        i, j = whites.pop()
        min_dist = 1000000
        min_dist_black = None
        for black in b:
            p, q = black
            d = abs(p - i) + abs(q - j)
            if d < min_dist:
                min_dist = d
                min_dist_black = black
        # tweak to remove non farthest whites
        for a in range(min(i, min_dist_black[0]), max(i, min_dist_black[0]) + 1):
            for b in range(min(j, min_dist_black[1]), max(j, min_dist_black[1]) + 1):
                if (a, b) in whites:
                    whites.remove((a, b))

        if max_dist < min_dist:
            max_dist = min_dist

    for i in range(h):
        for j in range(w):
            if a[i][j]:
                continue

    
    print(min_dist)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:

def mostCloseDistance(matrix, x, y, N, M):
    count = 1
    while True:
        startX = x
        startY = y - count
        for c in range(count):
            if -1 < startX < N and -1 < startY < M:
                if matrix[startX][startY] == "#":
                    return count
            startX += 1
            startY += 1
        for c in range(count):
            if -1 < startX < N and -1 < startY < M:
                if matrix[startX][startY] == "#":
                    return count
            startX -= 1
            startY += 1
        for c in range(count):
            if -1 < startX < N and -1 < startY < M:
                if matrix[startX][startY] == "#":
                    return count
            startX -= 1
            startY -= 1
        for c in range(count):
            if -1 < startX < N and -1 < startY < M:
                if matrix[startX][startY] == "#":
                    return count
            startX += 1
            startY -= 1
        count += 1



def main():
    a = input().split()
    N = int(a[0])
    M = int(a[1])

    matrix = []
    for i in range(N):
        innerMatrix = []
        for j in input():
            innerMatrix.append(j)
        matrix.append(innerMatrix)

    max = 0
    for i in range(N):
        for j in range(M):
            if matrix[i][j] == ".":
                tmp = mostCloseDistance(matrix, i, j, M, N)
                if tmp > max:
                    max = tmp
    
    print(max)
 
main()

Submission status: Runtime Error

Solution code:
import queue

h,w=map(int, input().split())
a=[list(list(input())) for i in range(h)]
q = queue.Queue()
ans=0

for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            for dy, dx in ([0, -1], [-1, 0], [0, 1], [1, 0]):
                ny=i+dy
                nx=j+dx
                if ny<0 or ny>=h or nx<0 or nx>=w:
                    continue
                if a[ny][nx]=='.':
                    q.put([ny,nx])
while not q.empty():
    ans+=1
    l=[]
    while not q.empty():
        l.append(q.get())
    for e in l:
        a[e[0]][e[0]]='#'
        for dy, dx in ([0, -1], [-1, 0], [0, 1], [1, 0]):
            ny=e[0]+dy
            nx=e[1]+dx
            if ny<0 or ny>=h or nx<0 or nx>=w:
                continue
            if a[ny][nx]=='.':
                a[ny][nx]='#'
                q.put([ny,nx])
print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
a = list(input() for i in range(h))
inf = 10**8
b = [[inf for i in range(w)]for i in range(h)]

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

def func(x,y):
        for k in range(4):
                nx = x + dx[k]
                ny = y + dy[k]
                if 0 <= nx and nx<w and 0 <= ny and ny<h:
                        if b[x][y]+1<b[nx][ny]:
                                b[nx][ny] = b[x][y]+1
                                func(nx,ny)

for i in range(w):
        for j in range(h):
                if a[i][j] == "#":
                        b[i][j] = 0
for i in range(w):
        for j in range(h):
                if a[i][j] == "#":
                        func(i,j)

ans = 0
for i in range(w):
        for j in range(h):
                ans = max(ans,b[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = [["" for i in range(dep)] for j in range(wid)]
  for i in range(dep):
    s = input()
    for j in range(len(s)):
      a[i][j] = s[j]
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if a[i][j] == "#":
          continue
        
        if i > 0 and a[i-1][j] == "#":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#":
          a[i][j] = "#"
          flg = 1
    
    if(flg == 1):
      result += 1
    else:
      break
      
  print(result)
  
main()

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
s = [list(input()) for i in range(h)]
ans = 0

for i in range(h):
	for j in range(w):
		count = 2000
		if s[i][j] == '#':
			for l in range(h):
				for m in range(w):
					if s[l][m] == '.':
						s[l][m] = str(abs(i-l)+abs(j-m))
					elif s[l][m] != '#':
						s[l][m] = str(min(int(s[l][m]),abs(i-l)+abs(j-m)))
for i in s:
	ans = max(ans,int(max(i)))
print(ans)

Submission status: Runtime Error

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

def search(y,x):
    fx = x
    fy = y
    q = [(y,x)]
    while q != []:
        y,x = q.pop(0)
        for py,px in [(y-1,x),(y,x-1),(y+1,x),(y,x+1)]:
            if 0<=py<H and 0<= px < W:
                if A[py][px] == "#":
                    return abs(py-fy)+abs(px-fx)
                else:
                    q.append((py,px))
anss = []
for i in range(H):
    for j in range(W):
        if A[i][j] == ".":
            anss.append(search(i,j))
print(max(anss))

Submission status: Runtime Error

Solution code:
import copy
h,w=map(int,input().split())
a=[list(map(lambda x:0 if x=="#" else 1,input())) for i in range(h)]
b=[[0 for i in range(w)] for j in range(h)]
count=0
while a!=b:
  z=copy.deepcopy(a)
  for i in range(h):
    for j in range(w):
      a[i][j]=(z[i-1][j] if i!=0 else 1)*(z[i+1][j] if i!=h-1 else 1)*(z[i][j-1] if j!=0 else 1)*(z[i][j+1] if j!=h-1 else 1)
  count+=1
print(count)

Submission status: Runtime Error

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

A = [str(input()) for i in range(H)]

def next_black(D, i, j, c):
    if c >= D[j][i]:
        return D
    else:
        D[j][i] = c
        if i + 1 < W:
            next_black(D, i + 1, j, c + 1)
        if i - 1 >= 0:
            next_black(D, i - 1, j, c + 1)
        if j + 1 < H:
            next_black(D, i, j + 1, c + 1)
        if j - 1 >= 0:
            next_black(D, i, j - 1, c + 1)
    
    return D

max_count = H + W -2
B = [[max_count for i in range(W)] for i in range(H)]

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

ans = 0
for i in B:
    if max(i) >= ans:
        ans = max(i)

print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
A = [[i for i in input()] for _ in range(H)]
d = []
black = []
for i in range(H):
    for j in range(W):
        if(A[i][j]=='#'):
            black.append([i,j])
for i in range(H):
    for j in range(W):
        if(A[i][j]=='.'):
            d.append(min([abs(i-p[0])+abs(j-p[1]) for p in black]))
print(max(d))