#include <bits/stdc++.h>
using namespace std;
const int mx = 1e9 + 7;
int n;
bool check(int i, int j) {
  return (i >= 1) && (i <= n) && (j >= 1) && (j <= n);
}
int main() {
  cin >> n;
  int r1, c1, r2, c2;
  cin >> r1 >> c1;
  cin >> r2 >> c2;
  char a[n + 1][n + 1];
  bool vis[n + 1][n + 1];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a[i][j];
      vis[i][j] = false;
    }
  }
  queue<pair<int, int>> q;
  q.push({r1, c1});
  vis[r1][c1] = true;
  set<pair<int, int>> s;
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, -1, 1};
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int cx = x + dx[i];
      int cy = y + dy[i];
      if (check(cx, cy)) {
        if ((!vis[cx][cy]) && (a[cx][cy] == '0')) {
          vis[cx][cy] = true;
          q.push({cx, cy});
          s.insert({cx, cy});
        }
      }
    }
  }
  if (vis[r2][c2]) {
    cout << 0 << endl;
  } else {
    queue<pair<int, int>> g;
    set<pair<int, int>> me;
    g.push({r2, c2});
    vis[r2][c2] = true;
    while (!g.empty()) {
      int x = g.front().first;
      int y = g.front().second;
      g.pop();
      for (int i = 0; i < 4; ++i) {
        int cx = x + dx[i];
        int cy = y + dy[i];
        if (check(cx, cy)) {
          if ((!vis[cx][cy]) && (a[cx][cy] == '0')) {
            vis[cx][cy] = true;
            g.push({cx, cy});
            me.insert({cx, cy});
          }
        }
      }
    }
    int ans = mx;
    auto sqr = [](int jp) { return (jp * jp); };
    me.insert({r2, c2});
    s.insert({r1, c1});
    for (auto h : s) {
      for (auto e : me) {
        ans = min(ans, sqr(h.first - e.first) + sqr(h.second - e.second));
      }
    }
    cout << ans << endl;
  }
}
