import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int n, startx, starty, endx, endy;
static int map[][];
static int dist[][];
static int dx[] = {-1, 0, 1, 0};
static int dy[] = {0, 1, 0, -1};
public static int bfs(int x, int y) {
for(int i = 1; i < dist.length; i++)
Arrays.fill(dist[i], -1);
Queue<Node> queue = new LinkedList<>();
Node first = new Node(x, y);
queue.offer(first);
dist[x][y] = 0;
while(!queue.isEmpty()) {
first = queue.poll();
int a, b;
for(int i = 0; i < 4; i++) {
a = first.x + dx[i];
b = first.y + dy[i];
if(a < 1 || a > n || b < 1 || b > n)
continue;
if(map[a][b] != 0)
continue;
if(dist[a][b] > 0)
continue;
Node node = new Node(a, b);
queue.offer(node);
dist[a][b] = dist[first.x][first.y] + 1;
if(a == endx && b == endy)
return dist[endx][endy];
}
}
return dist[endx][endy];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
map = new int[n+1][n+1];
dist = new int[n+1][n+1];
String str[];
for(int i = 1; i < map.length; i++) {
str = sc.next().split("");
for(int j = 1; j < map[0].length; j++) {
map[i][j] = Integer.valueOf(str[j-1]);
}
}
startx = sc.nextInt();
starty = sc.nextInt();
endx = sc.nextInt();
endy = sc.nextInt();
sc.close();
System.out.println(bfs(startx, starty));
}
}
class Node {
int x;
int y;
public Node(int x, int y) {
super();
this.x = x;
this.y = y;
}
}