#include <bits/stdc++.h>
using namespace std;
int vis[100][100], Map[100][100], ans = 0x3f3f3f3f;
char arr[100][100];
int n, r1, c1, r2, c2;
int dis[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
struct node {
  int x;
  int y;
  node(int x, int y) : x(x), y(y) {}
};
int check(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= n || vis[x][y] == 1 || arr[x][y] == '1')
    return 0;
  return 1;
}
int answer(int x1, int y1, int x2, int y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
void bfs(int x, int y, int k) {
  queue<node> Q;
  vis[x][y] = 1;
  Q.push(node(x, y));
  while (!Q.empty()) {
    node p = Q.front();
    Q.pop();
    Map[p.x][p.y] = k;
    for (int i = 0; i < 4; i++) {
      int xx = p.x + dis[i][0];
      int yy = p.y + dis[i][1];
      if (check(xx, yy)) {
        vis[xx][yy] = 1;
        Q.push(node(xx, yy));
      }
    }
  }
}
int main() {
  int sum = 0;
  cin >> n >> r1 >> c1 >> r2 >> c2;
  r1--, r2--, c2--, c1--;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (vis[i][j] == 0 && arr[i][j] == '0') {
        bfs(i, j, sum++);
      }
    }
  }
  if (Map[r1][c1] == Map[r2][c2]) {
    cout << "0" << endl;
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (arr[i][j] == '0' && Map[i][j] == Map[r1][c1]) {
          for (int w = 0; w < n; w++) {
            for (int s = 0; s < n; s++) {
              if (arr[w][s] == '0' && Map[w][s] == Map[r2][c2]) {
                ans = min(ans, answer(i, j, w, s));
              }
            }
          }
        }
      }
    }
    cout << ans;
  }
  return 0;
}
