using System;
using System.Collections.Generic;
using System.Linq;
namespace AIZU
{
struct V {
public int y;
public int x;
public int l;
public V(int a, int b, int c) { y = a;x = b; l = c; }
public static V operator +(V a, V b) {
return new V(a.y + b.y, a.x + b.x, Math.Max(a.l,b.l));
}
}
class Program
{
#region Reserve
static int cin() { return cin(-1); }
static int cin(int D)
{
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
return D;
return int.Parse(s);
}
static int[] cins(char spliter)
{
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
return new int[] { };
string[] ss = s.Split(spliter);
int[] Res = new int[ss.Length];
int g;
for (int i = 0; i < ss.Length; i++) {
if (int.TryParse(ss[i], out g))
Res[i] = g;
}
return Res;
}
static int[] cins() { return cins(' '); }
static long[] lins(char spliter)
{
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
return new long[] { };
string[] ss = s.Split(spliter);
long[] Res = new long[ss.Length];
long g;
for (int i = 0; i < ss.Length; i++) {
if (long.TryParse(ss[i], out g))
Res[i] = g;
}
return Res;
}
static long[] lins() { return lins(' '); }
static void print(object j) { Console.WriteLine(j.ToString()); }
static void print(string j) { Console.WriteLine(j); }
#endregion
static bool[,] Pass;
static bool ONG(V v)
{
return Pass[v.y, v.x];
}
static void Main(string[] args)
{
int[] I = cins();
int R = I[0], C = I[1];
Pass = new bool[R, C]; ;
I = cins();
int sy = I[0] - 1, sx = I[1] - 1;
I = cins();
int gy = I[0] - 1, gx = I[1] - 1;
string Road;
for (int iop = 0; iop < R; iop++) {
Road = Console.ReadLine();
for (int jop = 0; jop < C; jop++) {
Pass[iop, jop] = Road[jop] == '.';
}
}
HashSet<V> History = new HashSet<V>();
Queue<V> Frontier = new Queue<V>();
int[][] Adja = { new int[]{ 0, 1 }, new int[] { 1, 0 }, new int[] { -1, 0 }, new int[] { 0, -1 } };
Frontier.Enqueue(new V(sy, sx, 0));
int Res=0;
while (Frontier.Count != 0) {
V v = Frontier.Dequeue();
foreach (int[] J in Adja) {
V v1 = v + new V(J[0], J[1], v.l+1);
if (v1.y == gy && v1.x == gx) { Res = v1.l;goto JK; }
if (ONG(v1)&&!History.Contains(v1)) {
Frontier.Enqueue(v1);
History.Add(v1);
}
}
}
JK:
print(Res);
Console.ReadLine();
}
}
}