#include <iostream>
#include <string>
#include <queue>
using namespace std;
int N, M, X1, Y1, X2, Y2;
string map[300];
queue<pair<int, int>> q;
bool vst[300][300];
int dr[] = { -1, 0, 1, 0 };
int dc[] = { 0, 1, 0, -1 };
bool dfs(int r, int c, int t) {
vst[r][c] = 1;
for (int d = 0; d < 4; ++d) {
int nr = r + dr[d], nc = c + dc[d];
if (0 <= nr && nr < N && 0 <= nc && nc < M && !vst[nr][nc]) {
switch(map[nr][nc]){
case '1':
q.push(make_pair(nr, nc));
break;
case '0':
if (dfs(nr, nc, t)) {
return 1;
}
break;
case '#':
return 1;
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> M >> X1 >> Y1 >> X2 >> Y2;
q.push(make_pair(X1 - 1, Y1 - 1));
for (int r = 0; r < N; ++r) {
cin >> map[r];
}
for (int t = 0; !q.empty(); ++t) {
int qs = q.size();
while (qs--) {
int r = q.front().first, c = q.front().second;
q.pop();
map[r][c] = '0';
if (!vst[r][c]) {
if (dfs(r, c, t)) {
cout << t + 1;
goto out;
}
}
}
}
out:
return 0;
}