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

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

ch -= 1
cw -= 1
dh -= 1
dw -= 1

s = []
for _ in range(h):
    s.append(input())

mtx = [[10 ** 9]*w for _ in range(h)]

cnt = -1
now = deque()
nxt = deque()
nxt.append((ch, cw))
mtx[ch][cw] = 0

while True:
    if not now:
        now = nxt
        cnt += 1
        nxt = deque()
        if not now:
            break
    h_ptr, w_ptr = now.pop()
    if h_ptr != 0:
        if s[h_ptr-1][w_ptr] == '.':
            if cnt < mtx[h_ptr-1][w_ptr]:
                mtx[h_ptr-1][w_ptr] = cnt
                now.append((h_ptr-1, w_ptr))
    if w_ptr != 0:
        if s[h_ptr][w_ptr-1] == '.':
            if cnt < mtx[h_ptr][w_ptr-1]:
                mtx[h_ptr][w_ptr-1] = cnt
                now.append((h_ptr, w_ptr-1))
    if h_ptr != h-1:
        if s[h_ptr+1][w_ptr] == '.':
            if cnt < mtx[h_ptr+1][w_ptr]:
                mtx[h_ptr+1][w_ptr] = cnt
                now.append((h_ptr+1, w_ptr))
    if w_ptr != w-1:
        if s[h_ptr][w_ptr+1] == '.':
            if cnt < mtx[h_ptr][w_ptr+1]:
                mtx[h_ptr][w_ptr+1] = cnt
                now.append((h_ptr, w_ptr+1))
    for i in range(h_ptr-2, h_ptr+3):
        for j in range(w_ptr-2, w_ptr+3):
            if 0 <= i < h and 0 <= j < w and s[i][j] == '.':
                if cnt + 1 < mtx[i][j]:
                    mtx[i][j] = cnt + 1
                    nxt.append((i, j))

ret = mtx[dh][dw]

if ret == 10 ** 9:
    print(-1)
else:
    print(ret)


Fixed submission:
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())

ch -= 1
cw -= 1
dh -= 1
dw -= 1

s = []
for _ in range(h):
    s.append(input())

mtx = [[10 ** 9]*w for _ in range(h)]

cnt = -1
now = deque()
nxt = deque()
nxt.append((ch, cw))
mtx[ch][cw] = 0

while True:
    flag = False
    if not now:
        now = nxt
        cnt += 1
        nxt = deque()
        if not now:
            break
    h_ptr, w_ptr = now.pop()
    if h_ptr != 0:
        if s[h_ptr-1][w_ptr] == '.':
            if cnt < mtx[h_ptr-1][w_ptr]:
                mtx[h_ptr-1][w_ptr] = cnt
                now.append((h_ptr-1, w_ptr))
    if w_ptr != 0:
        if s[h_ptr][w_ptr-1] == '.':
            if cnt < mtx[h_ptr][w_ptr-1]:
                mtx[h_ptr][w_ptr-1] = cnt
                now.append((h_ptr, w_ptr-1))
    if h_ptr != h-1:
        if s[h_ptr+1][w_ptr] == '.':
            if cnt < mtx[h_ptr+1][w_ptr]:
                mtx[h_ptr+1][w_ptr] = cnt
                now.append((h_ptr+1, w_ptr))
    if w_ptr != w-1:
        if s[h_ptr][w_ptr+1] == '.':
            if cnt < mtx[h_ptr][w_ptr+1]:
                mtx[h_ptr][w_ptr+1] = cnt
                now.append((h_ptr, w_ptr+1))
    for i in range(h_ptr-2, h_ptr+3):
        for j in range(w_ptr-2, w_ptr+3):
            if 0 <= i < h and 0 <= j < w and s[i][j] == '.':
                if cnt + 1 < mtx[i][j]:
                    mtx[i][j] = cnt + 1
                    nxt.append((i, j))

ret = mtx[dh][dw]

if ret == 10 ** 9:
    print(-1)
else:
    print(ret)