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 copy
import numpy as np
H,W = map(int, input().split())
hoge1 = []
for i in range(0,H):
	a = input()
	x = [a[i:i+1] for i in range(len(a))]
	hoge1.append(x)

def procb(h,w,hoge1,hoge2):
	if hoge1[h][w] == '#':
		if h == 0 and w == 0:
			hoge2[h][w+1] = '#'
			hoge2[h+1][w] = '#'
		elif h == 0 and w == W-1:
			hoge2[h][w-1] = '#'
			hoge2[h+1][w]   = '#'
		elif h == H-1 and w == 0:
			hoge2[h-1][w] = '#'
			hoge2[h][w+1] = '#'
		elif h == H-1 and w == W-1:
			hoge2[h-1][w] = '#'
			hoge2[h][w-1] = '#'
		elif h == 0:
			hoge2[h][w-1] = '#'
			hoge2[h][w+1] = '#'
			hoge2[h+1][w] = '#'
		elif h == H-1:
			hoge2[h][w-1] = '#'
			hoge2[h][w+1] = '#'
			hoge2[h-1][w] = '#'
		elif w == 0:
			hoge2[h-1][w] = '#'
			hoge2[h+1][w] = '#'
			hoge2[h][w+1] = '#'
		elif w == W-1:
			hoge2[h-1][w] = '#'
			hoge2[h+1][w] = '#'
			hoge2[h][w-1] = '#'
		else:
			hoge2[h-1][w] = '#'
			hoge2[h+1][w] = '#'
			hoge2[h][w-1] = '#'
			hoge2[h][w+1] = '#'


cnt = 0
hoge2 = copy.deepcopy(hoge1)
epoc_list = range(max(H,W))
h_list = range(H)
w_list = range(W)

for epoc in epoc_list:
	for h in h_list:
		for w in w_list:
			procb(h,w,hoge1,hoge2)

	hoge1 = copy.deepcopy(hoge2)
	cnt += 1

	if np.all(np.array(hoge1) == '#'):
		break

print(cnt)

Submission status: Runtime Error

Solution code:
import copy

def is_complete(boad):
    for col in boad:
        if ['.'] in col:
            return False
    return True

def change_cell(cell):
    if cell == ['#']:
        return ['#']
    return ['#']

def change_around_cells(boad, h, w, x, y):
    # 5を中心とした座標の辞書
    index_dic = {1:(-1, 0), 2:(0, -1), 3:(0, 1), 4:(1, 0)}
    # 一番左の列に中心
    if x - 1 < 0:
        index_dic.pop(1)

    # 一番右の列に中心
    if x + 1 >= w:
        index_dic.pop(4)

    # 一番上の行に中心
    if y - 1 < 0:
        index_dic.pop(2)

    # 一番下の行に中心
    if y + 1 >= h:
        index_dic.pop(3)

    for key in index_dic.keys():
        i, j = index_dic[key]
        boad[y + j][x + i] = change_cell(boad[y + j][x + i])


    return boad


def main():
    #マス目入力
    input_data = input().split()
    h, w = (int(i) for i in input_data)
    # print(h,w)
    #盤面入力
    boad = []
    for i in range(h):
        boad.append(list(list(i) for i in input()))
    # print(boad)

    new_boad = copy.deepcopy(boad)

    cnt = 0
    complete_flag = is_complete(boad)
    while(not complete_flag):
        cnt += 1
        for y, col in enumerate(boad):
            if ['#'] in col:
                indexes = [i for i, d in enumerate(list) if d == ['#']]
                for x in indexes:
                    cell = col[x]
                    if cell == ['#']:
                        new_boad = change_around_cells(new_boad, h, w, x, y)
                        complete_flag = is_complete(new_boad)
                        if complete_flag:
                            break
                    if ['#'] not in col[x:]:
                        break
            if complete_flag:
                break
        boad = copy.deepcopy(new_boad)
        # for b in new_boad:
        #     print(b)
        # print("++++++++")
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
h,w = list(map(int, input().split()))
a = [list(input().replace("#", "0")) for i in range(h)]
count = 1
while 1:
    for i in range(h):
        for j in range(w):
            if a[i][j] == str(count - 1):
                if j == 0:
                    a[i][j + 1] = str(count)
                elif j == w-1:
                    a[i][j - 1] = str(count)
                else:
                    a[i][j - 1] = str(count)
                    a[i][j + 1] = str(count)
    for j in range(w):
        for i in range(h):
            if a[i][j] == str(count - 1):
                if i ==0:
                    a[i+1][j] = str(count)
                elif i == h-1:
                    a[i-1][j] = str(count)
                else:
                    a[i-1][j] = str(count)
                    a[i+1][j] = str(count)
    if all(a[i][j] != "." for i in range(h) for j in range(w)):
        break
    else:
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

