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:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H, W):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    tmp = ['#'*(W+2)]
    tmp = tmp + ['#' + read()[:-1] + '#' for _ in range(H)]
    tmp = tmp + ['#' * (W + 2)]
    return tmp


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


H, W = read_ints()
A = read_map(H, W)
visited = [[True]*(W+2)]
visited.extend([[True] + [False] * W + [True] for _ in range(H)])
visited.append([True]*(W+2))  # 無駄な探索を省くため
from itertools import product
from collections import deque
# print(visited)
# 開始地点
start = []
for i, a in enumerate(A):
    if i == 0 or i == H + 1:
        continue
    for j, aa in enumerate(a):
        if j == 0 or j == W + 1:
            continue
        if aa == '#':
            start.append((i, j))

# 探索の準備
mvx = (1, 0, -1, 0)
mvy = (0, 1, 0, -1)


# distlist = []

# for si, sj in start:
n_white = (W)*(H)
# visited = [[False] * (W+2) for _ in range(H+2)]
que = deque([(si, sj, 0) for si, sj in start])
for si, sj in start:
    n_white -= 1
    visited[si][sj] = True

# 幅優先探索
while que:
    y, x, cost = que.popleft()

    if n_white <= 0:
        # ans = cost
        break
    for dy, dx in zip(mvy, mvx):
        y_new, x_new = y + dy, x + dx
        # 探索に含めない条件
        if not (-1 < y_new < H+2) or not (-1 < x_new < W+2):
            continue
        if visited[y_new][x_new]:
            continue

        # 探索に追加してあげる
        que.append((y_new, x_new, cost + 1))
        visited[y_new][x_new] = True
        n_white -= 1
        if n_white == 0:
            ans = cost + 1
            break

    # A[si][sj] = '#'
print(ans+1)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

# 黒: #
# 白: .

H, W = input().split()

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

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

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

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

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

    B[1:W+1,1:H+1] = B[1:W+1,1:H+1] + B[2:W+2,1:H+1] + B[0:W,1:H+1] \
                                    + B[1:W+1,2:H+2] + B[1:W+1,0:H]

    B = np.where(B == 0, 0, 1)
    A = B[1:H+1,1:W+1]
    w_num = np.sum(A== 0)
#    print(B)

print(cnt)

Submission status: Runtime Error

Solution code:
A_list.insert(0,['z'*W])
A_list.append(['z'*W])

for hhh in range(H+2):
    A_list[hhh][0] = 'z' + A_list[hhh][0]
    A_list[hhh][0] = A_list[hhh][0] + 'z'
    
for hh in range(H+2):
    A_list[hh] = list(A_list[hh][0])
    
    
black_points = []
for h in range(H+2):
    for w in range(W+2):
        if A_list[h][w] == '#':
            black_points.append([h,w])

counter = 0
while len(black_points)<H*W:
    counter += 1
    for hw in range(len(black_points)):
      if A_list[black_points[hw][0]-1][black_points[hw][1]] == '.':
            black_points.append([black_points[hw][0]-1, black_points[hw][1]])
            A_list[black_points[hw][0]-1][black_points[hw][1]] = '#'
          
      if A_list[black_points[hw][0]+1][black_points[hw][1]] == '.':
            black_points.append([black_points[hw][0]+1, black_points[hw][1]])
            A_list[black_points[hw][0]+1][black_points[hw][1]] = '#'
          
      if A_list[black_points[hw][0]][black_points[hw][1]-1] == '.':
            black_points.append([black_points[hw][0]-1, black_points[hw][1]-1])
            A_list[black_points[hw][0]][black_points[hw][1]-1] = '#'
          
      if A_list[black_points[hw][0]][black_points[hw][1]+1] == '.':
            black_points.append([black_points[hw][0], black_points[hw][1]+1])
            A_list[black_points[hw][0]][black_points[hw][1]+1] = '#'
          
print(counter)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

h,w = na()

a = []

for i in range(h):
  s = ns()
  b = []
  for j in s:
    if j == '#':
      b.append(1)
    if j == '.':
      b.append(0)
  a.append(b)

