#include<bits/stdc++.h>
using namespace std;
int n, m;
int x_1, y_1, x_2, y_2;
char g[510][510];
int vi[510][510];
int move_x[5] = { 0,-1,0,1,0 };
int move_y[5] = { 0,0,1,0,-1 };
int flag = 1;
queue<pair<int, int>> q;
queue<pair<int, int>> q_0;
int bfs(int x, int y);
int bfs_0(int x, int y);
int bfs_0(int x, int y)
{
q_0.push({ x,y });
while (q_0.size() && flag)
{
pair<int, int> temp = q_0.front();
q_0.pop();
for (int i = 1; i <= 4; i++)
{
int a = temp.first + move_x[i]; int b = move_y[i] + temp.second;
if (a < 0 || a > n - 1 || b < 0 || b > m - 1) continue;
if (vi[a][b] != -1) continue;
if (g[a][b] != g[temp.first][temp.second])
{
vi[a][b] = vi[temp.first][temp.second] + 1;
q.push({ a,b });
}
else
{
vi[a][b] = vi[temp.first][temp.second];
q_0.push({ a,b });
}
if (a == x_2 && b == y_2)
{
cout << vi[a][b] << endl;
flag = 0;
return 0;
}
}
}
return 0;
}
int bfs(int x, int y)
{
memset(vi, -1, sizeof(vi));
vi[x][y] = 0;
q.push({ x,y });
while (q.size() && flag)
{
pair<int, int> temp = q.front();
q.pop();
for (int i = 1; i <= 4; i++)
{
int a = temp.first + move_x[i]; int b = move_y[i] + temp.second;
if (a < 0 || a > n - 1 || b < 0 || b > m - 1) continue;
if (vi[a][b] != -1) continue;
if (g[a][b] == g[temp.first][temp.second])
{
vi[a][b] = vi[temp.first][temp.second];
bfs_0(a, b);
continue;
}
vi[a][b] = vi[temp.first][temp.second] + 1;
if (a == x_2 && b == y_2)
{
cout << vi[a][b] << endl;
flag = 0;
return 0;
}
q.push({ a,b });
}
}
return 0;
}
int main()
{
while (1)
{
flag = 1;
cin >> n >> m;
if (n == m && n == 0)
break;
while (q.size())
q.pop();
while (q_0.size())
q_0.pop();
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
cin >> x_1 >> y_1 >> x_2 >> y_2;
if (x_1 == x_2 && y_1 == y_2)
{
cout << 0 << endl;
continue;
}
bfs(x_1, y_1);
}
return 0;
}