inf = float('inf')

def solve(H: int, W: int, A: "List[str]"):
    dist = [[inf] * W for h in range(H)]
    for h in range(1, H):
        for w in range(1, W):
            if A[h][w] == '#':
                dist[h][w] = 0
            else:
                dist[h][w] = min(dist[h-1][w], dist[h][w-1]) + 1
    
    dmax = 0
    for h in range(H-1, -1, -1):
        for w in range(W-1, -1, -1):
            if A[h][w] == '#':
                dist[h][w] = 0
            elif h+1 <= H-1 and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], min(dist[h+1][w], dist[h][w+1]) + 1)
            elif w+1 == W and h+1 <= H-1:
                dist[h][w] = min(dist[h][w], dist[h+1][w] + 1)
            elif h+1 == H and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], dist[h][w+1] + 1)
            dmax = max(dist[h][w], dmax)
    print(dmax) 
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
def darker(ary, i, j):
    ary[i][j] = '#'
    if i > 0:
        ary[i - 1][j] = '#'
    if i < W-1:
        ary[i + 1][j] = '#'
    if j > 0:
        ary[i][j - 1] = '#'
    if j < H-1:
        ary[i][j + 1] = '#'
    return ary

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

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


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

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

Submission status: Runtime Error

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


def paint(a):      
    for i in range(h-2):
        for j in range(w-2):
            if a[i+1][j+1]=="#":
                a[i][j+1]="#"
                a[i+2][j+1]="#"
                a[i+1][j]="#"
                a[i+1][j+2]="#"
    for p in range(h-2):
        if a[p+1][0]=="#":
            a[p][0]="#"
            a[p+1][1]="#"
            a[p+2][0]="#"
        if a[p+1][w-1]=="#":
            a[p][w-1]="#"
            a[p+1][w-2]="#"
            a[p+2][w-1]="#"
    for q in range(w-2):
        if a[0][q+1]=="#":
            a[0][q]="#"
            a[1][q+1]="#"
            a[0][q+2]="#"
        if a[h-1][q+1]=="#":
            a[h-1][q]="#"
            a[h-2][q+1]="#"
            a[h-1][q+2]="#"
            
    if a[0][0]=="#":
        a[0][1]="#"
        a[1][0]="#"
    elif a[h-1][0]=="#":
        a[h-1][1]="#"
        a[h-2][0]="#"
    elif a[0][w-1]=="#":
        a[0][w-2]="#"
        a[1][w-1]="#"
    elif [h-1][w-1]=="#":
        a[h-2][w-1]="#"
        a[h-1][w-2]="#"
        
    for k in range(h):
        for l in range(w):
            if a[k][l]==".":
                return 1
    return 0



ans=1
t=1
while t==1:
    t=paint(a)
    ans+=1

print(ans)

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

Solution code:
number = input().split()

number1 = int(number[0])
number2 = int(number[1])

lists = []

for i in range(number2):
    block = input()
    j = 0
    for i in range(number1):
        lists.append(block[j])
        j += 1

count = 0        
for i in lists:
    if i == "#":
        count += 1
        
