r, c = map(int, input().split())
gs = [[1] * c for i in range(r)]
k = input().split()
points = [int(i) for i in k]
# print(point)
 
for i in range(r):
    s = input()
    # print(s)
    for j in range(len(s)):
        if s[j] == ".":
            gs[i][j] = 0
 
 
def short(g, point):
    mem = {(0, 0): 0}
    q, step = [(point[0] - 1, point[1] - 1, 0)], 0
 
    while q:
        n = len(q)
        for _ in range(n):
            x, y, pre = q.pop(0)
            if pre > 0:
                continue
            if x == point[2] - 1 and y == point[3] - 1:
                return step
            for i, j in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
                nx, ny = x + i, y + j
                if 0 <= nx < r and 0 <= ny < c:
                    nPre = pre + 1 if g[nx][ny] == 1 else pre
                    if nPre < mem.get((nx, ny), float("inf")):
                        q.append((nx, ny, nPre))
                        mem[(nx, ny)] = nPre
        step += 1
    return -1
 
 
print(short(gs, points))