Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue_move = [(0, Cw, Ch)]
queue_warp = []
smallests[Ch][Cw] = 0

def calc():
	while queue_move or queue_warp:
		if queue_move:
			queue = queue_move
		else:
			queue = queue_warp
		d, x, y = heappop(queue)
		if d == smallests[y][x]:
			for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
				tx = x + dx
				ty = y + dy
				if tx >= 0 and tx < W and ty >= 0 and ty < H:
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue_move, (d, tx, ty))
						heappush(queue_warp, (d, tx, ty))
			d += 1
			for tx in range(max(0, x - 2), min(W, x + 3)):
				for ty in range(max(0, y - 2), min(H, y + 3)):
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue_move, (d, tx, ty))
						heappush(queue_warp, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)


Fixed submission:
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue_move = [(0, Cw, Ch)]
queue_warp = [(0, Cw, Ch)]
smallests[Ch][Cw] = 0

def calc():
	while queue_move or queue_warp:
		if queue_move:
			d, x, y = heappop(queue_move)
			if d == smallests[y][x]:
				for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
					tx = x + dx
					ty = y + dy
					if tx >= 0 and tx < W and ty >= 0 and ty < H:
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
		else:
			d, x, y = heappop(queue_warp)
			if d == smallests[y][x]:
				d += 1
				for tx in range(max(0, x - 2), min(W, x + 3)):
					for ty in range(max(0, y - 2), min(H, y + 3)):
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue = [(0, Cw, Ch)]
smallests[Ch][Cw] = 0

def calc():
	while queue:
		d, x, y = heappop(queue)
		if d == smallests[y][x]:
			for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
				tx = x + dx
				ty = y + dy
				if tx >= 0 and tx < W and ty >= 0 and ty < H:
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue, (d, tx, ty))
			d += 1
			for tx in range(max(0, x - 2), min(W, x + 3)):
				for ty in range(max(0, y - 2), min(H, y + 3)):
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)


Fixed submission:
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue_move = [(0, Cw, Ch)]
queue_warp = [(0, Cw, Ch)]
smallests[Ch][Cw] = 0

def calc():
	while queue_move or queue_warp:
		if queue_move:
			d, x, y = heappop(queue_move)
			if d == smallests[y][x]:
				for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
					tx = x + dx
					ty = y + dy
					if tx >= 0 and tx < W and ty >= 0 and ty < H:
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
		else:
			d, x, y = heappop(queue_warp)
			if d == smallests[y][x]:
				d += 1
				for tx in range(max(0, x - 2), min(W, x + 3)):
					for ty in range(max(0, y - 2), min(H, y + 3)):
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue = [(0, Cw, Ch)]
smallests[Ch][Cw] = 0

def calc():
	while queue:
		d, x, y = heappop(queue)
		if d == smallests[y][x]:
			for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
				tx = x + dx
				ty = y + dy
				if tx >= 0 and tx < W and ty >= 0 and ty < H:
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue, (d, tx, ty))
			d += 1
			for tx in range(max(0, x - 2), min(W, x + 3)):
				for ty in range(max(0, y - 2), min(H, y + 3)):
					if grid[ty][tx] and d < smallests[ty][tx]:
						smallests[ty][tx] = d
						heappush(queue, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)


Fixed submission:
from heapq import heappush, heappop

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

grid = []
for _ in range(H):
	grid.append(list(c == "." for c in input().strip()))

m = H * W + 1
smallests = [[m] * W for _ in range(H)]

queue_move = [(0, Cw, Ch)]
queue_warp = [(0, Cw, Ch)]
smallests[Ch][Cw] = 0

def calc():
	while queue_move or queue_warp:
		if queue_move:
			d, x, y = heappop(queue_move)
			if d == smallests[y][x]:
				for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
					tx = x + dx
					ty = y + dy
					if tx >= 0 and tx < W and ty >= 0 and ty < H:
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
		else:
			d, x, y = heappop(queue_warp)
			if d == smallests[y][x]:
				d += 1
				for tx in range(max(0, x - 2), min(W, x + 3)):
					for ty in range(max(0, y - 2), min(H, y + 3)):
						if grid[ty][tx] and d < smallests[ty][tx]:
							smallests[ty][tx] = d
							heappush(queue_move, (d, tx, ty))
							heappush(queue_warp, (d, tx, ty))
	return smallests[Dh][Dw]

result = calc()
if result == m:
	print(-1)
else:
	print(result)