if count > number1 * number2 / 2:
    print(2)
else:
    print(3)
    
#print(lists)

Submission status: Runtime Error

Solution code:
import numpy as np


def main():
    # H, W = [int(x) for x in input().split()]
    H, W = map(int, [int(x) for x in input().split()])
    board = [[0 for i in range(H)] for j in range(W)]

    """ #は1, .は0 """
    for i in range(H):
        X = input()
        for j in range(len(X)):
            if X[j] == "#":
                board[i][j] = 1

    num_board = np.array(board)
    height, width = num_board.shape[:2]
    count = 0
    while (np.sum(num_board) != height*width):
        num_board_ans = np.zeros((height, width))
        for i in range(height):
            for j in range(width):
                if num_board[i, j] == 1:
                    num_board_ans[i, j] = 1
                    a = max(i-1, 0)
                    num_board_ans[a, j] = 1
                    b = max(j-1, 0)
                    num_board_ans[i, b] = 1
                    c = min(i+1, height-1)
                    num_board_ans[c, j] = 1
                    d = min(j+1, width-1)
                    num_board_ans[i, d] = 1
        num_board = num_board_ans
        count += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()

def mv(i,sr,sc):
    if S[i]=='L':
        sc -= 1
    elif S[i]=='R':
        sc += 1
    elif s[i]=='U':
        sr -= 1
    else:
        sr += 1

def SSS(i,sr,sc):
    if S[i]=='L':
        if sc <= W/2:
            mv(i,sr,sc)
    elif S[i]=='R':
        if sc >= W/2:
            mv(i,sr,sc)
    elif S[i]=='U':
        if sr <= H/2:
            mv(i,sr,sc)
    else:
        if sr >= H/2:
            mv(i,sr,sc)

def TTT(i,sr,sc):
    if T[i]=='L':
        if sc > W/2:
            mv(i,sr,sc)
    elif T[i]=='R':
        if sc < W/2:
            mv(i,sr,sc)
    elif T[i]=='U':
        if sr > H/2:
            mv(i,sr,sc)
    else:
        if sr < H/2:
            mv(i,sr,sc)
    
for i in range(N):
    if sc>W or sc<1 or sr>H or sr<1:
        print('NO')
        exit()
    else:
        SSS(i,sr,sc)
        TTT(i,sr,sc)

if sc>W or sc<1 or sr>H or sr<1:
    print('NO')
else:
    print('YES')

Submission status: Runtime Error

Solution code:
import numpy as np


def main():
    # H, W = [int(x) for x in input().split()]
    H, W = map(int, [int(x) for x in input().split()])
    board = [[0 for i in range(H)] for j in range(W)]

    """ #は1, .は0 """
    for i in range(H):
        X = input()
        for j in range(len(X)):
            if X[j] == "#":
                board[i][j] = 1

    num_board = np.array(board)
    height, width = num_board.shape[:2]
    count = 0
    while (np.sum(np.array(board)) != height*width):
        num_board_ans = board
        for i in range(height):
            for j in range(width):
                if board[i][j] == 1:
                    num_board_ans[i][j] = 1
                    a = max(i-1, 0)
                    num_board_ans[a][j] = 1
                    b = max(j-1, 0)
                    num_board_ans[i][b] = 1
                    c = min(i+1, height-1)
                    num_board_ans[c][j] = 1
                    d = min(j+1, width-1)
                    num_board_ans[i][d] = 1
        board = num_board_ans
        count += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#3 3
#...
#.#.
#...
number = input().split()

number1 = int(number[0])
number2 = int(number[1])

lists = []

for i in range(number2):
    block = input()
    j = 0
    for i in range(number1):
        lists.append(block[j])
        j += 1

count = 0

