#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int main(){
int n;//測資數
while(cin>>n){
int x,y,ax,ay,bx,by;
cin>>x>>y>>ax>>ay>>bx>>by; // 起點和終點座標都要-1
bool map[x+100][y+100];
int step[x+100][y+100];//紀錄答案
memset(step,-1,sizeof(step));
for(int i=0;i<x;i++){
string s;
cin>>s;
for(int j=0;j<s.length();j++){
map[i][j]=s[j]-'0';
}
}
queue<pair<int,int> >q;
pair<int,int>start = make_pair(ax-1,ay-1); //這邊already-1
q.push(start);
step[ax-1][ay-1] = 1;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool flag=0;
while(!q.empty()){
pair<int,int> now = q.front();
q.pop();
int row = now.first;
int col = now.second;
if(row==bx-1&& col==by-1){flag=1; cout<<step[bx-1][by-1]<<endl; break;}// 判斷是否為終點
for(int i=0;i<4;i++){
int new_row=row+dx[i],new_col=col+dy[i];
if(new_row>=x || new_row<0 || new_col>=y || new_col<0) continue; //超出邊界
if(map[new_row][new_col] || step[new_row][new_col]!=-1) continue;//如果是障礙物
step[new_row][new_col] = step[row][col]+1;
pair<int,int>next =make_pair(new_row,new_col);
q.push(next);
}
}
if(!flag) cout<<0<<endl;//如果沒辦法到達要輸出 0
}
}