#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
const int INF = 2e9;
const int MX = 52;
const int MOD = 1000000007;
const double PI = acos(-1.0);
bool G[MX][MX];
struct pos {
  int r, c;
  pos(){};
  pos(int r, int c) : r(r), c(c){};
};
std::vector<pos> v1;
std::vector<pos> v2;
bool flag = false;
bool visited[MX][MX];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int n;
void dfs(int x, int y) {
  if (flag)
    v1.push_back(pos(x, y));
  else
    v2.push_back(pos(x, y));
  visited[x][y] = true;
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i];
    if (tx < 0 || tx == n) continue;
    int ty = y + dy[i];
    if (ty < 0 || ty == n) continue;
    if (!visited[tx][ty] && G[tx][ty]) dfs(tx, ty);
  }
  return;
}
int main() {
  n = ({
    int a;
    read(a);
    a;
  });
  int r1 = ({
             int a;
             read(a);
             a;
           }) -
           1,
      c1 = ({
             int a;
             read(a);
             a;
           }) -
           1;
  int r2 = ({
             int a;
             read(a);
             a;
           }) -
           1,
      c2 = ({
             int a;
             read(a);
             a;
           }) -
           1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      scanf("%c", &c);
      if (c == '0')
        G[i][j] = true;
      else
        G[i][j] = false;
    }
    getchar();
  }
  flag = true;
  memset(visited, false, sizeof(visited));
  dfs(r1, c1);
  flag = false;
  memset(visited, false, sizeof(visited));
  dfs(r2, c2);
  long long cost = INF;
  for (int i = 0; i < (int)v1.size(); i++) {
    for (int j = 0; j < (int)v2.size(); j++) {
      long long temp = (v1[i].r - v2[j].r) * (v1[i].r - v2[j].r) +
                       (v1[i].c - v2[j].c) * (v1[i].c - v2[j].c);
      cost = min(cost, temp);
    }
  }
  printf("%lld\n", cost);
  return 0;
}
