#include <bits/stdc++.h>
using namespace std;
struct Point {
  int x, y;
  Point(int p, int q) : x(p), y(q) {}
};
int n;
int arr[51][51];
vector<Point> fuck1, fuck2;
vector<int> xdisplace = {-1, 1, 0, 0};
vector<int> ydisplace = {0, 0, -1, 1};
long long dist(Point a, Point b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
void dfs(Point a, vector<Point> &fuck) {
  arr[a.x][a.y] = 1;
  fuck.push_back(a);
  for (int i = 0; i < 4; ++i) {
    Point temp = {a.x + xdisplace[i], a.y + ydisplace[i]};
    if (temp.x >= 1 && temp.x <= n && temp.y >= 1 && temp.y <= n &&
        arr[temp.x][temp.y] == 0)
      dfs(temp, fuck);
  }
}
long long solve() {
  long long ans = INT_MAX;
  for (auto x : fuck1) {
    for (auto y : fuck2) {
      ans = min(ans, dist(x, y));
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int r1, c1, r2, c2;
  cin >> n;
  cin >> r1 >> c1 >> r2 >> c2;
  Point x = {r1, c1}, y = {r2, c2};
  for (long long int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (long long int j = 1; j <= n; ++j) {
      arr[i][j] = s[j - 1] - '0';
    }
  }
  dfs(x, fuck1);
  dfs(y, fuck2);
  cout << solve() << endl;
  return 0;
}
