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:
import numpy as np
H,W =map(int ,raw_input().split())
l =[]
for i in range(H):
    l.append(map(str ,raw_input().split()))
for i in range(H):
    l[i] =list(l[i][0])
    
def distance(i,j,l):
    if l[i][j] == '#':
        return 1
    elif l[i][j] == '.':
        return 10000
    else:
        return l[i][j]+1
flag =1
Max = 0
while flag == 1:
    flag = 0
    for i in range(H):
        for j in range(W):
            if l[i][j] == '.':
                s = 10000
                if i>0:
                    k = distance(i-1,j,l)
                    if k < s:
                        s =k
                if i<H-1:
                    k = distance(i+1,j,l)
                    if k < s:
                        s =k
                if j>0:
                    k = distance(i,j-1,l)
                    if k < s:
                        s =k
                if j<W-1:
                    k = distance(i,j+1,l)
                    if k < s:
                        s =k
                if s == 10000:
                    flag =1
                else:
                    l[i][j] = s
                    if Max < s:
                        Max = s
print str(s)

Submission status: Runtime Error

Solution code:
import numpy
H, W = map(int, input().split())
blocknum = H * W
maps = ['0'*(H+2)]*(W+2)
for i in range(H):
  maps[i+1] = '0' + input().translate(str.maketrans({'.': '0', '#': '1'})) + '0'

sums = 0
res = 0
resmaps = maps[:]

while sums < blocknum:
  for Hin, j in enumerate(maps):
    if not (Hin == 0 or Hin == (len(maps)-1)):
      for Win, k in enumerate(j):
        if not (Win == 0 or Win == (len(j)-1)):
          if int(k) + int(j[Win - 1]) + int(j[Win + 1]) + int(maps[Hin - 1][Win]) + int(maps[Hin + 1][Win]) > 0:
            resmaps[Hin] = maps[Hin][:Win] + '1' + maps[Hin][Win+1:]
  res += 1
  maps = resmaps
  sums = sum(list(map(lambda x:x.count('1'), resmaps)))

print(res)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
S_X=[]
S_Y=[]
C_X=[]
C_Y=[]
ans_list=[]
B = []

for i in range(H):
    A = list(input())
    for j in range(W):
        if A[j] == '#':
            S_X.append(i+1)
            S_Y.append(j+1)
        else :
            C_X.append(i+1)
            C_Y.append(j+1)

S_len=len(S_X)
C_len=len(C_X)


for i in range(C_len):
    for j in range(S_len):
        B.append((abs(S_X[j]-C_X[i])+abs(S_Y[j]-C_Y[i])))
    a = min(B)*(-1)
    ans_list.append(a)
    
    B = []

ans_list.sort()


print(ans_list[0]*(-1))

Submission status: Runtime Error

Solution code:
H, W = [int(c) for c in input().split()]
A = [[c=='#' for c in input()] for row in range(H)]

black = []
white = []
for irow in range(H):
    for icol in range(W):
        if A[irow][icol]:
            black.append((irow,icol))
        else:
            white.append((irow,icol))


dmax=0
for w_x, w_y in white:
    dmin=1000000
    for b_x, b_y in black:
        d = abs(bx-wx)+abs(by-wy)
        if d<dmin:
            dmin=d
    if dmin>dmax:
        dmax=dmin

print(dmax)

Submission status: Runtime Error

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

flag = 0
loop_count = 0
A = [["#" for i in range(H)] for j in range(W)]

for i in range(H):
    word = a[i][0]
    for j in range(W):
        A[i][j] = word[j]

while flag == 0:
    count = 0
    B = []
    C = []
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                count += 1
                B.append(i)
                C.append(j)
    if count == H * W:
        flag = 1
    else:
        for i in range(count):
            if B[i] == 0:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]+1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]+1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]+1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
            elif B[i] == H-1:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]-1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
            else:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]+1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]+1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]+1][C[i]] = "#"
        loop_count += 1
        
print(loop_count)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
maps  =[ list(str(input())) for _ in range(h)]
import sys
sys.setrecursionlimit(1000000)


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:
#!/usr/bin/env python3
import sys
from pprint import pprint

INF = float('inf')

