from collections import deque def main(): import sys input = sys.stdin.read data = input().split() idx = 0 R = int(data[idx]) idx += 1 C = int(data[idx]) idx += 1 sy = int(data[idx]) - 1 idx += 1 sx = int(data[idx]) - 1 idx += 1 gy = int(data[idx]) - 1 idx += 1 gx = int(data[idx]) - 1 idx += 1 grid = [] for i in range(R): grid.append(data[idx]) idx += 1 if sy == gy and sx == gx: print(0) return directions = [ (-1, 0), (1, 0), (0, -1), (0, 1) ] dist = [ [ -1 for _ in range(C) ] for _ in range(R) ] q = deque() q.append( (sy, sx) ) dist[sy][sx] = 0 while q: y, x = q.popleft() for dy, dx in directions: ny = y + dy nx = x + dx if 0 <= ny < R and 0 <= nx < C: if grid[ny][nx] == '.' and dist[ny][nx] == -1: dist[ny][nx] = dist[y][x] + 1 if ny == gy and nx == gx: print(dist[ny][nx]) return q.append( (ny, nx) ) # According to problem statement, it's guaranteed to reach, so no need for this print(-1) if __name__ == "__main__": main()