#include<bits/stdc++.h>
//#include<windows.h>
using namespace std;
int n,m,go[105][105],sx,sy,ex,ey,tot,rob[105][105][10];
bool vis[105][105][10];
char ch;
struct data{
int x,y,w,ans;
};
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<48||ch>57){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>=48&&ch<=57){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
bool pd(int x,int y,int w,int l){
if(x<=0||y<=0)return false;
if(w==1){
if(vis[x][y+l][w]||y+l>=m)return false;
for(int i=1;i<=l;++i){
if(go[x][y+i]){
return false;
}
}
}
if(w==2){
if(vis[x+l][y][w]||x+l>=n)return false;
for(int i=1;i<=l;++i){
if(go[x+i][y]){
return false;
}
}
}
if(w==3){
if(vis[x][y-l][w]||y-l<=0||y<=0)return false;
for(int i=1;i<=l;++i){
if(go[x][y-i]){
return false;
}
}
}
if(w==4){
if(vis[x-l][y][w]||x-l<=0||x<=0)return false;
for(int i=1;i<=l;++i){
if(go[x-i][y]){
return false;
}
}
}
return true;
}
void bfs(int x1,int y1,int way){
memset(vis,false,sizeof(vis));
// memset(rob,0x3f,sizeof(rob));
queue<data>q;
q.push({x1,y1,way,0});
vis[x1][y1][way]=true;
while(!q.empty()){
int x=q.front().x;
int y=q.front().y;
int w=q.front().w;
int ans=q.front().ans;
q.pop();
// cout<<"x: "<<x<<" y: "<<y<<" w: "<<w<<" ans:"<<ans<<endl;
// Sleep(5);
if(x==ex&&y==ey){
cout<<ans;
exit(0);
}
for(int i=1;i<=4;++i){
for(int j=3;j>=1;--j){
if(i==1){
if(pd(x,y,i,j)){
int yy=y+j;
int cnt=ans;
if(w==3)break;
else if(w!=1)++cnt;
// else if(x==sx&&y==sy&&w==3)ans+=2;
q.push({x,yy,i,cnt+1});
vis[x][yy][i]=true;
// cout<<"x: "<<x<<" y: "<<y<<" w: "<<w<<" ans:"<<ans<<endl;
// cout<<"x: "<<x<<" y: "<<yy<<" i: "<<i<<" j: "<<j<<" ans:"<<cnt+1<<endl;
}
}
if(i==2){
if(pd(x,y,i,j)){
int xx=x+j;
int cnt=ans;
if(w==4)break;
else if(w!=2)++cnt;
// else if(x==sx&&y==sy&&w==4)ans+=2;
q.push({xx,y,i,cnt+1});
vis[xx][y][i]=true;
// cout<<"x: "<<x<<" y: "<<y<<" w: "<<w<<" ans:"<<ans<<endl;
// cout<<"x: "<<xx<<" y: "<<y<<" i: "<<i<<" j: "<<j<<" ans:"<<cnt+1<<endl;
}
}
if(i==3){
if(pd(x,y,i,j)){
int yy=y-j;
int cnt=ans;
if(w==1)break;
else if(w!=3)++cnt;
// else if(x==sx&&y==sy&&w==1)ans+=2;
q.push({x,yy,i,cnt+1});
vis[x][yy][i]=true;
// cout<<"x: "<<x<<" y: "<<y<<" w: "<<w<<" ans:"<<ans<<endl;
// cout<<"x: "<<x<<" y: "<<yy<<" i: "<<i<<" j: "<<j<<" ans:"<<cnt+1<<endl;
}
}
if(i==4){
if(pd(x,y,i,j)){
int xx=x-j;
int cnt=ans;
if(w==2)break;
else if(w!=4)++cnt;
// else if(x==sx&&y==sy&&w==2)ans+=2;
q.push({xx,y,i,cnt+1});
vis[xx][y][i]=true;
// cout<<"x: "<<x<<" y: "<<y<<" w: "<<w<<" ans:"<<ans<<endl;
// cout<<"x: "<<xx<<" y: "<<y<<" i: "<<i<<" j: "<<j<<" ans:"<<cnt+1<<endl;
}
}
}
}
}
}
signed main(){
n=read();m=read();
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
int a;
a=read();
if(a){
go[i][j]=1;
go[i+1][j]=1;
go[i][j+1]=1;
go[i+1][j+1]=1;
}
}
}
// for(int i=0;i<n;++i){
// for(int j=0;j<m;++j){
// if(go[i][j]==1)cout<<"i: "<<i<<" j: "<<j<<endl;
// }
// }
sx=read();sy=read();ex=read();ey=read();
cin>>ch;
int w;
if(ch=='E')w=1;
if(ch=='S')w=2;
if(ch=='W')w=3;
if(ch=='N')w=4;
bfs(sx,sy,w);
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