j = 0
for i in lists:
    if i == "#":
        #print(j)
        #print(j + number1)
        lists[j - number1] = "#"
        #lists[j + number1] = "#"
        #print(j + number1)
        #lists[j + 1] = "#"
        lists[j - 1] = "#"
        for i in range(number2):
            if j + 1 == number2 * (i + 1):
                listss = []
                #print(j + 1)
                #print(j - 1)
                #print("right")
        for i in range(number2):
            if j  == number2 * i or j - 1 == 0:
                listsss = []
                #print(j + 1)
                #print(j - 1)
                #print("left")
        break
    j += 1
        
for i in lists:
    if i == "#":
        count += 1
        
if count > number1 * number2 / 2:
    print(2)
else:
    print(3)
    
#print(lists)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

inf = float('inf')

def solve(H: int, W: int, A: "List[str]"):
    dist = [[inf] * W for h in range(H)]
    for h in range(1, H):
        for w in range(1, W):
            if A[h][w] == '#':
                dist[h][w] = 0
            else:
                dist[h][w] = min(dist[h-1][w], dist[h][w-1]) + 1
    
    dmax = 0
    for h in range(H-1, -1, -1):
        for w in range(W-1, -1, -1):
            if A[h][w] == '#':
                dist[h][w] = 0
            elif h+1 <= H-1 and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], min(dist[h+1][w], dist[h][w+1]) + 1)
            elif w+1 == W and h+1 <= H-1:
                dist[h][w] = min(dist[h][w], dist[h+1][w] + 1)
            elif h+1 == H and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], dist[h][w+1] + 1)
            else:
                dist[h][w] = dist[h][w]
            dmax = max(dist[h][w], dmax)
    print(dmax) 
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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(W)]
for i in range(W):
  inp = str(input())
  A[i] = list(inp)

black_list = []
white_list = []

ans = 0
for i in range(W):
  for j in range(H):
    if A[i][j] == "#":
      black_list.append([i,j])
    else:
      white_list.append([i,j])

if len(white_list) != 0:
  for white_dot in white_list:
    min_distance = W+H
    for black_dot in black_list:
      min_distance = min(min_distance, abs(black_dot[0]-white_dot[0])+abs(black_dot[1]-white_dot[1]))
    ans = max(ans,min_distance)

print(ans)

Submission status: Runtime Error

Solution code:
import copy

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

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

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

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

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

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

flag=0

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


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

print(flag)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#3 3
#...
#.#.
#...
number = input().split()

number1 = int(number[0])
number2 = int(number[1])

lists = []

for i in range(number2):
    block = input()
    j = 0
    for i in range(number1):
        lists.append(block[j])
        j += 1

count = 0

j = 0
for i in lists:
    if i == "#":
        #print(j)
        #print(j + number1)
        lists[j - number1] = "#"
        lists[j + number1] = "#"
        #print(j + number1)
        lists[j + 1] = "#"
        lists[j - 1] = "#"
        for i in range(number2):
            if j + 1 == number2 * (i + 1):
                listss = []
                #print(j + 1)
                #print(j - 1)
                #print("right")
        for i in range(number2):
            if j  == number2 * i or j - 1 == 0:
                listsss = []
                #print(j + 1)
                #print(j - 1)
                #print("left")
        break
    j += 1
        
for i in lists:
    if i == "#":
        count += 1
        
if count > number1 * number2 / 2:
    print(2)
else:
    print(3)
    
#print(lists)

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

 
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:
n, m = map(int, input().split())

b = {}
w = {}
for i in range(n):
  line = input()
  for j in range(m):
    if line[j] == "#":
      b[(i, j)] = 1
    else:
      w[(i, j)] = 1

arr = []
for (wi, wj) in w:
  li = []
  for (bi, bj) in b:
  	li.append(abs(wi - bi) + abs(wj - bj))
  arr.append(min(li))

