#include <bits/stdc++.h>
using namespace std;
long long n;
char mat[51][51];
int cost(int x1, int x2, int y1, int y2) {
  return (pow((x1 - x2), 2) + pow((y1 - y2), 2));
}
bool isvalid(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > n) return false;
  return true;
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void bfs(int srcrow, int srccol) {
  queue<pair<int, int>> q;
  q.push({srcrow, srccol});
  mat[srcrow][srccol] = 'A';
  while (!q.empty()) {
    int currx = q.front().first;
    int curry = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int newx = currx + dx[i];
      int newy = curry + dy[i];
      if (isvalid(newx, newy) && mat[newx][newy] == '0') {
        mat[newx][newy] = 'A';
        q.push({newx, newy});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  long long srcrow, srccol;
  cin >> srcrow >> srccol;
  long long destrow, destcol;
  cin >> destrow >> destcol;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) {
      cin >> mat[i][j];
    }
  bfs(srcrow, srccol);
  vector<pair<long long, long long>> vec1, vec2;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++)
      if (mat[i][j] == 'A') vec1.push_back(make_pair(i, j)), mat[i][j] = '0';
  }
  bfs(destrow, destcol);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++)
      if (mat[i][j] == 'A') vec2.push_back(make_pair(i, j)), mat[i][j] = '0';
  }
  long long ans = INT_MAX;
  for (auto it1 : vec1) {
    for (auto it2 : vec2) {
      long long c1 = cost(it1.first, it2.first, it1.second, it2.second);
      ans = min(ans, c1);
    }
  }
  cout << ans;
  return 0;
}
