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:
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)
        number.reverse()
        number2.append(number)


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)
number3.reverse()

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)
        number.reverse()
        number4.append(number)

number4.reverse()

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:
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 = 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:
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
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:
H, W = list(map(int, input().strip().split()))
A = list()

for _ in range(H):
    A.append(list(input().strip()))

flag = False
count = 0
white = 1

while white > 0:
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                if i == 0:
                    A[i+1][j] = '*'
                elif 0 < i < W - 1:
                    A[i-1][j] = '*'
                    A[i+1][j] = '*'
                elif i == W - 1:
                    A[i-1][j] = '*'
        A[i] = ''.join(A[i]).replace('.#.', '***').replace('.#', '**').replace('#.', '**')
        A[i] = list(A[i])

    white = 0
    for i in range(H):
        if '*' in A[i]:
            A[i] = ['#' if c == '*' else c for c in A[i]]
            if '.' in A[i]:
                white += 1
            flag = True

    if flag is True:
        count += 1

print(count)

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= 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:
            check_list.append([i-1,j])
          if (j-1)>=0:
            check_list.append([i,j-1])
          if (i+1)<=(W-1):
            check_list.append([i+1,j])
          if (j+1)<=(H-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:
#入力
R,C= map(int,input().split())
c= [list(input()) for _ in range(R)]
white=[]
black=[]
for i in range(R):
  for j in range(C):
    if c[i][j]=='.':
      white.append([i,j])
    else:
      black.append([i,j])
N=len(white)
M=len(black)

ans = []

for a, b in white:
    manh = 10**9
    num = 0
    for c, d in black:
        if abs(a - c) + abs(b - d) < manh:
            manh = abs(a - c) + abs(b - d)
            #num = black.index([c, d]) + 1
    ans.append(manh)
print(max(ans))

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)
        number.reverse()
        number2.append(number)


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)
number3.reverse()

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)
        number.reverse()
        number4.append(number)

number4.reverse()

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:
## 計算量が(10^6)^2あるから絶対TLEなるけど

from sys import stdin
import numpy as np
 
H, W = map(int,input().split())
board = [[int(c) for c in l.replace('#','1').replace('.','0').strip()] for l in stdin]
board = np.array(board)

## 黒が少ない時、それぞれの白について黒との最短距離を出すコード
board_black = np.where(board == 1)
n_black = len(board_black[0])
distance = []
for i in range(H):
  for j in range(W):
    if board[i,j] == 1:
      continue
    else:
      distance_ij = []
      for k in range(n_black):
        var = abs(board_black[0][k] - i) + abs(board_black[1][k] - j)
        distance_ij.append(var)
      distance.append(min(distance_ij))

ans = max(distance)
print(ans)
      

"""
## 題意の通り、黒マスを隣接へ広げていくコード
cnt = 0
while True:
  if np.all(board):
    ans = cnt
    break
  else:
    new_board_u = np.roll(board, -1, axis=0)
    new_board_u[-1,:] = 0
    new_board_d = np.roll(board, 1, axis=0)
    new_board_d[0,:] = 0
    new_board_l = np.roll(board, -1, axis=1)
    new_board_l[:,-1] = 0
    new_board_r = np.roll(board, 1, axis=1)
    new_board_r[:,0] = 0
    new_board = board + new_board_u + new_board_d + new_board_l + new_board_r
    board = new_board >= 1
    cnt += 1
    
print(ans)
"""

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)):
    k=[]
    l=[]
    m=[]
    for j in range(len(a1)):
        k.append(abs(a1[j]-a2[i]))
        l.append(abs(b1[j]-b2[i]))
        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:
import numpy as np
import copy
import sys

b, c = map(int, input().split())
K = np.zeros((b,c))
for i in range(b):
    s = input()
    #print(s)
    for j in range(c):
        if s[j] == "#" :
            K[i,j] =1