c = 1
while(True):
  p = True
  for i in range(w):
    for j in range(h):
      if a[i][j] == c:
        if i > 0 and a[i-1][j] == 0:
          p = False
          a[i-1][j] = c+1
        if j > 0 and a[i][j-1] == 0:
          p = False
          a[i][j-1] = c+1
        if i < w-1 and a[i+1][j] == 0:
          p = False
          a[i+1][j] = c+1
        if j < h-1 and a[i][j+1] == 0:
          p = False
          a[i][j+1] = c+1
  if p:
    break
  else:
    c+=1

print(c-1)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
listA=[]
list01 = [[h+w+2] * h for i in range(w)]
for i in range(h):
    eline=input()
    listA.append(eline)

loclist=[]
n1=0
for i in range(h):
    for j in range(w):
        if listA[i][j]=='#':
            list01[i][j]=0
            loclist.append((i,j))
            n1=n1+1

for pt in loclist:
    
    for i in range(-1,2):
        for j in range(-1,2):
            if abs(i)+abs(j)==1:
                x=pt[0]+i
                y=pt[1]+j            
                if -1<x<h and -1<y<w:
                    if list01[x][y]==(h+w+2):
                        n1=n1+1
                    if list01[x][y]>list01[pt[0]][pt[1]]+1:
                        list01[x][y]=list01[pt[0]][pt[1]]+1
                        loclist.append((x,y))
        if n1==h*w:
            break
mx=0
for i in range(h):
    for j in range(w):
        if list01[i][j]>mx:
            mx=list01[i][j]
print(mx)

Submission status: Runtime Error

Solution code:
H,W = map(int, input().split())
grid = [0]*(H*W)
black = []
for i in range(H):
  A_i = input()
  for j in range(W):
    grid[H*i + j] = 0 if A_i[j] == '#' else -1
    if A_i[j] == '#':
      black.append(H*i + j)

ans = 0
while True:
  black_new = []
  for i in black:
    if i - W >= 0:
      if grid[i - W] == -1:
        grid[i - W] = ans + 1
        black_new.append(i-W)
    if i - 1 >= 0 and i%W != 0:
      if grid[i - 1] == -1:
        grid[i - 1] = ans + 1
        black_new.append(i-1)
    if i + 1 <= H*W-1 and i%W != W-1:
      if grid[i + 1] == -1:
        grid[i + 1] = ans + 1
        black_new.append(i+1)
    if i + W <= H*W-1:
      if grid[i + W] == -1:
        grid[i + W] = ans + 1
        black_new.append(i+W)
  black = black_new
  if len(black) == 0:
    print(ans)
    break
  ans += 1

Submission status: Runtime Error

Solution code:
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def s(x):
    a = []
    aa = x[i]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

h,w = m()

a = []
aa = [[] for i in range(h)]
aaa = []
ma = 0
for i in range(h):
    k = list(input())
    a.append(k)
    for j in range(w):
        if k[j] == "#":
            aa[i].append(0)
            aaa.append([i,j])
        else:
            aa[i].append(Max)
kk = []
while True:
    if len(aaa) == 0:
        break
    for i in range(len(aaa)):
        c,v = aaa.pop(0)
        if not c == 0:
            if aa[c-1][v] != min(aa[c][v]+1,aa[c-1][v]) and a[c-1][v] != "#":
                a[c-1][v] = "#"
                aa[c-1][v] = min(aa[c][v]+1,aa[c-1][v])
                kk.append([c-1,v])
                ma = max(ma,aa[c-1][v])
        if not c == h-1:
            if aa[c+1][v] != min(aa[c][v]+1,aa[c+1][v])and a[c+1][v] != "#":
                a[c+1][v] = "#"
                aa[c+1][v] = min(aa[c][v]+1,aa[c+1][v])
                kk.append([c+1,v])
                ma = max(ma,aa[c+1][v])
        if not v == 0:
            if aa[c][v-1] != min(aa[c][v]+1,aa[c][v-1])and a[c][v-1] != "#":
                a[c][v-1] = "#"
                aa[c][v-1] = min(aa[c][v]+1,aa[c][v-1])
                kk.append([c,v-1])
                ma = max(ma,aa[c][v-1])
        if not v == w-1:
            if aa[c][v+1] != min(aa[c][v]+1,aa[c][v+1])and a[c][v+1] != "#":
                a[c][v+1] = "#"
                aa[c][v+1] = min(aa[c][v]+1,aa[c][v+1])
                kk.append([c,v+1])
                ma = max(ma,aa[c][v+1])
    aaa = kk
    kk = []