def solve(H: int, W: int, A: "List[str]"):
    #corner = [H + W] * 4
    #for i in range(H):
    #    for j in range(W):
    #        if A[i][j] == '#':
    #            corner[0] = min(corner[0], abs(i - 0) + abs(j - 0))
    #            corner[1] = min(corner[1], abs(i - 0) + abs(j - (W - 1)))
    #            corner[2] = min(corner[2], abs(i - (H - 1)) + abs(j - 0))
    #            corner[3] = min(corner[3], abs(i - (H - 1)) + abs(j - (W - 1)))

    count = [[INF] * (W + 2) for _ in range(H + 2)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                count[i + 1][j + 1] = 0
            else:
                count[i + 1][j + 1] = min(count[i][j + 1], count[i + 1][j]) + 1

    #pprint(count)
    ret = 0
    for i in range(H, 0, -1):
        for j in range(W, 0, -1):
            count[i][j] = min(count[i][j], min(count[i][j + 1], count[i + 1][j]) + 1)
            ret = max(ret, count[i][j])
    #pprint(count)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ next(tokens) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
(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())
l = []
a = [input() for j in range(w)]
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            l.append((i,j))

answer = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == '.':
            d = h*w
            for b in l:
                d1 = abs(i-b[0])+abs(j-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:
#!/usr/bin/env python3
import sys
from collections import deque
INF = float("inf")


def argmost(a, comp, ini):
    m, n = ini, -1
    for i, v in enumerate(a):
        if comp(m, v):
            m, n = v, i
    return m, n


def solve(H: int, W: int, A: "List[str]"):
    # 黒の位置をx, y座標として持つ。黒からの距離も一緒に持つ
    black = []
    B = [[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                black.append((i, j, 0))
            B[i][j] = 0 if A[i][j] == "#" else -INF  # 黒は1

    black.sort(key=lambda x: x[0])

    q = deque()
    q.extend(black)

    while len(q) > 0:
        cy, cx, dis = q.popleft()
        for y, x in zip([cy-1, cy, cy+1, cy], [cx, cx-1, cx, cx+1]):
            if 0 <= y < H and 0 <= x < W and B[y][x] < 0:
                B[y][x] = dis+1
                q.append((y, x, dis+1))

    m = -INF
    for i in range(H):
        a = max(B[i])
        if m < a:
            m = a
    print(m)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, A)


if __name__ == '__main__':
    main()

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+2 > i + x >= 0 and w+2 > j + y >= 0:
                        tem[i + x][j + y] = '#'
    ans += 1
    a = tem
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H,W =map(int ,raw_input().split())
l =[]
for i in range(H):
    l.append(map(str ,raw_input().split()))
for i in range(H):
    l[i] =list(l[i][0])
    
def distance(i,j,l):
    if l[i][j] == '#':
        return 1
    elif l[i][j] == '.':
        return 1000
    else:
        return l[i][j]+1
flag =1
Max = 0
while flag == 1:
    flag = 0
    for i in range(H):
        for j in range(W):
            if l[i][j] == '.':
                s = 1000
                if i>0:
                    k = distance(i-1,j,l)
                    if k < s:
                        s =k
                if i<H-1:
                    k = distance(i+1,j,l)
                    if k < s:
                        s =k
                if j>0:
                    k = distance(i,j-1,l)
                    if k < s:
                        s =k
                if j<W-1:
                    k = distance(i,j+1,l)
                    if k < s:
                        s =k
                if s == 1000:
                    flag =1
                else:
                    l[i][j] = s
                    if Max < s:
                        Max = s
print str(s)

Submission status: Runtime Error

Solution code:
from collections import deque


class PaintWork:
    Row: int
    Col: int
    gen: int

    def setWork(self, r, c, g):
        self.Row = r
        self.Col = c
        self.gen = g
        return self

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

CountMap = []

# キューの構造：行,列,世代
PaintQue = deque([])

for row in range(H):
    rowString = input()
    rowCount = []

    for col in range(W):
        cell = rowString[col]
        if cell == ".":
            rowCount.append(-1)
        else:
            rowCount.append(0)
            work = PaintWork()
            work.setWork(row, col, 0)
            PaintQue.append(work)

    CountMap.append(rowCount)

ans = 0

while len(PaintQue) > 0:
    nextBlack: PaintWork
    nextBlack = PaintQue.popleft()
    selectRow = nextBlack.Row
    selectCol = nextBlack.Col
    gen = nextBlack.gen

    ans = gen if gen > ans else ans

    if selectRow > 0 and CountMap[selectRow - 1][selectCol] == -1:
        CountMap[selectRow - 1][selectCol] = 0
        PaintQue.append(PaintWork().setWork(selectRow - 1, selectCol, gen + 1))

    if selectRow < H - 1 and CountMap[selectRow + 1][selectCol] == -1:
        CountMap[selectRow + 1][selectCol] = 0
        PaintQue.append(PaintWork().setWork(selectRow + 1, selectCol, gen + 1))

    if selectCol > 0 and CountMap[selectRow][selectCol - 1] == -1:
        CountMap[selectRow][selectCol - 1] = 0
        PaintQue.append(PaintWork().setWork(selectRow, selectCol - 1, gen + 1))

    if selectCol < W - 1 and CountMap[selectRow][selectCol + 1] == -1:
        CountMap[selectRow][selectCol + 1] = 0
        PaintQue.append(PaintWork().setWork(selectRow, selectCol + 1, gen + 1))

print(ans)

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(a)]  for i in range(a)]
e = [[0 for j in range(a)]  for i in range(a)]#コピー用
#print(c,d,e)

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

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

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


count = 0
while not check(d):
    count += 1
    for i in range(a):
        for j in range(a):
            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)
    check(d)
