#include <bits/stdc++.h>
using namespace std;
const int N = 63;
bool M[N][N];
bool seen[N][N];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, ax, ay, bx, by;
  cin >> n >> ax >> ay >> bx >> by;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      M[i][j] = (c == '1');
    }
  vector<pair<int, int> > a, b;
  ax--;
  bx--;
  ay--;
  by--;
  n--;
  a.push_back({ax, ay});
  seen[ax][ay] = 1;
  for (int i = 0; i < a.size(); i++) {
    int x, y;
    tie(x, y) = a[i];
    if (x > 0 && !M[x - 1][y] && !seen[x - 1][y]) {
      seen[x - 1][y] = 1;
      a.push_back({x - 1, y});
    }
    if (x < n && !M[x + 1][y] && !seen[x + 1][y]) {
      seen[x + 1][y] = 1;
      a.push_back({x + 1, y});
    }
    if (y > 0 && !M[x][y - 1] && !seen[x][y - 1]) {
      seen[x][y - 1] = 1;
      a.push_back({x, y - 1});
    }
    if (y < n && !M[x][y + 1] && !seen[x][y + 1]) {
      seen[x][y + 1] = 1;
      a.push_back({x, y + 1});
    }
  }
  b.push_back({bx, by});
  for (int i = 0; i < b.size(); i++) {
    int x, y;
    tie(x, y) = b[i];
    if (x > 0 && !M[x - 1][y] && !seen[x - 1][y]) {
      seen[x - 1][y] = 1;
      b.push_back({x - 1, y});
    }
    if (x < n && !M[x + 1][y] && !seen[x + 1][y]) {
      seen[x + 1][y] = 1;
      b.push_back({x + 1, y});
    }
    if (y > 0 && !M[x][y - 1] && !seen[x][y - 1]) {
      seen[x][y - 1] = 1;
      b.push_back({x, y - 1});
    }
    if (y < n && !M[x][y + 1] && !seen[x][y + 1]) {
      seen[x][y + 1] = 1;
      b.push_back({x, y + 1});
    }
  }
  int best = 1e9;
  for (pair<int, int> x : a)
    for (pair<int, int> y : b) {
      int dx = x.first - y.first;
      int dy = x.second - y.second;
      dx = dx * dx + dy * dy;
      best = min(best, dx);
    }
  cout << best << endl;
}
