#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m) {
  return b ? (b & 1 ? (a * power((a * a) % m, b >> 1, m)) % m
                    : power((a * a) % m, b >> 1, m))
           : 1;
}
int main() {
  int n;
  cin >> n;
  pair<int, int> pos1, pos2;
  cin >> pos1.first >> pos1.second;
  cin >> pos2.first >> pos2.second;
  pos1.first--;
  pos1.second--;
  pos2.first--;
  pos2.second--;
  vector<vector<int> > a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  int cmp = 1;
  vector<vector<int> > c(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 0 && c[i][j] == 0) {
        queue<pair<int, int> > q;
        pair<int, int> t(i, j);
        q.push(t);
        while (!q.empty()) {
          pair<int, int> p = q.front();
          q.pop();
          c[p.first][p.second] = cmp;
          if (p.first + 1 < n && a[p.first + 1][p.second] == 0 &&
              c[p.first + 1][p.second] == 0) {
            pair<int, int> tmp(p.first + 1, p.second);
            q.push(tmp);
            c[tmp.first][tmp.second] = cmp;
          }
          if (p.second + 1 < n && a[p.first][p.second + 1] == 0 &&
              c[p.first][p.second + 1] == 0) {
            pair<int, int> tmp(p.first, p.second + 1);
            q.push(tmp);
            c[tmp.first][tmp.second] = cmp;
          }
          if (p.first - 1 >= 0 && a[p.first - 1][p.second] == 0 &&
              c[p.first - 1][p.second] == 0) {
            pair<int, int> tmp(p.first - 1, p.second);
            q.push(tmp);
            c[tmp.first][tmp.second] = cmp;
          }
          if (p.second - 1 >= 0 && a[p.first][p.second - 1] == 0 &&
              c[p.first][p.second - 1] == 0) {
            pair<int, int> tmp(p.first, p.second - 1);
            q.push(tmp);
            c[tmp.first][tmp.second] = cmp;
          }
        }
        cmp++;
      }
    }
  }
  int m = INT_MAX;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        for (int l = 0; l < n; l++) {
          if (c[i][j] == c[pos1.first][pos1.second] &&
              c[k][l] == c[pos2.first][pos2.second]) {
            m = min(m, (i - k) * (i - k) + (j - l) * (j - l));
          }
        }
      }
    }
  }
  cout << m;
}