print(max(arr))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(H: int, W: int, A: "List[str]"):
    holes = [[float('inf')] * W for _ in range(H)]
    for i,l in enumerate(A):
        for j, c in enumerate(l):
            if c == '#':
                holes[i][j] = 0
    for i in range(H):
        for j in range(1, W):
            holes[i][j] = min(holes[i][j], holes[i][j-1] + 1)
        for j in range(W - 2, -1, -1):
            holes[i][j] = min(holes[i][j], holes[i][j+1] + 1)

    for i in range(1, H):
        for j in range(W):
            holes[i][j] = min(holes[i][j], holes[i-1][j] + 1)
    for i in range(H - 2, -1, -1):
        for j in range(W):
            holes[i][j] = min(holes[i][j], holes[i+1][j] + 1)

    ret = -float('inf')
    for l in holes:
        ret = max(ret, max(l))
    return ret


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]"
    result = solve(H, W, A)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8

from collections import deque

h, w = [int(x) for x in input().split()]

i_mat = []

queue = deque([])


for _ in range(h):
    x = input()
    i_mat.append([0 if y == "#" else 9999 for y in list(x)])

    for (i, t) in enumerate(i_mat[-1]):
        if t == 0:
            queue.append((len(i_mat)-1, i))


result = 0

while queue:
    index = queue.popleft()
    curr = i_mat[index[0]][index[1]]
    if index[0]-1 >= 0 and i_mat[index[0]-1][index[1]] > curr + 1:
        i_mat[index[0]-1][index[1]] = curr + 1
        queue.append((index[0]-1, index[1]))
    if index[1]-1 >= 0 and i_mat[index[0]][index[1]-1] > curr + 1:
        i_mat[index[0]][index[1]-1] = curr + 1
        queue.append((index[0], index[1]-1))
    if index[0]+1 < w and i_mat[index[0]+1][index[1]] > curr + 1:
        i_mat[index[0]+1][index[1]] = curr + 1
        queue.append((index[0]+1, index[1]))
    if index[1] + 1 < h and i_mat[index[0]][index[1]+1] > curr + 1:
        i_mat[index[0]][index[1]+1] = curr + 1
        queue.append((index[0], index[1]+1))

    if result < curr:
        result = curr

print(result)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
a,s=[],[["x"for i in range(w+2)]]
for i in range(h):
  t,tt=input(),[]
  for j in range(w):
    if t[j]=="#":a.append([i+1,j+1])
    tt.append(t[j])
  s.append(["x"]+tt+["x"])
s.append(["x"for i in range(w+2)])
f=1
while 1:
  b=[]
  for k in a:
    i,j=k[0],k[1]
    if s[i-1][j]==".":s[i-1][j]=f;b.append([i-1,j])
    if s[i+1][j]==".":s[i+1][j]=f;b.append([i+1,j])
    if s[i][j-1]==".":s[i][j-1]=f;b.append([i,j-1])
    if s[i][j+1]==".":s[i][j+1]=f;b.append([i,j+1])
  a=b.copy()
  f+=1
  if len(a)==0:break
print(f-2)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
# グラフの最長経路が-1か-2かを選べる
N = int(input())
edge = [[] for _ in range(N)]
INF = 10**18
for _ in range(N-1):
  a,b = map(int,input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

opposite = 0
max_dist = 0
def f(d,x):
  global max_dist,opposite
  dx = d[x]
  if dx > max_dist:
    opposite = x
    max_dist = dx
  for y in edge[x]:
    if d[y] == INF:
      d[y] = dx + 1
      f(d,y)

# 0からの距離
d = [INF] * N
d[0] = 0
f(d,0)

v = opposite
d = [INF] * N
d[v] = 0
f(d,v)

X = max_dist

ans = 'First' if max_dist%3 != 1 else 'Second'
print(ans)

Submission status: Runtime Error

Solution code:

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

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

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

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

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

print(ans)

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)

black_list = []
white_list = []

ans = 0
for i in range(W):
  for j in range(H):
    if A[i][j] == "#":
      black_list.append([i,j])
    else:
      white_list.append([i,j])

distances = []

if len(white_list) != 0:
  for white_dot in white_list:
    tmp_distance = []
    for black_dot in black_list:
      tmp_distance.append(abs(black_dot[0]-white_dot[0])+abs(black_dot[1]-white_dot[1]))
    distances.append(min(tmp_distance))

