#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<char> > a;
char x = 1;
void dfss(int i, int j) {
  a[i][j] = x;
  if (i + 1 < n) {
    if (a[i + 1][j] != '1' && a[i + 1][j] != x) {
      dfss(i + 1, j);
    }
  }
  if (j + 1 < n) {
    if (a[i][j + 1] != '1' && a[i][j + 1] != x) {
      dfss(i, j + 1);
    }
  }
  if (i > 0) {
    if (a[i - 1][j] != '1' && a[i - 1][j] != x) {
      dfss(i - 1, j);
    }
  }
  if (j > 0) {
    if (a[i][j - 1] != '1' && a[i][j - 1] != x) {
      dfss(i, j - 1);
    }
  }
}
int main() {
  cin >> n;
  int rs, cs, rf, cf;
  cin >> rs >> cs >> rf >> cf;
  rs--;
  cs--;
  rf--;
  cf--;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    a[i].resize(n);
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
      if (a[i][j] == '0') a[i][j] = 0;
    }
  }
  dfss(rs, cs);
  x = 2;
  dfss(rf, cf);
  if (a[rs][cs] == 2) {
    cout << 0;
    return 0;
  }
  int ans = INT_MAX, temp;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[i][j] != 1) continue;
      for (int k = 0; k < n; ++k) {
        for (int l = 0; l < n; ++l) {
          if (a[k][l] == 2) {
            temp = (i - k) * (i - k) + (j - l) * (j - l);
            if (temp < ans) ans = temp;
          }
        }
      }
    }
  }
  cout << ans;
  return 0;
}
