#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int
N=505,
M=300005;
int n,m,Ecnt;
int dis[N*N],Q[M];
bool vis[N*N];
char s[N][N];
struct Edge{
int next,to,val;
}E[N*N<<1]; int head[N*N];
int vertex(int i,int j){
return (i-1)*m+j;
}
void add(int u,int v,int w){
E[++Ecnt].next=head[u];
E[Ecnt].to=v;
E[Ecnt].val=w;
head[u]=Ecnt;
E[++Ecnt].next=head[v];
E[Ecnt].to=u;
E[Ecnt].val=w;
head[v]=Ecnt;
}
void build(){
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
if (i-1) add(vertex(i,j),vertex(i-1,j),s[i][j]!=s[i-1][j]);
if (j-1) add(vertex(i,j),vertex(i,j-1),s[i][j]!=s[i][j-1]);
}
}
int SPFA(int sta,int end){
memset(vis,0,sizeof(vis));
memset(dis,60,sizeof(dis));
int h=0,tail=1;
Q[0]=sta; dis[sta]=0;
while (h!=tail){
int u=Q[h];
h=(h+1)%M;
vis[u]=0;
for (int i=head[u];i;i=E[i].next)
if (dis[u]+E[i].val<dis[E[i].to]){
dis[E[i].to]=dis[u]+E[i].val;
if (!vis[E[i].to]){
vis[E[i].to]=1;
Q[tail]=E[i].to;
tail=(tail+1)%M;
}
}
}
return dis[end];
}
int main(){
int x0,y0,x1,y1;
while (1){
n=read(),m=read();
if (!n) break;
Ecnt=0;
memset(head,0,sizeof(head));
for (int i=1;i<=n;i++)
scanf("%s",s[i]+1);
build();
x0=read(),y0=read(),x1=read(),y1=read();
x0++,y0++,x1++,y1++;
printf("%d\n",SPFA(vertex(x0,y0),vertex(x1,y1)));
}
return 0;
}