Problem p02803 (python)

Fixed submission:
# -*- coding: utf-8 -*-

import numpy as np
import copy
from collections import deque

n, m = map(int, input().split())

# マス目と距離保存のための箱を作成
d = []
mas = []
for i in range(n):
    mas.extend(list(map(list, input().split())))
    d.append([float("nan")] * m)


def tansaku(sx, sy, d):
    local_d = copy.deepcopy(d)
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sx, sy))
    local_d[sx][sy] = 0
    while que:
        # キューの先頭を取り出す
        p = que.popleft()
        for i in range(4):
            # 移動4方向をループ
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < n
                and 0 <= ny < m
                and mas[nx][ny] != "#"
                and not local_d[nx][ny] == local_d[nx][ny]
            ):
                # 移動できるならキューに入れ，その点の距離を p からの距離 +1 で確定する．最後Nan判定
                que.append((nx, ny))
                local_d[nx][ny] = local_d[p[0]][p[1]] + 1
    d_max = int(np.nanmax(np.array(local_d)))
    return d_max


dis = 0
for i in range(n):
    for j in range(m):
        if mas[i][j] != "#":
            dis_i = tansaku(i, j, d)
            if dis_i > dis:
                dis = copy.copy(dis_i)

print(dis)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
 
import numpy as np
import copy
from collections import deque
 
n, m = map(int, input().split())
 
# マス目と距離保存のための箱を作成
d = []
mas = []
for i in range(n):
    mas.extend(list(map(list, input().split())))
    d.append([float("nan")] * m)
 
 
def tansaku(sx, sy, d):
    local_d = copy.deepcopy(d)
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
 
    que = deque([])
    que.append((sx, sy))
    local_d[sx][sy] = 0
    while que:
        # キューの先頭を取り出す
        p = que.popleft()
        for i in range(4):
            # 移動4方向をループ
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < n
                and 0 <= ny < m
                and mas[nx][ny] != "#"
                and not local_d[nx][ny] == local_d[nx][ny]
            ):
                # 移動できるならキューに入れ，その点の距離を p からの距離 +1 で確定する．最後Nan判定
                que.append((nx, ny))
                local_d[nx][ny] = local_d[p[0]][p[1]] + 1
    d_max = max(local_d)
    return d_max
 
 
dis = 0
for i in range(n):
  	for j in range(m):
	    dis_i = tansaku(i, j, d)
    	if dis_i > dis:
        	dis = copy.copy(dis_i) 
 
print(dis)

Buggy submission (verdict: Runtime Error):
from collections import deque

n, m = map(int, input().split())
d = []

mas = []
    for i in range(n):
        mas.extend(list(map(list, input().split())))
        d.append([float("inf")] * m)


def tansaku(sx, sy, d):

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

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0
    while que:
        # キューの先頭を取り出す
        p = que.popleft()
        for i in range(4):
            # 移動4方向をループ
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < n
                and 0 <= ny < m
                and mas[nx][ny] != "#"
                and d[nx][ny] == float("inf")
            ):
                # 移動できるならキューに入れ，その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
        d_max = max(list([max(d[0]), max(d[n-1]), max(d[:][0]), max(d[:][m-1])]))
    return d_max

dis = 0
for i in range(n):
    dis_i = tansaku(i, 0,d)
    if dis_i > dis:
        dis = dis_i
    dis_i = tansaku(i, m-1,d)
    if dis_i > dis:
        dis = dis_i

for i in range(m):
    dis_i = tansaku(0, i,d)
    if dis_i > dis:
        dis = dis_i
    dis_i = tansaku(n-1, i,d)
    if dis_i > dis:
        dis = dis_i

print(dis)