Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))
S[Ch][Cw] = 0

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        heapq.heappush(h, (newd, newh, neww))        
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))
S[Ch][Cw] = 0

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        heapq.heappush(h, (newd, newh, neww))        
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [[c for c in input()] for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] == ".":
                    S[newh][neww] = newd
                    heapq.heappush(h, (newd, newh, neww))
                elif S[newh][neww] == "#":
                    continue
                else:
                    if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        heapq.heappush(h, (newd, newh, neww))
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [[c for c in input()] for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] == ".":
                    S[newh][neww] = str(newd)
                    heapq.heappush(h, (newd, newh, neww))
                elif S[newh][neww] == "#":
                    continue
                else:
                    if int(S[newh][neww]) > newd:
                        S[newh][neww] = str(newd)
                        heapq.heappush(h, (newd, newh, neww))
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [[c for c in input()] for _ in range(H)]


h = []
heapq.heappush(h, (0, Ch, Cw))

while len(h) != 0:
    d, nh, nw = heapq.heappop(h)
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] == ".":
                    S[newh][neww] = str(newd)
                    heapq.heappush(h, (newd, newh, neww))
                elif S[newh][neww] == "#":
                    continue
                else:
                    if int(S[newh][neww]) > newd:
                        S[newh][neww] = str(newd)
                        heapq.heappush(h, (newd, newh, neww))
print(-1)


Fixed submission:
from collections import deque

INF = 10 ** 9

H, W = map(int, input().split())
Ch, Cw = map(lambda x:int(x)-1, input().split())
Dh, Dw = map(lambda x:int(x)-1, input().split())
S = [list(map(lambda x: INF if x == "." else -1, [c for c in input()])) for _ in range(H)]


"""h = []
heapq.heappush(h, (0, Ch, Cw))"""

q = deque()
q.append((0, Ch, Cw))
S[Ch][Cw] = 0

while len(q) != 0:
    d, nh, nw = q.popleft()
    if nh == Dh and nw == Dw:
        print(d)
        exit()

    for dh in [-2, -1, 0, 1, 2]:
        for dw in [-2, -1, 0, 1, 2]:
            flag = False
            if dh == 0 and dw == 0:
                continue
            newd = d + 1
            if abs(dh) + abs(dw) == 1:
                newd = d
                flag = True
            newh = nh + dh
            neww = nw + dw
            if 0 <= newh < H and 0 <= neww < W:
                if S[newh][neww] > newd:
                        S[newh][neww] = newd
                        if flag:
                            q.appendleft((newd, newh, neww))
                        else:
                            q.append((newd, newh, neww))   
print(-1)