#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
int n,m,a[51][51],b[51][51][27],wx,wy,s=0;
struct sb
{
int x;
int y;
int s;
char fx;
};
void bfs(int x,int y,char fx)
{
b[x][y][fx-'A'+1]=1;
queue<sb>q;
q.push((sb){x,y,0,fx});
while(!q.empty())
{
int u=q.front().x,v=q.front().y,ns=q.front().s;
char fs=q.front().fx;
q.pop();
if(u==wx&&v==wy)
{
cout<<ns;
s=1;
break;
}
if(fs=='S')
{
q.push((sb){u,v,ns+1,'E'});
q.push((sb){u,v,ns+1,'W'});
if(u+2<=n&&b[u+1][v][fs-'A'+1]==0&&a[u+2][v]==0&&a[u+2][v+1]==0)
{
q.push((sb){u+1,v,ns+1,'S'});
b[u+1][v][fs-'A'+1]=1;
if(u+3<=n&&b[u+2][v][fs-'A'+1]==0&&a[u+3][v]==0&&a[u+3][v+1]==0)
{
q.push((sb){u+2,v,ns+1,'S'});
b[u+2][v][fs-'A'+1]=1;
if(u+4<=n&&b[u+3][v][fs-'A'+1]==0&&a[u+4][v]==0&&a[u+4][v+1]==0)
{
q.push((sb){u+3,v,ns+1,'S'});
b[u+3][v][fs-'A'+1]=1;
}
}
}
}
if(fs=='E')
{
q.push((sb){u,v,ns+1,'S'});
q.push((sb){u,v,ns+1,'N'});
if(v+2<=m&&b[u][v+1][fs-'A'+1]==0&&a[u][v+2]==0&&a[u+1][v+2]==0)
{
q.push((sb){u,v+1,ns+1,'E'});
b[u][v+1][fs-'A'+1]=1;
if(v+3<=m&&b[u][v+2][fs-'A'+1]==0&&a[u][v+3]==0&&a[u+1][v+3]==0)
{
q.push((sb){u,v+2,ns+1,'E'});
b[u][v+2][fs-'A'+1]=1;
if(v+4<=m&&b[u][v+3][fs-'A'+1]==0&&a[u][v+4]==0&&a[u+1][v+4]==0)
{
q.push((sb){u,v+3,ns+1,'E'});
b[u][v+3][fs-'A'+1]=1;
}
}
}
}
if(fs=='W')
{
q.push((sb){u,v,ns+1,'S'});
q.push((sb){u,v,ns+1,'N'});
if(v-1>=1&&b[u][v-1][fs-'A'+1]==0&&a[u][v-1]==0&&a[u+1][v-1]==0)
{
q.push((sb){u,v-1,ns+1,'W'});
b[u][v-1][fs-'A'+1]=1;
if(v-2>=1&&b[u][v-2][fs-'A'+1]==0&&a[u][v-2]==0&&a[u+1][v-2]==0)
{
q.push((sb){u,v-2,ns+1,'W'});
b[u][v-2][fs-'A'+1]=1;
if(v-3>=1&&b[u][v-3][fs-'A'+1]==0&&a[u][v-3]==0&&a[u+1][v-3]==0)
{
q.push((sb){u,v-3,ns+1,'W'});
b[u][v-3][fs-'A'+1]=1;
}
}
}
}
if(fs=='N')
{
q.push((sb){u,v,ns+1,'E'});
q.push((sb){u,v,ns+1,'W'});
if(u-1>=1&&b[u-1][v][fs-'A'+1]==0&&a[u-1][v]==0&&a[u-1][v+1]==0)
{
q.push((sb){u-1,v,ns+1,'N'});
b[u-1][v][fs-'A'+1]=1;
if(u-2>=1&&b[u-2][v][fs-'A'+1]==0&&a[u-2][v]==0&&a[u-2][v+1]==0)
{
q.push((sb){u-2,v,ns+1,'N'});
b[u-2][v][fs-'A'+1]=1;
if(u-3>=1&&b[u-3][v][fs-'A'+1]==0&&a[u-3][v]==0&&a[u-3][v+1]==0)
{
q.push((sb){u-3,v,ns+1,'N'});
b[u-3][v][fs-'A'+1]=1;
}
}
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
int x,y;
char fx;
cin>>x>>y>>wx>>wy>>fx;
bfs(x,y,fx);
if(s==0)
cout<<-1;
return 0;
}