var a:array[-2..54,-2..54]of 0..1;
p:array[-2..53,-2..53,1..4]of longint;
x,y,time:array[1..10000]of longint;
f:array[1..10000]of char;
n,m,i,j,o,k,t,w,xx,yy,zz:longint;
c:char;
s,ss:string;
function ty(c:char):longint;
begin
if c='E' then exit(1);
if c='S' then exit(2);
if c='W' then exit(3);
if c='N' then exit(4);
end;
function turn(c:char;x:longint):char;
begin
if x=1 then
begin
if c='E' then exit('N');
if c='S' then exit('E');
if c='W' then exit('S');
if c='N' then exit('W');
end;
if x=2 then
begin
if c='E' then exit('S');
if c='S' then exit('W');
if c='W' then exit('N');
if c='N' then exit('E');
end;
end;
function robot(x,y:longint):boolean;
begin
if(x+1>n)or(y+1>m) then exit(false);
if a[x,y]=1 then exit(false);
if a[x+1,y]=1 then exit(false);
if a[x,y+1]=1 then exit(false);
if a[x+1,y+1]=1 then exit(false);
exit(true);
end;
begin
readln(n,m);
for i:=1 to n do
begin
for j:=1 to n do read(a[i,j]);
readln;
end;
readln(s);
while pos(' ',s)<>0 do
begin
inc(o);
ss:=copy(s,1,pos(' ',s)-1);
if o=1 then val(ss,x[1]);
if o=2 then val(ss,y[1]);
if o=3 then val(ss,xx);
if o=4 then val(ss,yy);
delete(s,1,pos(' ',s));
end;
f[1]:=s[1];
p[x[1],y[1],ty(f[1])]:=1;
t:=0;
w:=1;
while t<w do
begin
inc(t);
if(x[t]=xx)and(y[t]=yy) then
begin
writeln(time[t]);
halt;
end;
i:=x[t];
j:=y[t];
c:=f[t];
if p[i,j,ty(turn(c,1))]=0 then
begin
inc(w);
x[w]:=i;
y[w]:=j;
f[w]:=turn(c,1);
time[w]:=time[t]+1;
p[i,j,ty(turn(c,1))]:=1;
end;
if p[i,j,ty(turn(c,2))]=0 then
begin
inc(w);
x[w]:=i;
y[w]:=j;
f[w]:=turn(c,2);
time[w]:=time[t]+1;
p[i,j,ty(turn(c,2))]:=1;
end;
if c='E' then
begin
if(p[i,j+1,1]=0)and(j+1<=m)and(robot(i,j+1)) then
begin
inc(w);
x[w]:=i;
y[w]:=j+1;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j+1,1]:=1;
end;
if(p[i,j+2,1]=0)and(j+2<=m)and(robot(i,j+1))and(robot(i,j+2)) then
begin
inc(w);
x[w]:=i;
y[w]:=j+2;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j+2,1]:=1;
end;
if(p[i,j+3,1]=0)and(j+3<=m)and(robot(i,j+1))and(robot(i,j+2))and(robot(i,j+3)) then
begin
inc(w);
x[w]:=i;
y[w]:=j+3;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j+3,1]:=1;
end;
end;
if c='W' then
begin
if(p[i,j-1,3]=0)and(j-1>=1)and(robot(i,j-1)) then
begin
inc(w);
x[w]:=i;
y[w]:=j-1;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j-1,3]:=1;
end;
if(p[i,j-2,3]=0)and(j-2>=1)and(robot(i,j-1))and(robot(i,j-2)) then
begin
inc(w);
x[w]:=i;
y[w]:=j-2;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j-2,3]:=1;
end;
if(p[i,j-3,3]=0)and(j-3>=1)and(robot(i,j-1))and(robot(i,j-2))and(robot(i,j-3)) then
begin
inc(w);
x[w]:=i;
y[w]:=j-3;
f[w]:=c;
time[w]:=time[t]+1;
p[i,j-3,3]:=1;
end;
end;
if c='S' then
begin
if(p[i+1,j,2]=0)and(i+1<=n)and(robot(i+1,j)) then
begin
inc(w);
x[w]:=i+1;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i+1,j,2]:=1;
end;
if(p[i+2,j,2]=0)and(i+2<=n)and(robot(i+1,j))and(robot(i+2,j)) then
begin
inc(w);
x[w]:=i+2;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i+2,j,2]:=1;
end;
if(p[i+3,j,2]=0)and(i+3<=n)and(robot(i+1,j))and(robot(i+2,j))and(robot(i+3,j)) then
begin
inc(w);
x[w]:=i+3;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i+3,j,2]:=1;
end;
end;
if c='N' then
begin
if(p[i-1,j,4]=0)and(i-1>=1)and(robot(i-1,j)) then
begin
inc(w);
x[w]:=i-1;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i-1,j,4]:=1;
end;
if(p[i-2,j,4]=0)and(i-2>=1)and(robot(i-1,j))and(robot(i-2,j)) then
begin
inc(w);
x[w]:=i-2;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i-2,j,4]:=1;
end;
if(p[i-3,j,4]=0)and(i-3>=1)and(robot(i-1,j))and(robot(i-2,j))and(robot(i-3,j)) then
begin
inc(w);
x[w]:=i-3;
y[w]:=j;
f[w]:=c;
time[w]:=time[t]+1;
p[i-3,j,4]:=1;
end;
end;
end;
writeln('-1');
end.