print(count)

Submission status: Runtime Error

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

wc = 0
L = []
wb = [[0] * W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if A[i][j] == w:
      wc += 1
    else:
      L.append([i, j])
      wb[i][j] = 1

Ans = 0
while wc > 0:
  L2 = []
  Ans += 1
  for ki, kj in L:
    if ki - 1 >= 0:
      if wb[ki - 1][kj] == 0:
        wb[ki - 1][kj] = 1
        wc -= 1
        L2.append([ki - 1 , kj])
    if ki + 1 < W:
      if wb[ki + 1][kj] == 0:
        wb[ki + 1][kj] = 1
        wc -= 1
        L2.append([ki + 1 , kj])
    if kj - 1 >= 0:
      if wb[ki][kj - 1] == 0:
        wb[ki][kj - 1] = 1
        wc -= 1
        L2.append([ki, kj - 1])
    if kj + 1 < H:
      if wb[ki][kj + 1] == 0:
        wb[ki][kj + 1] = 1
        wc -= 1
        L2.append([ki, kj + 1])
  L = L2
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 flatten_2dim(array):
    return [item for sublist in array for item in sublist]


for k in range(N[0] + N[1]):
    if flatten_2dim(Room).count("#") >= 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:
import copy
import numpy
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+2 > i + x >= 0 and w+2 > j + y >= 0:
                        tem[i + x][j + y] = '#'
    ans += 1
    a = tem
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ni = lambda: int(ns()) #int
na = lambda: list(map(int, stdin.readline().split())) #int,list
ns = lambda: stdin.readline().rstrip()  # str
h, w = na()
a = []
for i in range(h):
	a.append(list(ns()))
a=np.array(a)
b=np.array(np.where(a=="#")).T
w=np.array(np.where(a==".")).T

m_list=[]
for i in w:
	m=10000000
	for j in b:
		#print(abs(i-j).sum())
		m = min(abs(i-j).sum(),m)
	m_list.append(m)

print(max(m_list))

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
t = list()
for i in range(H):
  i = list(int(i) for i in input().split())
  t.extend(i)

r = 0
while True:
  for c in range(H*W):
    if t[c] == "#":
      pass
    else:
      if t[c-1] == "#" or t[c+1] == "#" or t[c+W] == "#" or t[c-W] == "#":
        t[c] = "#"
      else:
        pass
  r += 1
  if t.count(".") == 0:
    break
print(r)

Submission status: Runtime Error

Solution code:
import numpy as np
#input
#[n,m,r] = list(map(int,input().split()))
#s = list(map(int,input().split()))
#b = list(map(int,input().split()))
#example
[H,W] = list(map(int,input().split()))
A = np.zeros((H+2,W+2))
num_black = 0
for i in range(1,W+1):
    tmp_list = np.zeros(W)
    string = str(input())
    for j in range(W):
        if string[j] == "#":
            num_black += 1
            A[i][j+1] = 1
        else:
            A[i][j+1] = 0
        

def check_black(A,i,j):
    if A[i-1][j] == 1 or A[i][j-1] == 1 or A[i][j+1] == 1 or A[i+1][j] == 1:
        return True
    else:
        return False
ans = 0    
while (num_black < H*W):
    black_list_future = []
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j] == 0:
                if check_black(A,i,j):
                    black_list_future.append([i,j])
    for k in range(len(black_list_future)):
        A[black_list_future[k][0]][black_list_future[k][1]] = 1
    num_black += len(black_list_future)
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
a=[[float("inf")]*w for i in range(h)]
b=[[False]*w for i in range(h)]
def f(x,y,t):
    if 0<=x<h and 0<=y<w:
        if s[x][y]=="#":
            if b[x][y]==False:
                a[x][y]=0
                b[x][y]=True
                f(x,y+1,1)
                f(x+1,y,1)
                f(x-1,y,1)
                f(x,y-1,1)
        else:
            if a[x][y]>t:
                a[x][y]=t
                f(x,y+1,t+1)
                f(x+1,y,t+1)
                f(x-1,y,t+1)
                f(x,y-1,t+1)
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            f(i,j,0)
            break
