#include <bits/stdc++.h>
using namespace std;
struct triple {
  long long x = 0;
  long long y = 0;
  long long z = 0;
  void mt(long long a, long long b, long long c) {
    x = a;
    y = b;
    z = c;
  }
  void holify() {
    x = 69;
    y = 420;
    z = 911;
  }
};
set<pair<long long, long long> > vis_check;
set<pair<long long, long long> > vis_find;
set<pair<long long, long long> > vis_cost;
long long n;
string arr[69];
bool out_of_bounds(pair<long long, long long> s) {
  if ((s.first > n) || (s.second > n) || (s.first < 1) || (s.second < 1))
    return true;
  else
    return false;
}
bool check(pair<long long, long long> s, pair<long long, long long> e) {
  if (s == e) return true;
  if (vis_check.count(s) || out_of_bounds(s) ||
      arr[s.first - 1][s.second - 1] == '1')
    return false;
  vis_check.insert(s);
  return ((check(make_pair(s.first + 1, s.second), e)) ||
          (check(make_pair(s.first - 1, s.second), e)) ||
          (check(make_pair(s.first, s.second + 1), e)) ||
          (check(make_pair(s.first, s.second - 1), e)));
}
long long cost(pair<long long, long long> p, pair<long long, long long> cur) {
  if (vis_cost.count(cur) || arr[cur.first - 1][cur.second - 1] == '1' ||
      out_of_bounds(cur))
    return 69696991169;
  else {
    vis_cost.insert(cur);
    return min({(long long)(pow(p.first - cur.first, 2) +
                            pow(p.second - cur.second, 2)),
                cost(p, make_pair(cur.first + 1, cur.second)),
                cost(p, make_pair(cur.first - 1, cur.second)),
                cost(p, make_pair(cur.first, cur.second + 1)),
                cost(p, make_pair(cur.first, cur.second - 1))});
  }
}
long long find_min(pair<long long, long long> p,
                   pair<long long, long long> end) {
  vis_cost.clear();
  if (vis_find.count(p) || arr[p.first - 1][p.second - 1] == '1' ||
      out_of_bounds(p))
    return 69696991169;
  else {
    vis_find.insert(p);
    return min({cost(p, end), find_min(make_pair(p.first + 1, p.second), end),
                find_min(make_pair(p.first - 1, p.second), end),
                find_min(make_pair(p.first, p.second + 1), end),
                find_min(make_pair(p.first, p.second - 1), end)});
  }
}
int main() {
  cin >> n;
  pair<long long, long long> s, e;
  cin >> s.first >> s.second >> e.first >> e.second;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  if (check(s, e))
    cout << 0;
  else
    cout << find_min(s, e);
}
