#include <bits/stdc++.h>
using namespace std;
const int N = 55;
char v[N][N];
int viz[N][N], n;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool ok(int i, int j) {
  return (i >= 1 && i <= n && j >= 1 && j <= n && v[i][j] == '0');
}
void dfs(int i, int j, int k) {
  viz[i][j] = k;
  for (int z = 0; z < 4; z++) {
    int i2 = i + dx[z], j2 = j + dy[z];
    if (!viz[i2][j2] && ok(i2, j2)) dfs(i2, j2, k);
  }
}
int main() {
  int x1, y1, x2, y2;
  cin >> n >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> v[i][j];
  dfs(x1, y1, 1);
  if (viz[x2][y2]) {
    cout << 0;
    return 0;
  }
  dfs(x2, y2, 2);
  int ans = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (viz[i][j] == 1) {
        for (int a = 1; a <= n; a++)
          for (int b = 1; b <= n; b++)
            if (viz[a][b] == 2)
              ans = min(ans, (i - a) * (i - a) + (j - b) * (j - b));
      }
  cout << ans;
}