print(max(max(i)for i in a))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
maze=[list(input()) for i in range(w)]
s=[]
#print(maze)
for i in range(w):
    for j in range(h):
        if maze[i][j]=="#":
            s.append([i,j])
l=[]
#print(maze,s)
for i in range(w):
    for j in range(h):
        cnt=10**7
        if maze[i][j]==".":
            for k in s:
                a=abs(k[0]-i)+abs(k[1]-j)
                cnt=min(cnt,a)
            l.append(cnt)
#print(l)
print(max(l))

Submission status: Runtime Error

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

import numpy as np

A = np.zeros((H+2,W+2))
for i in range(1,H+1):
    for j in range(1,W+1):
        if A_input[i+1][0][j+1] =='#':
            A[i+1][j+1]=1


counter = 0
A_next = np.copy(A)
while True:
    if np.min(A)==1:
        break

    else:
        counter +=1
        for i in range(1,H+1):
            for j in range(1,W+1):
                if A[i][j] ==0:
                    if A[i-1][j]==1:
                        A_next[i][j]=1
                    elif A[i][j-1]==1:
                        A_next[i][j]=1
                    elif A[i][j+1]==1:
                        A_next[i][j]=1
                    elif A[i+1][j]==1:
                        A_next[i][j]=1

        A = np.copy(A_next)
print(counter)

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = []
  for i in range(dep):
    s = input()
    a.append([])
    for in j in range(len(s)):
      a[i].append(s[j])
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if i > 0 and a[i-1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#" and a[i][j] == ".":
          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())
lst = []
for h in range(H):
    lst.append([s for s in input()])

aaa = True
for j, h in enumerate(lst):
    for i, w in enumerate(h):
        if lst[i][j] == '.':
            aaa = False
            break
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:
import numpy as np

H, W = list(map(int, input().split()))
env = []
for i in range(H):
    tmp = []
    for j in input():
        if j == '#':
            tmp.append(0)
        else:
            tmp.append(1)
    env.append(tmp)
    tmp = []
env = np.array(env)


ans = 0
for _ in range(1000000):
    pos_black = np.where(env == 0)
    
    if len(pos_black[0]) == H*W:
        break
    else:
        ans += 1
    
    if len(pos_black[0]) == 1:
        h, w = pos_black
        h = h.item()
        w = w.item()
        if h-1 >= 0:
            env[h-1][w] = 0
        if h+1 <= H:
            env[h+1][w] = 0
        if w-1 >= 0:
            env[h][w-1] = 0
        if w+1 <= W:
            env[h][w+1] = 0

    else:
        for h, w in zip(pos_black[0], pos_black[1]):
            if h-1 >= 0:
                env[h-1][w] = 0
            if h+1 <= H-1:
                env[h+1][w] = 0
            if w-1 >= 0:
                env[h][w-1] = 0
            if w+1 <= W-1:
                env[h][w+1] = 0
                
