Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
ch, cw = [int(i) - 1 for i in  input().split()]
dh, dw = [int(i) - 1 for i in  input().split()]
maze = []
for _ in range(H):
    a = input()
    maze.append(a)
d = deque([(ch, cw, 0)])
value = [[-1 for _ in range(W)] for _ in range(H)]
while d:
    h, w, v = d.popleft()
    if value[h][w] == -1:
        value[h][w] = v
    for hi in range(-2, 3):
        for wi in range(-2, 3):
            next_h = h + hi
            next_w = w + wi
            if (0 <= next_h < H) & (0 <= next_w < W):                
                if (maze[next_h][next_w] == '.'):
                    if (abs(hi) + abs(wi) == 1) & (value[next_h][next_w] == -1):
                        d.appendleft((next_h, next_w, v))
                    elif (abs(hi) + abs(wi) > 1) & (value[next_h][next_w] == -1):
                        d.append((next_h, next_w, v + 1))
print(value[dh][dw])

Fixed submission:
from collections import deque
H, W = map(int, input().split())
ch, cw = [int(i) - 1 for i in  input().split()]
dh, dw = [int(i) - 1 for i in  input().split()]
maze = []
for _ in range(H):
    a = input()
    maze.append(a)
d = deque([(ch, cw, 0)])
value = [[-1 for _ in range(W)] for _ in range(H)]
while d:
    h, w, v = d.popleft()
    if value[h][w] == -1:
        value[h][w] = v
        for hi in range(-2, 3):
            for wi in range(-2, 3):
                next_h = h + hi
                next_w = w + wi
                if (0 <= next_h < H) & (0 <= next_w < W):                
                    if (maze[next_h][next_w] == '.'):
                        if (abs(hi) + abs(wi) == 1) & (value[next_h][next_w] == -1):
                            d.appendleft((next_h, next_w, v))
                        elif (abs(hi) + abs(wi) > 1) & (value[next_h][next_w] == -1):
                            d.append((next_h, next_w, v + 1))
    else:
        continue
print(value[dh][dw])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
ch, cw = [int(i) - 1 for i in  input().split()]
dh, dw = [int(i) - 1 for i in  input().split()]
maze = []
for _ in range(H):
    a = input()
    maze.append(a)
d = deque([(ch, cw, 0)])
value = [[-1 for _ in range(W)] for _ in range(H)]
while d:
    h, w, v = d.popleft()
    if value[h][w] == -1:
        value[h][w] = v
    for hi in range(-2, 3):
        for wi in range(-2, 3):
            next_h = h + hi
            next_w = w + wi
            if (0 <= next_h < H) & (0 <= next_w < W):                
                if (maze[next_h][next_w] == '.'):
                    if (abs(hi) + abs(wi) == 1) & (value[next_h][next_w] == -1):
                        d.appendleft((next_h, next_w, v))
                    elif (abs(hi) + abs(wi) > 1) & (value[next_h][next_w] == -1):
                        d.append((next_h, next_w, v + 1))
print(value[dh][dw])

Fixed submission:
from collections import deque
H, W = map(int, input().split())
ch, cw = [int(i) - 1 for i in  input().split()]
dh, dw = [int(i) - 1 for i in  input().split()]
maze = []
for _ in range(H):
    a = input()
    maze.append(a)
d = deque([(ch, cw, 0)])
value = [[-1 for _ in range(W)] for _ in range(H)]
while d:
    h, w, v = d.popleft()
    if value[h][w] == -1:
        value[h][w] = v
        for hi in range(-2, 3):
            for wi in range(-2, 3):
                next_h = h + hi
                next_w = w + wi
                if (0 <= next_h < H) & (0 <= next_w < W):                
                    if (maze[next_h][next_w] == '.'):
                        if (abs(hi) + abs(wi) == 1) & (value[next_h][next_w] == -1):
                            d.appendleft((next_h, next_w, v))
                        elif (abs(hi) + abs(wi) > 1) & (value[next_h][next_w] == -1):
                            d.append((next_h, next_w, v + 1))
    else:
        continue
print(value[dh][dw])