Problem p02803 (python)

Fixed submission:
import numpy as np
from collections import deque
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = 0
for i in range(len(s)):
    for j in range(len(s[0])):
        if s[i][j] == '#':
            continue
        pre = 0
        p = np.full((h, w), -1)
        p[i][j] = 0
        check = deque([((i, j), 0)])#(from,now, dist)
        while check:
            now, d = check.popleft()
            pre = max(pre, d)
            for k, l in [(1,0), (-1,0),(0,1), (0,-1)]:
                b = now[0] + k
                c = now[1] + l
                if b < 0 or c < 0 or b >= h or c >= w:
                    continue 
                elif p[b][c] == -1 and s[b][c] != '#':
                    check.append(((b, c), d + 1))
                    p[b][c] = d + 1
        ans = max(ans, pre)

print(ans)

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

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
s = np.array(s)
s = (s == ".") * 1
def neext(a):
    l, m = a[0], a[1]
    if l == 0:
        if m == 0:
            return [(l, m+1), (l+1, m)]
        elif m == w - 1:
            return [(l, m-1), (l+1, m)]
        else:
            return [(l, m+1), (l, m-1), (l+1, m)]
    elif m == 0:
        if l == h - 1:
            return [(l, m+1), (l-1, m)]
        else:
            return [(l, m+1), (l+1, m), (l-1, m)]
    elif l == h - 1:
        if m == w - 1:
            return [(l, m-1), (l-1, m)]
        else:
            return [(l, m+1), (l, m-1), (l-1, m)]
    elif m == w - 1:
        return [(l, m-1), (l+1, m), (l-1, m)]
    else:
        return [(l, m+1), (l, m-1), (l+1, m), (l-1, m)]
def solve(i, j):
    if s[i][j] == 0:
        return 0
    else:
        pre = 0
        p = np.full_like(s, -1)
        check = deque([((-1, -1), (i, j), 0)])#(from,now, dist)
        while check:
            From, now, d = check.popleft()
            p[now[0]][now[1]] = d
            pre = max(pre, d)
            for b, c in neext((now[0], now[1])):
                if (b == From[0] and c == From[1]) or p[b][c] != -1 or s[b][c] == 0:
                    continue
                else:
                    check.append(((i, j), (b, c), d + 1))
        return pre
ans = 0
for H in range(len(s)):
    for W in range(len(s[0])):
        ans = max(ans, solve(H, W))
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
s = np.array(s)
s = (s == ".") * 1
def neext(a):
    l, m = a[0], a[1]
    if l == 0:
        if m == 0:
            return [(l, m+1), (l+1, m)]
        elif m == w - 1:
            return [(l, m-1), (l+1, m)]
        else:
            return [(l, m+1), (l, m-1), (l+1, m)]
    elif m == 0:
        if l == h - 1:
            return [(l, m+1), (l-1, m)]
        else:
            return [(l, m+1), (l+1, m), (l-1, m)]
    elif l == h - 1:
        if m == w - 1:
            return [(l, m-1), (l-1, m)]
        else:
            return [(l, m+1), (l, m-1), (l-1, m)]
    elif m == w - 1:
        return [(l, m-1), (l+1, m), (l-1, m)]
    else:
        return [(l, m+1), (l, m-1), (l+1, m), (l-1, m)]
def solve(i, j):
    if s[i][j] == 0:
        return 0
    else:
        pre = 0
        p = np.full_like(s, -1)
        check = [((-1, -1), (i, j), 0)]#(from,now, dist)
        while check:
            From, now, d = check.pop(0)
            p[now[0]][now[1]] = d
            pre = max(pre, d)
            for b, c in neext((now[0], now[1])):
                if (b == From[0] and c == From[1]) or p[b][c] != -1 or s[b][c] == 0:
                    continue
                else:
                    check.append(((i, j), (b, c), d + 1))
        return pre
ans = 0
for H in range(len(s)):
    for W in range(len(s[0])):
        ans = max(ans, solve(H, W))
print(ans)