Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
S = [input() for _ in range(H)]
global_visited = []

count = [[float("inf")] * W for _ in range(H)]

start = (ch - 1, cw -1)

def judge(start):
    if start in global_visited:
        return False
    if start[0] < 0 or H <= start[0]:
        return False
    if start[1] < 0 or W <= start[1]:
        return False
    # print(start)
    if S[start[0]][start[1]] == '#':
        return False
    return True

que = [start]
now_count = 0
while que:
    local_visited = []
    while que:
        start = que.pop()
        global_visited.append(start)
        local_visited.append(start)
        count[start[0]][start[1]] = now_count
        next_list = [
            (start[0], start[1] + 1),
            (start[0], start[1] - 1),
            (start[0] + 1, start[1]),
            (start[0] - 1, start[1]),
        ]
        for next in next_list:
            if judge(next):
                que.append(next)

    now_count += 1

    while local_visited:
        start = local_visited.pop()
        next_list = [
            (start[0] + 2, start[1] + 2),
            (start[0] + 2, start[1] + 1),
            (start[0] + 2, start[1] + 0),
            (start[0] + 2, start[1] - 1),
            (start[0] + 2, start[1] - 2),
            (start[0] + 1, start[1] + 2),
            (start[0] + 1, start[1] + 1),
            (start[0] + 1, start[1] - 1),
            (start[0] + 1, start[1] - 2),
            (start[0] + 0, start[1] + 2),
            (start[0] + 0, start[1] - 2),
            (start[0] - 1, start[1] + 2),
            (start[0] - 1, start[1] + 1),
            (start[0] - 1, start[1] - 1),
            (start[0] - 1, start[1] - 2),
            (start[0] - 2, start[1] + 2),
            (start[0] - 2, start[1] + 1),
            (start[0] - 2, start[1] + 0),
            (start[0] - 2, start[1] - 1),
            (start[0] - 2, start[1] - 2),
        ]
        for next in next_list:
            if judge(next):
                count[next[0]][next[1]] = now_count
                global_visited.append(next)
                que.append(next)

if count[dh-1][dw-1] == float("inf"):
    print(-1)
else:
    print(count[dh-1][dw-1])

Fixed submission:
H, W = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
S = [input() for _ in range(H)]
global_visited = set()

count = [[float("inf")] * W for _ in range(H)]

start = (ch - 1, cw -1)

def judge(start):
    if start in global_visited:
        return False
    if start[0] < 0 or H <= start[0]:
        return False
    if start[1] < 0 or W <= start[1]:
        return False
    if S[start[0]][start[1]] == '#':
        return False
    return True

que = [start]
now_count = 0
while que:
    local_visited = []
    while que:
        start = que.pop()
        global_visited.add(start)
        local_visited.append(start)
        count[start[0]][start[1]] = now_count
        next_list = [
            (start[0], start[1] + 1),
            (start[0], start[1] - 1),
            (start[0] + 1, start[1]),
            (start[0] - 1, start[1]),
        ]
        for next in next_list:
            if judge(next):
                que.append(next)

    now_count += 1

    while local_visited:
        start = local_visited.pop()
        next_list = [
            (start[0] + 2, start[1] + 2),
            (start[0] + 2, start[1] + 1),
            (start[0] + 2, start[1] + 0),
            (start[0] + 2, start[1] - 1),
            (start[0] + 2, start[1] - 2),
            (start[0] + 1, start[1] + 2),
            (start[0] + 1, start[1] + 1),
            (start[0] + 1, start[1] - 1),
            (start[0] + 1, start[1] - 2),
            (start[0] + 0, start[1] + 2),
            (start[0] + 0, start[1] - 2),
            (start[0] - 1, start[1] + 2),
            (start[0] - 1, start[1] + 1),
            (start[0] - 1, start[1] - 1),
            (start[0] - 1, start[1] - 2),
            (start[0] - 2, start[1] + 2),
            (start[0] - 2, start[1] + 1),
            (start[0] - 2, start[1] + 0),
            (start[0] - 2, start[1] - 1),
            (start[0] - 2, start[1] - 2),
        ]
        for next in next_list:
            if judge(next):
                count[next[0]][next[1]] = now_count
                global_visited.add(next)
                que.append(next)

if count[dh-1][dw-1] == float("inf"):
    print(-1)
else:
    print(count[dh-1][dw-1])