#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
class point
{
public:
int x;
int y;
int step;
};
//初始化地图
void create_map(int arr[][100], int temparr[][100], int x_num,int y_num)
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
arr[i][j] = 0;
temparr[i][j] = 0;
}
}
for (int i = 1; i <= x_num; i++)
{
for (int j = 1; j <= y_num; j++)
{
//1为路,2为墙
cin>>arr[i][j];
}
}
}
/*
测试代码
5 4
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
1 1
4 3
*/
int main() {
int arr[100][100],x_num,y_num,star_x,star_y,px,py;
int temparr[100][100];
int move_x[4] = { 0,0,-1,1 };
int move_y[4] = { 1,-1,0,0 };
queue<point> que;
point po;
cin >> x_num >> y_num;
create_map(arr,temparr, x_num, y_num);
cin >> star_x >> star_y;
cin >> px >> py;
po.x = star_x;
po.y = star_y;
temparr[star_x][star_y] = 2;
po.step = 0;
que.push(po);
int flag = 0;
int re = 0;
while (que.empty() == 0)
{
flag = 0;
int x = que.front().x, y = que.front().y;
for (int i = 0; i < 4; i++)
{
if (arr[x + move_x[i]][y + move_y[i]] == 1 && temparr[x + move_x[i]][y + move_y[i]] == 0)
{
point temppo;
temppo.x = x + move_x[i];
temppo.y = y + move_y[i];
temppo.step = que.front().step+1;
if (temppo.x == px && temppo.y == py)
{
re = temppo.step;
break;
}
que.push(temppo);
temparr[x + move_x[i]][y + move_y[i]] = 2;
flag = 1;
}
}
if (re != 0)
break;
que.pop();
}
cout << re << endl;
system("pause");
}