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 = [sys.stdin.readline().strip() for _ in range(R)] # Initialize distance matrix with -1 (unvisited) distance = [[-1 for _ in range(C)] for _ in range(R)] distance[sy][sx] = 0 # Starting point # 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() # Check if current position is the goal if y == gy and x == gx: print(distance[y][x]) return # Explore all four directions for i in range(4): ny = y + dy[i] nx = x + dx[i] # Check if the next position is within bounds, is a path, and unvisited if 0 <= ny < R and 0 <= nx < C: if maze[ny][nx] == '.' and distance[ny][nx] == -1: distance[ny][nx] = distance[y][x] + 1 q.append((ny, nx)) # If somehow the loop exits without returning (though problem states a path exists) print(-1) if __name__ == "__main__": main()