print(ma)

Submission status: Runtime Error

Solution code:
import numpy as np


class BDF():
    def __init__(self):
        self.H, self.W = map(int,input().split())
        matrix = []
        for _ in range(self.H):

            matrix.append(list(input()))
        
        self.matrix = np.array(matrix)
        

        self.que = []
        self.count = []
        self.went = []
        
        self.white_list = []

        self.max_step = 0        
        self.terminal = False
        self.get_parent()

    def get_parent(self):
        white_list = np.where(self.matrix=='.')
        # ToDo 白がない場合終了
        if len(white_list[0]) ==0:
            print(self.max_step)
            self.terminal = True
        else:
            parent = [white_list[0][0],white_list[1][0]]    
            self.matrix[parent[0],parent[1]] = "d"
            self.que.append(parent)
            self.count.append(0)

    
    def check(self,row,col):
        state = self.matrix[row,col]
        return state

    def end(self,count):
        total_count = count +1
        self.max_step = max(self.max_step,total_count)
        self.que.clear()
        self.count.clear()
        for pos in self.went:
            self.matrix[pos[0],pos[1]] = total_count
            total_count -= 1
        self.went.clear()
        self.get_parent()

    def bdf(self):
        # matrix = self.matrix
        cur_info = self.que.pop(0)
        count = self.count.pop(0)
        cur_row = cur_info[0]
        cur_column = cur_info[1]
        
        self.matrix[cur_row,cur_column] = "d"
        self.went.append([cur_row,cur_column])

        num = -1

        # 上を見る       
        if not cur_row == 0:
            state = self.check(cur_row-1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row-1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 下を見る       
        if not cur_row == self.H-1:
            state = self.check(cur_row+1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row+1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 左を見る       
        if not cur_column == 0:
            state = self.check(cur_row,cur_column-1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column-1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 右を見る       
        if not cur_column == self.W-1:
            state = self.check(cur_row,cur_column+1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column+1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        if len(self.que) != 0 and num != -1:
            count = num + count 
            self.end(count)


    def main(self):
        while not self.terminal:
            self.bdf()  



bdf = BDF()
bdf.main()

Submission status: Runtime Error

Solution code:
import numpy as np


class BDF():
    def __init__(self):
        self.H, self.W = map(int,input().split())
        matrix = []
        for _ in range(H):

            matrix.append(list(input()))
        
        self.matrix = np.array(matrix)
        

        self.que = []
        self.count = []
        self.went = []
        
        self.white_list = []

        self.max_step = 0        
        self.terminal = False
        self.get_parent()

    def get_parent(self):
        white_list = np.where(self.matrix=='.')
        # ToDo 白がない場合終了
        if len(white_list[0]) ==0:
            print(self.max_step)
            self.terminal = True
        else:
            parent = [white_list[0][0],white_list[1][0]]    
            self.matrix[parent[0],parent[1]] = "d"
            self.que.append(parent)
            self.count.append(0)

    
    def check(self,row,col):
        state = self.matrix[row,col]
        return state

    def end(self,count):
        total_count = count +1
        self.max_step = max(self.max_step,total_count)
        self.que.clear()
        self.count.clear()
        for pos in self.went:
            self.matrix[pos[0],pos[1]] = total_count
            total_count -= 1
        self.went.clear()
        self.get_parent()

    def bdf(self):
        # matrix = self.matrix
        cur_info = self.que.pop(0)
        count = self.count.pop(0)
        cur_row = cur_info[0]
        cur_column = cur_info[1]
        
        self.matrix[cur_row,cur_column] = "d"
        self.went.append([cur_row,cur_column])

        num = -1

        # 上を見る       
        if not cur_row == 0:
            state = self.check(cur_row-1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row-1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 下を見る       
        if not cur_row == self.H-1:
            state = self.check(cur_row+1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row+1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 左を見る       
        if not cur_column == 0:
            state = self.check(cur_row,cur_column-1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column-1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 右を見る       
        if not cur_column == self.W-1:
            state = self.check(cur_row,cur_column+1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column+1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        if len(self.que) != 0 and num != -1:
            count = num + count 
            self.end(count)


    def main(self):
        while not self.terminal:
            self.bdf()  



bdf = BDF()
bdf.main()

Submission status: Runtime Error

Solution code:
def li():
    return list(map(int, input().split()))


# # 一番左上の黒と左上隅との距離　HW log(HW)
# if __name__ == '__main__':
#     [H, W] = li()
#     A = [input() for _ in range(H)]
#
#     # 黒点のリストを作る
#     black_list = []
#     for i in range(H):
#         for j in range(W):
#             if A[i][j] == '#':
#                 black_list.append((i, j))
#     print(black_list)
#
#     # 左上　右上　左下　右下でソートする
#     # 左上
#     black_list.sort(key=lambda pos: pos[0] + pos[1])
#     print(black_list)
#     print(black_list[0][0] + black_list[0][1])
#     # 右上
#     black_list.sort(key=lambda pos: pos[0] + (W - 1 - pos[1]))
#     print(black_list)
#     print(black_list[0][0] + (W - 1 - black_list[0][1]))
#     # 左下
#     black_list.sort(key=lambda pos: (H - 1 - pos[0]) + pos[1])
#     print(black_list)
#     print((H - 1 - black_list[0][0]) + black_list[0][1])
#     # 右下
#     black_list.sort(key=lambda pos: pos[0] + pos[1], reverse=True)
#     print(black_list)
#     print((H - 1 - black_list[0][0]) + (W - 1 - black_list[0][1]))


def get_char(i, j):
    if not (0 <= i < H and 0 <= j < W):
        return '.'
    return A[i][j]


def get_dist(i, j):
    if not (0 <= i < H and 0 <= j < W):
        return H + W
    if dist[i][j] < H + W:
        # print('cache ', i, j, dist[i][j])
        return dist[i][j]
    if get_char(i, j) == '#':
        return 0
    if get_char(i - 1, j) == '#' or get_char(i + 1, j) == '#' or get_char(i, j - 1) == '#' or get_char(i, j + 1) == '#':
        return 1

    hidariue = min(get_dist(i - 1, j), get_dist(i, j - 1))

    if not (0 <= i < H and 0 <= j < W):
        return H + W

    # print('call ', i, j)
    # print('stack list ', stack)
    # print('dist\n', dist)
    if (i, j) in stack:
        # print('stack ', i, j)
        return H + W
    if not (0 <= i < H and 0 <= j < W):
        return H + W
    if dist[i][j] < H + W:
        # print('cache ', i, j, dist[i][j])
        return dist[i][j]
    if A[i][j] == '#':
        # print('black ', i, j)
        dist[i][j] = 0
        # print('save ', dist)
        return dist[i][j]
    if (i, j) in stack:
        # print('stack ', i, j)
        return H + W
    stack.append((i, j))
    call_list = [(i - 1, j), (i, j - 1), (i + 1, j), (i, j + 1)]
    value_list = []
    for arg in call_list:
        if arg not in stack:
            value_list.append(get_dist(arg[0], arg[1]))
    if len(value_list) == 0:
        return H + W
    min_value = min(value_list) + 1
    stack.remove((i, j))
    return min_value


if __name__ == '__main__':
    [H, W] = li()
    A = [input() for _ in range(H)]

    # print(H, W)
    # print(A)

    stack = []
    dist = []
    for i in range(H):
        row = []
        for j in range(W):
            row.append(H + W)
        dist.append(row)
    for i in range(H):
        for j in range(W):
            dist[i][j] = get_dist(i, j)
            # print('save ', i, j, dist[i][j])

    print(max(list(map(max, dist))))

Submission status: Runtime Error

Solution code:

H , W = map(int,input().split())
A = [0]*H
for i in range(H):
    A[i] = list(input())
w_index=[]
b_index =[]
black =False
max_dist =0
for j in range(H):
        for k in range(W):
            if A[j][k] =='.':
                w_index.append([j,k])
            else:
                b_index.append([j,k])
dist_list= []
for w in w_index:
    
    for b in b_index:
        w_dist =abs(w[0]-b[0])+abs(w[1]-b[1])
        if dist < min_dist:
            min_dist = dist

    dist_list.append(min_dist)  
    w_dist= []

ans =max(dist_list)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

h, w = map(int, input().split())
A=[]

for i in range(0,h):
    A.append(list(str(x) for x in input()))
    
i=0

A = np.array(A)

B = np.where(A == "#")
B1 = np.array([B[0], B[1]]).T

B2 = []

for i in range(0,max(h,w)):
    if (i<h) & (A[i,0] == "."):
         B2.append([i,0])
    if (i<h) & (A[i,w-1] == "."):
        B2.append([i,w-1])
    if (i<w) & (A[0,i] == "."):
         B2.append([0,i])
    if (i<w) & (A[h-1,i] == "."):
        B2.append([h-1,i])
    
C1=[]
C2=[]

for i in range(0,max(len(B1))):
    temp1=[]
    temp2=[]
    for j in range(0,max(i, len(B2))):
        if j<i:
            temp1.append(math.ceil(sum(np.abs(B1[i]-B1[j]))/2))
        if j<len(B2):
            temp2.append(sum(np.abs(B1[i]-B2[j])))
    if len(temp1)>0:
        C1.append(min(temp1))
    if len(temp2)>0:
        C2.append(min(temp2))
        
C3= np.insert(C1,0,C2)
print(int(max(C3)))

Submission status: Runtime Error

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

for i, item in enumerate(a):
    a[i] = list(item[0])

change = [[0 for _ in range(len(a[0]))] for _ in range(len(a))]


def isWhite(a):
    items = []
    for i in range(len(a)):
        items.extend(a[i])
    if "." in items:
        return True
    return False

cnt = 0
while isWhite(a):
    flg = False
    for i in range(len(a)):
        if "#" in a[0]:
            for j in range(len(a[0])):
                if a[i][j] == "#":
                    if i - 1 >= 0:
                        if a[i - 1][j] != "#":
                            change[i-1][j] = 1
                    if i + 1 < len(a):
                        if a[i + 1][j] != "#":
                            change[i+1][j] = 1
                    if j - 1 >= 0:
                        if a[i][j - 1] != "#":
                            change[i][j-1] = 1
                    if j + 1 < len(a):
                        if a[i][j + 1] != "#":
                            change[i][j+1] = 1
                    if not flg:
                        cnt += 1
                    flg = True
        else:
            continue
    for i in range(len(a)):
        if 1 in a[i]:
            for j in range(len(a[0])):
                if change[i][j] == 1:
                    change[i][j] = 0
                    a[i][j] = "#"
        else:
            continue
print(cnt)

Submission status: Runtime Error

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

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

if H==1 and W==1:
  print(0)
  exit()

c=0
while True:

  f=True
  for i in range(H):
    if '.' in l[i]:
      f=False

  if f:
    break

  c+=1
  
  for i in range(H):
    for j in range(W):
      x=l[i][j]
      if x=='#':
        if i>0 and i<H-1 and j>0 and j<W-1:
          f1=True
          if l[i-1][j]=='b':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i+1][j]=='b':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
          if l[i][j-1]=='b':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if l[i][j+1]=='b':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          	if f1:
            	l[i][j]='b'
  
        elif i==0 and j==0:
          f1=True
          if l[i][j+1]=='.':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          if l[i+1][j]=='.':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
         	 if f1:
           	 	l[i][j]='b'
  
        elif i==0 and j>0 and j<W-1:
          f1=True
          if l[i+1][j]=='.':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
          if l[i][j-1]=='.':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if l[i][j+1]=='.':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          	if f1:
            	l[i][j]='b'
  
        elif i==0 and j==W-1:
          f1=True
          if l[i][j-1]=='.':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if l[i+1][j]=='.':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
          if f1:
            l[i][j]='b'
  
        elif i>0 and i<H-1 and j==0:
          f1=True
          if l[i-1][j]=='.':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i][j+1]=='.':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          if l[i+1][j]=='.':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
          if f1:
            l[i][j]='b'
  
        elif i>0 and i<H-1 and j==W-1:
          f1=True
          if l[i-1][j]=='.':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i][j-1]=='.':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if l[i+1][j]=='.':
            l[i][j]='b'
          elif l[i+1][j]=='.':
            l[i+1][j]='a'
            f1=False
          if f1:
            l[i][j]='b'
  
        elif i==H-1 and j==0:
          f1=True
          if l[i-1][j]=='.':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i][j+1]=='.':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          if f1:
            l[i][j]='b'

        elif i==H-1 and j>0 and j<W-1:
          f1=True
          if l[i-1][j]=='.':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i][j-1]=='.':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if l[i][j+1]=='.':
            l[i][j]='b'
          elif l[i][j+1]=='.':
            l[i][j+1]='a'
            f1=False
          if f1:
            l[i][j]='b'
  
        elif i==H-1 and j==W-1:
          f1=True
          if l[i-1][j]=='.':
            l[i][j]='b'
          elif l[i-1][j]=='.':
            l[i-1][j]='a'
            f1=False
          if l[i][j-1]=='.':
            l[i][j]='b'
          elif l[i][j-1]=='.':
            l[i][j-1]='a'
            f1=False
          if f1:
            l[i][j]='b'
  
    # l[i]=list(''.join(l[i]).replace('a','#'))
    for j in range(W):
      if l[i][j]=='a':
        l[i][j]='#'

print(c)

Submission status: Runtime Error

Solution code:
tmp=int()
A[H][W]= 
B[H][W]
for i int (0,H-1):
    for j int (0,W-1):
        if (A[i][j] =# ):
        A[i-1][j-1]=#
        A[i+1][j-1]=#
        A[i-1][j+1]=#
        A[i+1][j+1]=#
for i int(0,HW)
if (A[i][j]=#)

Submission status: Runtime Error

Solution code:
import numpy as np


class BDF():
    def __init__(self,H,W,matrix):
        self.H = H
        self.W = W
        self.matrix = np.array(matrix)
        

        self.que = []
        self.count = []
        self.went = []
        
        self.white_list = []

        self.max_step = 0        
        self.terminal = False
        self.get_parent()

    def get_parent(self):
        white_list = np.where(self.matrix=='.')
        # ToDo 白がない場合終了
        if len(white_list[0]) ==0:
            print(self.max_step)
            self.terminal = True
        else:
            parent = [white_list[0][0],white_list[1][0]]    
            self.matrix[parent[0],parent[1]] = "d"
            self.que.append(parent)
            self.count.append(0)

    
    def check(self,row,col):
        state = self.matrix[row,col]
        return state

    def end(self,count):
        total_count = count +1
        self.max_step = max(self.max_step,total_count)
        self.que.clear()
        self.count.clear()
        for pos in self.went:
            self.matrix[pos[0],pos[1]] = total_count
            total_count -= 1
        self.went.clear()
        self.get_parent()

    def bdf(self):
        # matrix = self.matrix
        cur_info = self.que.pop(0)
        count = self.count.pop(0)
        cur_row = cur_info[0]
        cur_column = cur_info[1]
        
        self.matrix[cur_row,cur_column] = "d"
        self.went.append([cur_row,cur_column])

        num = -1

        # 上を見る       
        if not cur_row == 0:
            state = self.check(cur_row-1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row-1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 下を見る       
        if not cur_row == self.H-1:
            state = self.check(cur_row+1,cur_column)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row+1,cur_column])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 左を見る       
        if not cur_column == 0:
            state = self.check(cur_row,cur_column-1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column-1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        # 右を見る       
        if not cur_column == self.W-1:
            state = self.check(cur_row,cur_column+1)
            if state == "#":
                self.end(count)
            elif state == ".":
                self.que.append([cur_row,cur_column+1])
                self.count.append(count+1)
            elif state == "d":
                pass
            else: # 数字だった場合
                num = int(state)

        if len(self.que) != 0 and num != -1:
            count = num + count 
            self.end(count)


    def main(self):
        while not self.terminal:
            self.bdf()  

H, W = map(int,input().split())
matrix = []
for _ in range(H):

    matrix.append(list(input()))


bdf = BDF(H,W,matrix)
bdf.main()