ans = max(distances)

print(ans)

Submission status: Runtime Error

Solution code:
#3 3
#...
#.#.
#...
number = input().split()

number1 = int(number[0])
number2 = int(number[1])

lists = []

for i in range(number1):
    block = input()
    j = 0
    for i in range(number1):
        lists.append(block[j])
        j += 1

count = 0

j = 0
for i in lists:
    if i == "#":
        #print(j)
        #print(j + number1)
        lists[j - number1] = "#"
        lists[j + number1] = "#"
        #print(j + number1)
        lists[j + 1] = "#"
        lists[j - 1] = "#"
        for i in range(number2):
            if j + 1 == number2 * (i + 1):
                listss = []
                #print(j + 1)
                #print(j - 1)
                #print("right")
        for i in range(number2):
            if j  == number2 * i or j - 1 == 0:
                listsss = []
                #print(j + 1)
                #print(j - 1)
                #print("left")
        break
    j += 1
        
for i in lists:
    if i == "#":
        count += 1
        
if count > number1 * number2 / 2:
    print(2)
else:
    print(3)
    
#print(lists)

Submission status: Runtime Error

Solution code:
for i in range(W):
  for j in range(H):
    if A[i][j] == "#":
      black_list.append([i,j])
    else:
      white_list.append([i,j])

distances = []

if len(white_list) != 0:
  for white_dot in white_list:
    tmp_distance = []
    for black_dot in black_list:
      tmp_distance.append(abs(black_dot[0]-white_dot[0])+abs(black_dot[1]-white_dot[1]))
    distances.append(min(tmp_distance))

ans = max(distances)

print(ans)

Submission status: Runtime Error

Solution code:
import copy

# 全てが黒かどうかチェックする
def checkEnd():
  for h in range(H):
    for w in range(W):
      if(A[h][w] == "0"):
        return False
  return True

# 周辺を黒色に変更する
def changeColor(i,j):
  print(str(i) + ", " + str(j))
  if(i>0):
    A[i-1][j] = "1"
  if(i<H-1):
    A[i+1][j] = "1"
  if(j>0):
    A[i][j-1] = "1"
  if(j<W-1):
    A[i][j+1] = "1"
  print(A)

  
# 入力を読み込み２次元配列を作成
i=list(map(int, input().split()))
H=i[0]
W=i[1]
A = [[0]*H]*W
for h in range(H):
  # 白は0に黒は1に置換する
  A[h]=list(input().replace("#","1").replace(".","0"))


change_count=0
while True:
  if checkEnd():
    break
    
  change_count+=1
  A_base=copy.deepcopy(A)
  
  for h in range(H):
    for w in range(W):
      if((A[h][w] == "1") and (A_base[h][w] == "1")):
        changeColor(h,w)
        
print(change_count)

Submission status: Runtime Error

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

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

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

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

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

print(ans)

Submission status: Runtime Error

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

blacks = []
whites = []
for i in range(H):
    for j in range(W):
        if Awh[i][j] == '#':
            blacks.append((i, j))
        else:
            whites.append((i, j))

from collections import deque

inf = 10**9
scores = [[inf] * W for _ in range(H)]
for b in blacks:
    queue = deque([])
    mark = [[0] * W for _ in range(H)]
    queue.append((b, 1))
    while queue:
        cxy, dist = queue.popleft()
        cx, cy = cxy
        for y, x in ((cy-1,cx), (cy+1,cx), (cy,cx-1), (cy,cx+1)):
            if y < 0 or x < 0 or y >= H or x >= W:
                continue
            if Awh[y][x] == '#':
                continue
            if mark[y][x] > 0:
                continue

            mark[y][x] = dist
            if scores[y][x] > dist:
                scores[y][x] = dist
            queue.append(((y, x), dist+1))

print(max(v for ss in scores for v in ss if v < inf))