#include <bits/stdc++.h>
using namespace std;
const long long M = 1e5 + 7, MOD = 1e9;
bool f_2[52][52] = {false}, f[52][52] = {false};
string s, p, q, ok, ch;
map<long long, long long> mp, mp_2;
set<long long> st, st_1;
long long arr[55][55];
long long cnt = 0, cost = 0;
deque<long long> Q;
vector<pair<long long, long long> > v, v_1;
struct node {
  long long pa, ran, visit;
};
long long tourist_2() {
  long long mx = 999999999;
  for (long long j = 0; j < v_1.size(); j++) {
    for (long long i = 0; i < v.size(); i++) {
      long long e = v[i].second;
      long long d = v[i].first;
      cost = (d - v_1[j].first) * (d - v_1[j].first);
      cost += ((e - v_1[j].second) * (e - v_1[j].second));
      mx = min(mx, cost);
    }
  }
  return mx;
}
void tourist(long long t) {
  for (long long i = 0; i < v_1.size(); i++) {
    long long d = v_1[i].first;
    long long e = v_1[i].second;
    if (d + 1 <= t && !f_2[d + 1][e] && arr[d + 1][e] == 0) {
      v_1.push_back(make_pair(d + 1, e));
      f_2[d + 1][e] = true;
    }
    if (d - 1 >= 1 && !f_2[d - 1][e] && arr[d - 1][e] == 0) {
      v_1.push_back(make_pair(d - 1, e));
      f_2[d - 1][e] = true;
    }
    if (e + 1 <= t && !f_2[d][e + 1] && arr[d][e + 1] == 0) {
      v_1.push_back(make_pair(d, e + 1));
      f_2[d][e + 1] = true;
    }
    if (e - 1 >= 1 && !f_2[d][e - 1] && arr[d][e - 1] == 0) {
      v_1.push_back(make_pair(d, e - 1));
      f_2[d][e - 1] = true;
    }
  }
}
void check(long long t) {
  for (long long i = 0; i < v.size(); i++) {
    long long d = v[i].first;
    long long e = v[i].second;
    if (d + 1 <= t && !f[d + 1][e] && arr[d + 1][e] == 0) {
      v.push_back(make_pair(d + 1, e));
      f[d + 1][e] = true;
    }
    if (d - 1 >= 1 && !f[d - 1][e] && arr[d - 1][e] == 0) {
      v.push_back(make_pair(d - 1, e));
      f[d - 1][e] = true;
    }
    if (e + 1 <= t && !f[d][e + 1] && arr[d][e + 1] == 0) {
      v.push_back(make_pair(d, e + 1));
      f[d][e + 1] = true;
    }
    if (e - 1 >= 1 && !f[d][e - 1] && arr[d][e - 1] == 0) {
      v.push_back(make_pair(d, e - 1));
      f[d][e - 1] = true;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long a, b, c = 0, n, t, sum = 0, mx = 0, l, x, y, i = 0, j = 0,
                  result = 0, e, sum_2, ans, k, ceck, r_1, r_2, c_1, c_2;
  cin >> t >> r_1 >> c_1 >> r_2 >> c_2;
  for (i = 1; i <= t; i++) {
    cin >> s;
    for (j = 1; j <= t; j++) {
      if (s[j - 1] == '0')
        arr[i][j] = 0;
      else
        arr[i][j] = 1;
    }
  }
  f[r_2][c_2] = true;
  f_2[r_1][c_1] = true;
  v.push_back(make_pair(r_2, c_2));
  check(t);
  if (f[r_1][c_1]) return cout << "0" << endl, 0;
  v_1.push_back(make_pair(r_1, c_1));
  tourist(t);
  ans = tourist_2();
  cout << ans << endl;
  return 0;
}
