#include <bits/stdc++.h>
using namespace std;
struct coord {
  int r, c;
};
int m[52][52];
int d[52][52];
int n;
coord cc[2];
vector<coord> s[2];
void bfs(int k) {
  for (int i = 0; i < 52; i++)
    for (int j = 0; j < 52; j++) d[i][j] = -1;
  queue<coord> q;
  q.push(cc[k]);
  while (!q.empty()) {
    coord c = q.front();
    q.pop();
    if (d[c.r][c.c] == -1 && m[c.r][c.c] == 0) {
      d[c.r][c.c] = k;
      for (int i = -1; i <= 1; i += 2) {
        coord ct = {c.r + i, c.c};
        if (m[ct.r][ct.c] == 1) {
          s[k].push_back(c);
        } else {
          q.push(ct);
        }
        ct = {c.r, c.c + i};
        if (m[ct.r][ct.c] == 1) {
          s[k].push_back(c);
        } else {
          q.push(ct);
        }
      }
    }
  }
}
int main(void) {
  for (int i = 0; i < 52; i++)
    for (int j = 0; j < 52; j++) m[i][j] = -1;
  cin >> n >> cc[0].r >> cc[0].c >> cc[1].r >> cc[1].c;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) {
      m[i][j] = s[j - 1] - '0';
    }
  }
  bfs(0);
  bfs(1);
  if (d[cc[0].r][cc[0].c] == 1) {
    cout << 0 << endl;
    return 0;
  }
  int d = 1000000000;
  for (coord cs : s[0]) {
    for (coord ce : s[1]) {
      d = min(d, (cs.r - ce.r) * (cs.r - ce.r) + (cs.c - ce.c) * (cs.c - ce.c));
    }
  }
  cout << d << endl;
}
