import sys from collections import deque def main(): R, C = map(int, sys.stdin.readline().split()) sy, sx = map(int, sys.stdin.readline().split()) gy, gx = map(int, sys.stdin.readline().split()) # Convert to 0-based indices sy -= 1 sx -= 1 gy -= 1 gx -= 1 # Read the maze maze = [] for _ in range(R): line = sys.stdin.readline().strip() maze.append(list(line)) # Initialize distance matrix with -1 (unvisited) dist = [[-1 for _ in range(C)] for _ in range(R)] dist[sy][sx] = 0 # Directions: up, down, left, right dy = [-1, 1, 0, 0] dx = [0, 0, -1, 1] q = deque() q.append((sy, sx)) while q: y, x = q.popleft() current_dist = dist[y][x] for i in range(4): ny = y + dy[i] nx = x + dx[i] if 0 <= ny < R and 0 <= nx < C: if maze[ny][nx] == '.' and dist[ny][nx] == -1: dist[ny][nx] = current_dist + 1 q.append((ny, nx)) if ny == gy and nx == gx: print(dist[ny][nx]) return # According to the problem statement, the path always exists. print(dist[gy][gx]) if __name__ == "__main__": main()