print(ans)

Submission status: Runtime Error

Solution code:
h,w= map(int, input().split())
a = []
for i in range(h):
    li = input()
    a.append(li)
t = []
for i in range(h):
    t.append([-1]*w)
x = []
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            t[i][j] = 0
            x.append((i,j))
ans = 0
y = ans
while True:
    # print("ans"+str(ans))
    y += 1
    xx = []
    for p in x:
        pp = [(p[0]-1, p[1]), (p[0]+1, p[1]), (p[0], p[1]-1), (p[0], p[1]+1)]
        for ppp in pp:
            if (0 <= ppp[0] < w) and (0 <= ppp[1] < h):
                if t[ppp[0]][ppp[1]] == -1:
                    t[ppp[0]][ppp[1]] = ans
                    xx.append((ppp[0], ppp[1]))
    x = xx
    if len(x) == 0:
        break
    ans += 1
    # print(x)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = list(map(int, input().split()))
env = []
for i in range(H):
    tmp = []
    for j in input():
        if j == '#':
            tmp.append(0)
        else:
            tmp.append(1)
    env.append(tmp)
    tmp = []
env = np.array(env)


ans = 0
memo = []
for _ in range(10000000000):
    pos_black = np.where(env == 0)
    
    if len(pos_black[0]) == H*W:
        break
    else:
        ans += 1
    
    if len(pos_black[0]) == 1:
        h, w = pos_black
        h = h.item()
        w = w.item()
        if h-1 >= 0:
            env[h-1][w] = 0
            memo.append(h-1, w)
        if h+1 <= H:
            env[h+1][w] = 0
            memo.append(h+1, w)
        if w-1 >= 0:
            env[h][w-1] = 0
            memo.append(h, w-1)
        if w+1 <= W:
            env[h][w+1] = 0
            memo.append(h, w+1)

    else:
        for h, w in zip(pos_black[0], pos_black[1]):
            if h-1 >= 0:
                env[h-1][w] = 0
                memo.append(h-1うんおｓ, w)
            if h+1 <= H-1:
                env[h+1][w] = 0
            if w-1 >= 0:
                env[h][w-1] = 0
            if w+1 <= W-1:
                env[h][w+1] = 0
                
print(ans)

Submission status: Runtime Error

Solution code:
import sys

if __name__ == '__main__':
    H, W = map(lambda x:int(x), sys.stdin.readline().split())
    # array = [[0 for x in range(W)] for y in range(H)]
    array = []
    for i in range(H):
        array += [list(sys.stdin.readline().rstrip())]
    # print(array)

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

Submission status: Runtime Error

Solution code:
 import sys
input = sys.stdin.readline

H, W = map(int, input().split())
A_array = [input()[:-1] for s in range(H)]

ans_array = [[10000] * W for i in range(H)]
# print(ans_array)

for i, A in enumerate(A_array):
    black_index = [i for i, x in enumerate(A) if x == "#"]
    # print(black_index)
    for black in black_index:
        ans_array = [[
            min(ans_array[h][w],
                abs(h - i) + abs(w - black)) for w in range(W)
        ] for h in range(H)]
print(max([max(ans_array[i]) for i in range(H)]))

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = []
  for i in range(dep):
    s = input()
    a.append([])
    for in j in range(s):
      a[i].append(s[j])
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if i > 0 and a[i-1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#" and a[i][j] == ".":
          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())
a=[input() for i in range(h)]
x=[[0 for i in range(h)] for j in range(w)]
y=[[0 for i in range(h)] for j in range(w)]
def check(i, j):
    if 0 <= i <= (h-1) and 0 <= j <= (w-1):
        y[i][j] = 1
for i in range(h):
    for j, char in enumerate(a[i]):
        if char == '#':
            x[i][j] = 1
counter = 0
while True:
    counter += 1
    for i in range(h):
        for j in range(w):
            if x[i][j] == 1:
                check(i+1,j)
                check(i-1,j)
                check(i,j+1)
                check(i,j-1)
    for i in range(h):
        for j in range(w):
            if y[i][j] == 0:
                break
        else:
            continue
        break
    else:
        break
    x = [[y[i][j] for i in range(h)] for j in range(w)]
print(counter)