#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
istream &operator>>(istream &inp, pair<int, int> &k) {
  inp >> k.first >> k.second;
  return inp;
}
int sqr(int x) { return x * x; }
int dist(pair<int, int> s, pair<int, int> t) {
  return sqr(s.first - t.first) + sqr(s.second - t.second);
}
int main() {
  int n;
  cin >> n;
  vector<vector<int>> b(n + 2, vector<int>(n + 2, -1));
  pair<int, int> s, t;
  cin >> s >> t;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      char x;
      cin >> x;
      b[i][j] = (int)(x - '0');
    }
  }
  int cnt = 0;
  vector<vector<int>> color(n + 2, vector<int>(n + 2, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (b[i][j] == 0) {
        queue<pair<int, int>> q;
        q.push({i, j});
        ++cnt;
        color[i][j] = cnt;
        while (!q.empty()) {
          pair<int, int> u = q.front();
          q.pop();
          for (int i = 0; i < 4; ++i) {
            pair<int, int> v = {u.first + dx[i], u.second + dy[i]};
            if (b[v.first][v.second] == 0 && color[v.first][v.second] == 0) {
              q.push(v);
              color[v.first][v.second] = cnt;
              b[v.first][v.second] = -1;
            }
          }
        }
      }
    }
  }
  if (color[s.first][s.second] == color[t.first][t.second]) {
    cout << 0 << '\n';
  } else {
    vector<pair<int, int>> ss, st;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (color[i][j] == color[s.first][s.second]) {
          ss.push_back({i, j});
        }
        if (color[i][j] == color[t.first][t.second]) {
          st.push_back({i, j});
        }
      }
    }
    int answer = (int)1e9;
    for (auto i : ss) {
      for (auto j : st) {
        answer = min(answer, dist(i, j));
      }
    }
    cout << answer << '\n';
  }
  return 0;
}