#print(K)
count = 0
while(1):
    count =count+1
    S = copy.deepcopy(K)
    S1 = np.vstack((S,np.zeros(b)))
    S1 = S1[1:,:]
    S2 = np.vstack((np.zeros(b),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:
h, w = map(int, input().split())

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

ans_list = []
for i in range(h):
    for j in range(w):
        min_dis = 10000000
        if a[i][j] == ".":
            for (x,y) in black:
                min_dis = min(abs(x - i) + abs(y - j), min_dis)
            ans_list.append(min_dis)

print(max(ans_list))

Submission status: Runtime Error

Solution code:
import numpy as np
import random
import sys

sys.setrecursionlimit(pow(10,6))

while True:
    width=input()
    height=input()
    if int(width)>=1 and int(width)<=1000 and int(height)>=1 and int(height)<=1000:
        break

A=np.empty((int(height),int(width)),dtype="unicode")
Is_change=np.zeros((int(height),int(width)))

for i in range(int(height)):
    for j in range(int(width)):
        rand=random.random()
        if rand>0.5:
            A[i,j]="#"
            Is_change[i,j]=1
        if rand<0.5:
            A[i,j]="."
print(A)

loop_num=0

while True:
    loop_num+=1
    for i in range(int(height)):
        for j in range(int(width)):
            if A[i,j] == "#":
                try:
                    Is_change[i,j-1]=1
                    Is_change[i,j+1]=1
                    Is_change[i-1,j]=1
                    Is_change[i+1,j]=1
                except:
                    pass
    for i in range(int(height)):
        for j in range(int(width)):
            if Is_change[i,j]==1:
                A[i,j]="#"

    
    if "*" in A:
        pass
    else:
        break

print(loop_num)

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 = []
            for m in range(len(listk)):
                maxd.append(manhattand(listk[m], (k,l)))
            kld.append(min(maxd))
        

print(max(kld))

Submission status: Runtime Error

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

P = [[-int(i) for i in input().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:
                    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]


while len(queue) != 1:
    h,w,cnt = queue.pop(0)
    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 = max(P[h+dh][w+dw],a)
                if P[h+dh][w+dw]==-1:
                    queue+=[[h+dh,w+dw,cnt+1]]       
        m = max(m, cnt+1 + a)

print(m)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
H, W = map(int, input().split())
A = [[s for s in input()] for i in range(H)]

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

c = 0
for k in range(H*W):
    c += 1
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                if i >= 1 and [i-1,j] not in l:
                    l.append([i-1,j])
                if j >= 1 and [i,j-1] not in l:
                    l.append([i,j-1])
                if i+1 < H and [i+1,j] not in l:
                    l.append([i+1, j])
                if j+1 < H and [i,j+1] not in l:
                    l.append([i, j+1])
    if len(l) == H*W:
        break
    for x in l:
        A[x[0]][x[1]] = '#'
print(c)

Submission status: Runtime Error

Solution code:
from itertools import combinations
H, W = map(int, input().split())
bp = []
wp = []
for i in range(H):
    s = input()
    for j, e in enumerate(s):
        if e == '#':
            bp.append((i, j))
        if e == '.':
            wp.append((i, j))
d = {}
for v in combinations(bp, 2):
    if v[0] in d.keys():
        d[v[0]] = min(d[v[0]], abs(v[0][0]-v[1][0])+abs(v[0][1]-v[1][1]))
    else:
        d[v[0]] = abs(v[0][0]-v[1][0])+abs(v[0][1]-v[1][1])
print(max(d.values())-1)

Submission status: Runtime Error

Solution code:
import copy
a = list(map(int, input().split()))
c=[input() for i in range(a[1])]
d=[]
for i in range(a[1]):
  d.append(list(c[i]))
b=[copy.deepcopy(d),copy.deepcopy(d)]
z=1
t=1
while z==1:
  for h in range(a[0]):
    for w in range(a[1]):
      #print(t,h,w,b[t%2])
      f=0
      if b[t%2][h][w]=='#':
        if h<a[0]:
          b[(t+1)%2][h-1][w]='#'
        if h>a[1]:          
          b[(t+1)%2][h+1][w]='#'
        if w<a[0]:   
          b[(t+1)%2][h][w-1]='#'
        if w>a[1]:   
          b[(t+1)%2][h][w+1]='#'
        t=t+1
        f=1
  if f==0:
    print(t)
    break

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
        if l==1:
            break
    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=[]
for i in range(h):
  a.append(list(map(int,input().split())))
cnt=0
lst=['#']*w
for j in range(1,h-1):
  for k in range(1,w-1):
    if all(a[l]==lst for l in range(h)):
      break
    elif a[j][k]=='#':
      a[j-1][k]='#'
      a[j+1][k]='#'
      a[j-1][k-1]='#'
      a[j-1][k]='#'
      a[j-1][k+1]='#'
      a[j+1][k-1]='#'
      a[j+1][k]='#'
      a[j+1][k+1]='#'
      cnt+=1
print(cnt)

Submission status: Runtime Error

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

import numpy as np

black_list = []
white_list = []
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            black_list.append([i, j])
        else:
            white_list.append([i, j])

all_diffs = np.expand_dims(white_list, axis=1) - np.expand_dims(black_list, axis=0)
all_diffs = np.sum(np.abs(all_diffs), axis=-1)

print(np.max(np.min(all_diffs, axis=1)))

Submission status: Runtime Error

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

for _ in range(H):
    A.append(list(input().strip()))

count = 0
white = 1

while True:
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                if i == 0:
                    A[i+1][j] = '*'
                elif 0 < i < W - 1:
                    A[i-1][j] = '*'
                    A[i+1][j] = '*'
                elif i == W - 1:
                    A[i-1][j] = '*'
        A[i] = ''.join(A[i]).replace('.#.', '***').replace('.#', '**').replace('#.', '**')
        A[i] = list(A[i])

    white = 0
    for i in range(H):
        if '*' in A[i]:
            A[i] = ['#' if c == '*' else c for c in A[i]]
            if '.' in A[i]:
                white += 1
    
    if white == 0:
        break
    else:
        count += 1

print(count)

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:
  check_list = []
  for i in range(W):
    for j in range(H):
      if A[i][j] == "#":
        if (i-1)>=0:
          check_list.append([i-1,j])
        if (j-1)>=0:
          check_list.append([i,j-1])
        if (i+1)<=(W-1):
          check_list.append([i+1,j])
        if (j+1)<=(H-1):
          check_list.append([i,j+1])

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

  ans += 1

  status = []
  for i in range(W):
    if "." in A[i]:
      status.append(False)

  if False not in status:
    break

print(ans)

Submission status: Runtime Error

Solution code:
import copy

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

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

ansCnt = 0
masLst = [['' for i in range(H)] for j in range(W)]
for i in range(W):
    tmp = input()
    for j in range(len(tmp)):
        masLst[i][j] = tmp[j]

loopBoean = True
while loopBoean:
    msLst = copy.deepcopy(masLst)
    for i in range(W):
        for j in range(H):
            if masLst[i][j] == '#':
                fib(msLst, i, j)
    ansCnt += 1
    masLst = msLst

    jloop = True
    for i in range(W):
        for j in range(H):
            if masLst[i][j] != '#':
                jloop = False
                break
        if jloop == False:
            break
    if jloop == True:
        loopBoean = False

print(ansCnt)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
A_input = [list(str(input())) 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))
if white:
    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)
else:
    print(0)

Submission status: Runtime Error

Solution code:
from queue import Queue

def main():
    h, w = map(int, input().rstrip().split(' '))
    a = [None] * h
    for i in range(h):
        a[i] = [c == '#' for c in input().rstrip()]
    
    print(count(h, w, a))
    
def count(h, w, a):
    def neighbor(ij):
        n = []
        i, j = ij
        if i > 0:
            n.append((i-1, j))
        if i < h - 1:
            n.append((i+1, j))
        if j > 0:
            n.append((i, j-1))
        if j < w - 1:
            n.append((i, j+1))
        return n


    def min_dist2black(a, s1, s2):
        v = (s1, s2)
        dist = [None] * h
        for i in range(h):
            dist[i] = [1000000 for _ in range(w)]
        visited = [None] * h
        for i in range(h):
            visited[i] = [False for _ in range(w)]
        dist[s1][s2] = 0

        q = Queue()
        q.put((s1, s2))
        while not q.empty():
            v = q.get()
            for n in neighbor(v):
                i, j = n
                if visited[i][j]:
                    continue
                d = dist[v[0]][v[1]] + 1
                if a[i][j]:
                    return d
                visited[i][j] = True
                dist[i][j] = d
                q.put((i, j))
        raise RuntimeError(f"There is no black? ({s1}, {s2})")
        return None

    longest_distance = 0
    not_max = [None] * h
    for i in range(h):
        not_max[i] = [None] * w
    for i in range(h):
        for j in range(w):
            if a[i][j]:
                # aij is black
                continue
            if not_max[i][j]: # farther v is known
                continue
            d = min_dist2black(a, i, j)
            if d > longest_distance:
                longest_distance = d
    return longest_distance

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from io import StringIO
from collections import deque
import sys


def main(inputs):
    h, w = map(int, next(inputs).split())
    mass = [ next(inputs) for _ in range(h) ]

    done = [[False for _ in m] for m in mass ]
    ans = 0
    for i in range(h):
        for j in range(w):
            if mass[i][j] != '#' and not done[i][j]:
                x = find_black(mass, i, j, done)
                ans = max(ans, x)
                pass
            pass
        pass

    print(ans)
    pass


def find_black(mass, h, w, done):
    q = deque()
    q.append((h, w, 0))
    while len(q):
        h, w, d = q.popleft()
        if mass[h][w] == '#':
            return d
        done[h][w] = True
        if h-1 >= 0 and not done[h-1][w]:
            q.append((h-1, w, d+1))
            pass
        if h+1 < len(mass) and not done[h+1][w]:
            q.append((h+1, w, d+1))
            pass
        if w-1 >= 0 and not done[h][w-1]:
            q.append((h, w-1, d+1))
            pass
        if w+1 < len(mass[0]) and not done[h][w+1]:
            q.append((h, w+1, d+1))
            pass
        pass
    pass

def gen_inputs(str_=None):
    inputs = StringIO(str_) if str_ else sys.stdin
    while True:
        a = inputs.readline().rstrip()
        yield a
        pass
    pass


if __name__ == "__main__":
    main(gen_inputs())
    pass

Submission status: Runtime Error

Solution code:
import numpy as np
mas = np.array(list(map(int,input().split())))
go = []
for i in range(int(mas[1])):
    try:
        go.append(list(input()))

    except:
        break
go = np.array(go)
H = int(mas[0])
W = int(mas[1])

print(go)
b = np.array(['#' for i in range(H*W)])
itr = 1

while not all(go.flatten() == b):
    b_w = np.where(go == '#')
    for i in range(len(x)):
        try:
            go[b_w[i] + [0,1]] ='#'
            go[b_w[i] + [0,-1] ] = '#'
            go[b_w[i] + [1,0]  ] = '#'
            go[b_w[i] + [-1,0] ] = '#'
        except:
            pass
    itr += 1
print(itr)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
list1 = ["."*(w+2)]+["."+input()+"." for i in range(h)]+["."*(w+2)]
print(list1[1][3])
print(list1)
cnt = 0
maxcnt = 0
for j in range(h+1):
  for k in range(w+1):
    if list1[j][k] == '.' and cnt == 0:
      cnt+=1
    if list1[j][k-1]=='.' and list1[j][k+1]=='.' and list1[j+1][k]=='.' and list1[j-1][k]=='.':
      if j != 0 and k != 0:  
        cnt+=1
        print(j,k)
    else:
      if cnt > maxcnt:
        maxcnt = cnt
      cnt = 0
print(maxcnt)

Submission status: Runtime Error

Solution code:
import numpy as np
import random

while True:
    width=input()
    height=input()
    if int(width)>=1 and int(width)<=1000 and int(height)>=1 and int(height)<=1000:
        break

A=np.empty((int(height),int(width)),dtype="unicode")
Is_change=np.zeros((int(height),int(width)))

for i in range(int(height)):
    for j in range(int(width)):
        rand=random.random()
        if rand>0.5:
            A[i,j]="#"
            Is_change[i,j]=1
        if rand<0.5:
            A[i,j]="."
print(A)

loop_num=0

while True:
    loop_num+=1
    for i in range(int(height)):
        for j in range(int(width)):
            if A[i,j] == "#":
                try:
                    Is_change[i,j-1]=1
                    Is_change[i,j+1]=1
                    Is_change[i-1,j]=1
                    Is_change[i+1,j]=1
                except:
                    pass
    for i in range(int(height)):
        for j in range(int(width)):
            if Is_change[i,j]==1:
                A[i,j]="#"
    
    if "*" in A:
        pass
    else:
        break

print(loop_num)

Submission status: Runtime Error

Solution code:
from collections import deque

h,w = map(int, input().split())
A = [['*']*(w+2)] + [['*']+[s for s in input()]+['*'] for _ in range(h)] + [['*']*(w+2)]

dist_to_black = [[0]*w for _ in range(h)]

def bfs(sx,sy):
    dq = deque([(sy,sx,0)])
    while dq:
        y,x,n = dq.popleft()
        if A[y][x]=='#':
            return n
        elif A[y][x]=='.':
            dq.append([y+1,x,n+1])
            dq.append([y-1,x,n+1])
            dq.append([y,x+1,n+1])
            dq.append([y,x-1,n+1])

ans = -1
for i in range(h):
    for j in range(w):
        # dist_to_black[i][j] = bfs(i+1,j+1)
        ans = max(ans, bfs(i+1, j+1))

print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
a=[]
for i in range(h):
  a.append(list(map(int,input().split())))
cnt=0
lst=['#']*w
for j in range(h):
  for k in range(w):
    if a[j][k]=='#':
      a[j-1][k]='#'
      a[j+1][k]='#'
      a[j-1][k-1]='#'
      a[j-1][k]='#'
      a[j-1][k+1]='#'
      a[j+1][k-1]='#'
      a[j+1][k]='#'
      a[j+1][k+1]='#'
      cnt+=1
    elif all(a[l]==lst for l in range(h)):
      break
print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
mas = np.array(list(map(int,input().split())))
go = []
for i in range(int(mas[1])):
    try:
        go.append(list(input()))

    except:
        break
go = np.array(go)
H = int(mas[0])
W = int(mas[1])

print(go)
b = np.array(['#' for i in range(H*W)])
itr = 1

while not all(go.flatten() == b):
    b_w = np.where(go == '#')
    for i in range(len(x)):
        try:
            go[b_w[i] + [0,1]] ='#'
            go[b_w[i] + [0,-1] ] = '#'
            go[b_w[i] + [1,0]  ] = '#'
            go[b_w[i] + [-1,0] ] = '#'
            print(go)
        except ValueError:
            pass
    itr += 1
print(itr)