#include <bits/stdc++.h>
using namespace std;
int operator+(pair<int, int> a, pair<int, int> b) {
  int x = a.first + b.first;
  int y = a.second + b.second;
  return x * x + y * y;
}
pair<int, int> no(0, 0);
class graph {
  vector<vector<int> > graf;
  vector<vector<int> > value;
  pair<int, int> start;
  pair<int, int> finish;
  int size;

 public:
  graph(vector<vector<int> >& a, pair<int, int> x, pair<int, int> y)
      : graf(a),
        start(x),
        finish(y),
        size(graf.size()),
        value(graf.size(), vector<int>(graf.size(), 1024 * 2048)) {
    value[start.first][start.second] = 0;
    graf[finish.first][finish.second] = 2;
    dest(finish.first, finish.second);
    visit(start.first, start.second, make_pair(0, 0));
    cout << value[finish.first][finish.second] << "\n";
  }
  void dest(int x, int y) {
    if (x + 1 < size) {
      if (graf[x + 1][y] == 0) {
        graf[x + 1][y] = 2;
        dest(x + 1, y);
      }
    }
    if (x > 0) {
      if (graf[x - 1][y] == 0) {
        graf[x - 1][y] = 2;
        dest(x - 1, y);
      }
    }
    if (y + 1 < size) {
      if (graf[x][y + 1] == 0) {
        graf[x][y + 1] = 2;
        dest(x, y + 1);
      }
    }
    if (y > 0) {
      if (graf[x][y - 1] == 0) {
        graf[x][y - 1] = 2;
        dest(x, y - 1);
      }
    }
  }
  void visit(int x, int y, pair<int, int> cift) {
    if (graf[x][y] != 2) {
      if (graf[x][y] == 0 && cift == no) {
        if (x + 1 < size) {
          if (graf[x + 1][y] == 0) {
            if (value[x + 1][y] > value[x][y]) {
              value[x + 1][y] = value[x][y];
              visit(x + 1, y, cift);
            }
          } else if (graf[x + 1][y] == 1) {
            if (value[x + 1][y] > value[x][y] + 1) {
              value[x + 1][y] = value[x][y] + 1;
              visit(x + 1, y, make_pair(1, 0));
            }
          }
        }
        if (x > 0) {
          if (graf[x - 1][y] == 0) {
            if (value[x - 1][y] > value[x][y]) {
              value[x - 1][y] = value[x][y];
              visit(x - 1, y, cift);
            }
          } else if (graf[x - 1][y] == 1) {
            if (value[x - 1][y] > value[x][y] + 1) {
              value[x - 1][y] = value[x][y] + 1;
              visit(x - 1, y, make_pair(1, 0));
            }
          }
        }
        if (y + 1 < size) {
          if (graf[x][y + 1] == 0) {
            if (value[x][y + 1] > value[x][y]) {
              value[x][y + 1] = value[x][y];
              visit(x, y + 1, cift);
            }
          } else if (graf[x][y + 1] == 1) {
            if (value[x][y + 1] > value[x][y] + 1) {
              value[x][y + 1] = value[x][y] + 1;
              visit(x, y + 1, make_pair(0, 1));
            }
          }
        }
        if (y > 0) {
          if (graf[x][y - 1] == 0) {
            if (value[x][y - 1] > value[x][y]) {
              value[x][y - 1] = value[x][y];
              visit(x, y - 1, cift);
            }
          } else if (graf[x][y - 1] == 1) {
            if (value[x][y - 1] > value[x][y] + 1) {
              value[x][y - 1] = value[x][y] + 1;
              visit(x, y - 1, make_pair(0, 1));
            }
          }
        }
      } else {
        if (x + 1 < size) {
          if (value[x + 1][y] > (cift + pair<int, int>(1, 0))) {
            value[x + 1][y] = (cift + pair<int, int>(1, 0));
            visit(x + 1, y, make_pair(cift.first + 1, cift.second));
          }
        }
        if (x > 0) {
          if (value[x - 1][y] > (cift + pair<int, int>(1, 0))) {
            value[x - 1][y] = (cift + pair<int, int>(1, 0));
            visit(x - 1, y, make_pair(cift.first + 1, cift.second));
          }
        }
        if (y + 1 < size) {
          if (value[x][y + 1] > (cift + pair<int, int>(0, 1))) {
            value[x][y + 1] = (cift + pair<int, int>(0, 1));
            visit(x, y + 1, make_pair(cift.first, cift.second + 1));
          }
        }
        if (y > 0) {
          if (value[x][y - 1] > (cift + pair<int, int>(0, 1))) {
            value[x][y - 1] = (cift + pair<int, int>(0, 1));
            visit(x, y - 1, make_pair(cift.first, cift.second + 1));
          }
        }
      }
    } else {
      value[x][y] = (cift + no);
      if (value[finish.first][finish.second] > (cift + no))
        value[finish.first][finish.second] = (cift + no);
    }
  }
};
int main() {
  int n, x, y, z, a, c;
  cin >> n;
  vector<vector<int> > vec(n, vector<int>(n));
  cin >> x >> y >> z >> a;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%1d", &c);
      vec[i][j] = c;
    }
  }
  graph obje(vec, make_pair(x - 1, y - 1), make_pair(z - 1, a - 1));
}
