Problem p02579 (python)

Fixed submission:
from collections import deque
H, W= map(int, input().split())
CH, CW= map(int, input().split())
DH, DW= map(int, input().split())
S = [list(map(str, input())) for _ in range(H)]
q = deque([(CH-1, CW-1, 0)])
while q:
    h, w, c = q.popleft()
    if S[h][w] == '.':
        S[h][w] = c
        for i in range(-2, 3):
            for j in range(-2, 3):
                if 0<=h+i<H and 0<=w+j<W and S[h+i][w+j] == '.':
                    if abs(i)+abs(j) == 1:
                        q.appendleft((h+i, w+j, c))
                    elif abs(i)+abs(j) >= 2:
                        q.append((h+i, w+j, c+1))
if S[DH-1][DW-1] == '.':
    print(-1)
else:
    print(S[DH-1][DW-1])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
def myfunc(h, w, c):
    if 0<=h and h<H+2 and 0<=w and w<W+2 and T[h][w] != '#':
        if T[h][w] == '.' or T[h][w] > c:
            T[h][w] = c
            for i in [(0,1), (0,-1), (1,0), (-1,0)]:
                myfunc(h+i[0], w+i[1], c)
            for k in range(-2, 3):
                for l in range(-2, 3):
                    myfunc(h+k, w+l, T[h][w]+1)

H, W= map(int, input().split())
CH, CW= map(int, input().split())
DH, DW= map(int, input().split())
S = ['#'+input()+'#' for _ in range(H)]
S.insert(0,'#'*(W*2))
S.append('#'*(W*2))
T = [[0] * (W+2) for _ in range(H+2)]
for i in range(H+2):
    for j in range(W+2):
        T[i][j] = S[i][j]
myfunc(CH, CW, 0)
if T[DH][DW] == '#' or T[DH][DW] == '.':
    print(-1)
else:
    print(T[DH][DW])

Buggy submission (verdict: Runtime Error):
def myfunc(h, w, c):
    if 0<=h and h<H+2 and 0<=w and w<W+2 and T[h][w] != '#':
        if T[h][w] == '.' or T[h][w] > c:
            T[h][w] = c
            for i in [(0,1), (0,-1), (1,0), (-1,0)]:
                myfunc(h+i[0], w+i[1], c)
            for k in range(-2, 3):
                for l in range(-2, 3):
                    myfunc(h+k, w+l, T[h][w]+1)

H, W= map(int, input().split())
CH, CW= map(int, input().split())
DH, DW= map(int, input().split())
S = ['#'+input()+'#' for _ in range(H)]
S.insert(0,'#'*(W*2))
S.append('#'*(W*2))
T = [[0] * (W+2) for _ in range(H+2)]
for i in range(H+2):
    for j in range(W+2):
        T[i][j] = S[i][j]
myfunc(CH, CW, 0)
if T[DH][DW] == '#' or T[DH][DW] == '.':
    print(-1)
else:
    print(T[DH][DW])

Buggy submission (verdict: Runtime Error):
def myfunc(h, w, c):
    if 0<=h and h<H+2 and 0<=w and w<W+2 and T[h][w] != '#':
        if T[h][w] == '.' or T[h][w] > c:
            T[h][w] = c
            global warp
            for i in [(0,1), (0,-1), (1,0), (-1,0)]:
                myfunc(h+i[0], w+i[1], c)
                for k in range(-2, 3):
                    for l in range(-2, 3):
                        myfunc(h+k, w+l, T[h][w]+1)

H, W= map(int, input().split())
CH, CW= map(int, input().split())
DH, DW= map(int, input().split())
S = ['#'+input()+'#' for _ in range(H)]
S.insert(0,'#'*(W*2))
S.append('#'*(W*2))
T = [[0] * (W+2) for _ in range(H+2)]
for i in range(H+2):
    for j in range(W+2):
        T[i][j] = S[i][j]
myfunc(CH, CW, 0)
warp = True
# while T[DH][DW] == '.' and warp:
#     warp = False
#     for i in range(H+2):
#         for j in range(W+2):
#             if T[i][j] != '.' and T[i][j] != '#':
#                 for k in range(-2, 3):
#                     for l in range(-2, 3):
#                         myfunc(i+k, j+l, T[i][j]+1)
if T[DH][DW] == '#' or T[DH][DW] == '.':
    print(-1)
else:
    print(T[DH][DW])

Buggy submission (verdict: Runtime Error):
def myfunc(h, w, c):
    if 0<=h and h<H+2 and 0<=w and w<W+2 and T[h][w] == '.':
        T[h][w] = c
        global warp
        warp = True
        for i in [(0,1), (0,-1), (1,0), (-1,0)]:
            myfunc(h+i[0], w+i[1], c)

H, W= map(int, input().split())
CH, CW= map(int, input().split())
DH, DW= map(int, input().split())
S = ['#'+input()+'#' for _ in range(H)]
S.insert(0,'#'*(W*2))
S.append('#'*(W*2))
T = [[0] * (W+2) for _ in range(H+2)]
for i in range(H+2):
    for j in range(W+2):
        T[i][j] = S[i][j]
myfunc(CH, CW, 0)
warp = True
while T[DH][DW] == '.' and warp:
    warp = False
    for i in range(H+2):
        for j in range(W+2):
            if T[i][j] != '.' and T[i][j] != '#':
                for k in range(-2, 3):
                    for l in range(-2, 3):
                        myfunc(i+k, j+l, T[i][j]+1)
if warp == False:
    print(-1)
else:
    print(T[DH][DW])