#include <bits/stdc++.h>
using namespace std;
void _print(bool t) { cerr << t; }
void _print(long long t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(double t) { cerr << t; }
template <class T>
void _print(vector<T> v) {
  cerr << "[  ";
  for (T i : v) {
    _print(i);
    cerr << "  ";
  }
  cerr << "]\n";
}
template <class T, class V>
void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.first);
  cerr << ", ";
  _print(p.second);
  cerr << "}";
}
template <class T>
void _print(set<T> v) {
  cerr << "[  ";
  for (T i : v) {
    _print(i);
    cerr << "  ";
  }
  cerr << "]\n";
}
template <class T>
void _print(multiset<T> v) {
  cerr << "[  ";
  for (T i : v) {
    _print(i);
    cerr << "  ";
  }
  cerr << "]\n";
}
template <class T, class V>
void _print(map<T, V> v) {
  cerr << "[  ";
  for (auto i : v) {
    _print(i);
    cerr << "  ";
  }
  cerr << "]\n";
}
template <class T>
void _print(T v[], long long n) {
  cerr << "[  ";
  for (long long i = 0; i < n; i++) {
    _print(v[i]);
    cerr << "  ";
  }
  cerr << "]\n";
}
struct DSU {
  vector<long long> parent;
  vector<long long> rank;
  void init(long long n) {
    parent.resize(n);
    rank.resize(n);
    for (long long i = 0; i < n; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  long long find(long long i) {
    if (parent[i] == i) return i;
    return parent[i] = find(parent[i]);
  }
  void merge(long long i, long long j) {
    long long p1 = find(i);
    long long p2 = find(j);
    if (p1 == p2) return;
    if (rank[p1] < rank[p2])
      parent[p1] = p2;
    else {
      parent[p2] = p1;
      if (rank[p1] == rank[p2]) rank[p1]++;
    }
  }
};
long long dist(pair<long long, long long> x, pair<long long, long long> y) {
  return (x.first - y.first) * (x.first - y.first) +
         (x.second - y.second) * (x.second - y.second);
}
void run() {
  long long n;
  cin >> n;
  long long sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  sx--, sy--, dx--, dy--;
  string a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  DSU ds;
  ds.init(n * n);
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (a[i][j] == '0') {
        if (i > 0 && a[i - 1][j] == '0') ds.merge(i * n + j, (i - 1) * n + j);
        if (j > 0 && a[i][j - 1] == '0') ds.merge(i * n + j, i * n + j - 1);
      }
    }
  }
  vector<pair<long long, long long>> x, y;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (ds.find(i * n + j) == ds.find(sx * n + sy)) x.push_back({i, j});
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (ds.find(i * n + j) == ds.find(dx * n + dy)) y.push_back({i, j});
    }
  }
  long long ans = INT_MAX;
  for (long long i = 0; i < x.size(); i++) {
    for (long long j = 0; j < y.size(); j++) {
      ans = min(ans, dist(x[i], y[j]));
    }
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie();
  long long t;
  t = 1;
  while (t--) {
    run();
  }
}
