#include <bits/stdc++.h>
using namespace std;
const int N = 50;
struct NODE
{
int x, y, step;
} que[N * N];
int front, rear, r, c, x_1, x_2, y_1, y_2, vis[N][N];
int g[N][N];
int dx[5] = { 0, -1, 1, 0, 0 };
int dy[5] = { 0, 0, 0, 1, -1 };
void BFS( int x, int y )
{
vis[x][y] = 1;
NODE temp = { x, y, 0 };
que[rear++] = temp;
while ( rear > front )
{
if ( que[front].x == x_2 && que[front].y == y_2 )
{
cout << que[front].step;
return ;
}
for ( int i = 1; i <= 4; i++ )
{
int tx = que[front].x + dx[i];
int ty = que[front].y + dy[i];
if ( tx < 1 || tx > r || ty < 1 || ty > c )
{
continue;
}
if ( g[tx][ty] == '.' && vis[tx][ty] == 0 )
{
NODE temp = { tx, ty, que[front].step + 1 };
que[rear++] = temp;
vis[tx][ty] = 1;
}
}
front++;
}
}
int main()
{
cin >> r >> c;
for ( int i = 1; i <= r; i++ )
{
for ( int j = 1; j <= c; j++ )
{
cin >> g[i][j];
}
}
cin >> x_1 >> y_1 >> x_2 >> y_2;
BFS( x_1, y_1 );
return 0;
}