/**OOO**/
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n,m;
   cin>>n>>m;

    vector<string>strvec;
    string st,instruction;

    for(int i=0;i<n;i++)
    {
       cin>>st;
       strvec.push_back(st);
    }
    cin>>instruction;

//   cout<<endl;
//   for(int i=0;i<strvec.size();i++)
//   {
//       cout<<strvec[i]<<endl;
//   }

   int start_row;
   int start_column;
   int End_row;
   int End_column;

    int row;
    int column;
   for(int i=0;i<n;i++)
   {    st=strvec[i];
       for(int j=0;j<m;j++)
       {    char myword[]={st[j]};
            string temp=myword;
           if(temp=="S"){
            start_row=i;row=i;
            start_column=j;column=j;
           }
           else if(temp=="E"){
            End_row=i;
            End_column=j;
           }
       }
   }

   //cout<<start_row<<" "<<start_column<<endl;
   //cout<<End_row<<" "<<End_column<<endl;

   int cnt=0;
   int flag;

   for(int u=0;u<=3;u++)
   {    //start_row=row;
        //start_column=column;
       for(int d=0;d<=3;d++)
       {
           for(int l=0;l<=3;l++)
           {
               for(int r=0;r<=3;r++)
               {
                   if((u!=d && u!=l && u!=r) && (l!=u&&l!=d&&l!=r) &&(r!=u&&r!=d&&r!=l)&&(d!=l&&d!=u&&d!=r))
                   {    start_row=row;
                        start_column=column;
                       for(int i=0;i<instruction.length();i++)
                       {
                           stringstream ss;
                           ss<<u;
                           string upp=ss.str();/**for up**/

                            stringstream ss2;
                            ss2<<d;
                            string downn=ss2.str();/**for down**/

                            stringstream ss3;
                            ss3<<l;
                            string leftt=ss3.str();/**for left**/

                            stringstream ss4;
                            ss4<<r;
                            string rightt=ss4.str();/**for right**/

                            //char myword[]={instruction[i]};
                            //cout<<" myword = "<<myword[]<<endl;
                            string temp="";
                            temp=temp+instruction[i];
                            //cout<<"temp "<<temp<<" "<<"up ="<<up<<" "<<"down = "<<down<<" "<<"left = "<<left<<" "<<"right = "<<right<<endl;
                            if(temp==upp)start_row--;
                            else if(temp==downn)start_row++;
                            else if(temp==leftt)start_column--;
                            else if(temp==rightt)start_column++;

                            if(start_row>=0 &&start_row<n)st=strvec[start_row];
                            if(start_column>=0&& start_column<m){
                                    char tempword[]={st[start_column]};
                                    temp=tempword;
                            }

                            //cout<<"star rw "<<start_row<<endl;
                            //cout<<"start colmn "<<start_column<<endl;
                            string has="#";
                            if(start_row==End_row && start_column==End_column){cnt++;break;}
                            else if(start_row<0 || start_row==n)break;
                            else if(start_column<0 || start_column==m)break;
                            else if(temp==has)break;

                       }
                   }
               }
           }
       }
   }

   cout<<cnt<<endl;

return 0;
}


/**OOO**/
#include<bits/stdc++.h>
#include<string.h>
using namespace std;

int main()
{
   int n,m;
   cin>>n>>m;

    vector<string>strvec;
    string st,instruction;

    for(int i=0;i<n;i++)
    {
       cin>>st;
       strvec.push_back(st);
    }
    cin>>instruction;

//   cout<<endl;
//   for(int i=0;i<strvec.size();i++)
//   {
//       cout<<strvec[i]<<endl;
//   }

   int start_row;
   int start_column;
   int End_row;
   int End_column;

    int row;
    int column;
   for(int i=0;i<n;i++)
   {    st=strvec[i];
       for(int j=0;j<st.length();j++)
       {    //char myword[]={st[j]};
            //string temp=myword;
            string temp="";
            temp+=st[j];
           if(temp=="S"){
            //start_row=i;
            row=i;
            //start_column=j;
            column=j;
           }
           else if(temp=="E"){
            End_row=i;
            End_column=j;
           }
       }
   }

   //cout<<start_row<<" "<<start_column<<endl;
   //cout<<End_row<<" "<<End_column<<endl;

   int cnt=0;
   int flag;

   for(int u=0;u<=3;u++)
   {    //start_row=row;
        //start_column=column;
       for(int d=0;d<=3;d++)
       {
           for(int l=0;l<=3;l++)
           {
               for(int r=0;r<=3;r++)
               {
                   if((u!=d && u!=l && u!=r) && (l!=u&&l!=d&&l!=r) &&(r!=u&&r!=d&&r!=l)&&(d!=l&&d!=u&&d!=r))
                   {    start_row=row;
                        start_column=column;
                       for(int i=0;i<instruction.length();i++)
                       {
                           stringstream ss;
                           ss<<u;
                           string upp=ss.str();/**for up**/

                            stringstream ss2;
                            ss2<<d;
                            string downn=ss2.str();/**for down**/

                            stringstream ss3;
                            ss3<<l;
                            string leftt=ss3.str();/**for left**/

                            stringstream ss4;
                            ss4<<r;
                            string rightt=ss4.str();/**for right**/

                            //char myword[]={instruction[i]};
                            //cout<<" myword = "<<myword[]<<endl;
                            string temp="";
                            temp=temp+instruction[i];
                            //cout<<"temp "<<temp<<" "<<"up ="<<upp<<" "<<"down = "<<downn<<" "<<"left = "<<leftt<<" "<<"right = "<<rightt<<endl;
                            if(temp==upp)start_row--;
                            else if(temp==downn)start_row++;
                            else if(temp==leftt)start_column--;
                            else if(temp==rightt)start_column++;

                            if(start_row>=0 &&start_row<n)st=strvec[start_row];

                            if(start_column>=0&& start_column<m){
                                    //char tempword[]={st[start_column]};
                                    //temp=tempword;
                                    temp="";
                                    temp+=st[start_column];
                            }

                            //cout<<"star rw "<<start_row<<endl;
                            //cout<<"start colmn "<<start_column<<endl;
                            string has="#";
                            if(start_row==End_row && start_column==End_column){cnt++;break;}
                            else if(start_row<0 || start_row==n)break;
                            else if(start_column<0 || start_column==m)break;
                            else if(temp==has)break;

                       }
                   }
               }
           }
       }
   }

   cout<<cnt<<endl;

return 0;
}


/**OOO**/
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n,m;
   cin>>n>>m;

    vector<string>strvec;
    string st,instruction;

    for(int i=0;i<n;i++)
    {
       cin>>st;
       strvec.push_back(st);
    }
    cin>>instruction;

//   cout<<endl;
//   for(int i=0;i<strvec.size();i++)
//   {
//       cout<<strvec[i]<<endl;
//   }

   int start_row;
   int start_column;
   int End_row;
   int End_column;

    int row;
    int column;
   for(int i=0;i<n;i++)
   {    st=strvec[i];
       for(int j=0;j<m;j++)
       {    char myword[]={st[j]};
            string temp=myword;
           if(temp=="S"){
            start_row=i;row=i;
            start_column=j;column=j;
           }
           else if(temp=="E"){
            End_row=i;
            End_column=j;
           }
       }
   }

   //cout<<start_row<<" "<<start_column<<endl;
   //cout<<End_row<<" "<<End_column<<endl;

   int cnt=0;
   int flag;

   for(int u=0;u<=3;u++)
   {    //start_row=row;
        //start_column=column;
       for(int d=0;d<=3;d++)
       {
           for(int l=0;l<=3;l++)
           {
               for(int r=0;r<=3;r++)
               {
                   if((u!=d && u!=l && u!=r) && (l!=u&&l!=d&&l!=r) &&(r!=u&&r!=d&&r!=l)&&(d!=l&&d!=u&&d!=r))
                   {    start_row=row;
                        start_column=column;
                       for(int i=0;i<instruction.length();i++)
                       {
                           stringstream ss;
                           ss<<u;
                           string upp=ss.str();/**for up**/

                            stringstream ss2;
                            ss2<<d;
                            string downn=ss2.str();/**for down**/

                            stringstream ss3;
                            ss3<<l;
                            string leftt=ss3.str();/**for left**/

                            stringstream ss4;
                            ss4<<r;
                            string rightt=ss4.str();/**for right**/

                            //char myword[]={instruction[i]};
                            //cout<<" myword = "<<myword[]<<endl;
                            string temp="";
                            temp=temp+instruction[i];
                            //cout<<"temp "<<temp<<" "<<"up ="<<up<<" "<<"down = "<<down<<" "<<"left = "<<left<<" "<<"right = "<<right<<endl;
                            if(temp==upp)start_row--;
                            else if(temp==downn)start_row++;
                            else if(temp==leftt)start_column--;
                            else if(temp==rightt)start_column++;

                            st=strvec[start_row];
                            char tempword[]={st[start_column]};
                            temp=tempword;
                            //cout<<"star rw "<<start_row<<endl;
                            //cout<<"start colmn "<<start_column<<endl;
                            string has="#";
                            if(start_row==End_row && start_column==End_column){cnt++;break;}
                            else if(start_row<0 || start_row==n)break;
                            else if(start_column<0 || start_column==m)break;
                            else if(temp==has)break;

                       }
                   }
               }
           }
       }
   }

   cout<<cnt<<endl;

return 0;
}


/**OOO**/
#include<bits/stdc++.h>
#include<string.h>
#define ll long long
#define ull unsigned long long
#define mems(x,y) memset(x,y,sizeof(x))
#define pf printf
#define sc scanf
using namespace std;

int main()
{
   int n,m;
   cin>>n>>m;

    vector<string>strvec;
    string st,instruction;

    for(int i=0;i<n;i++)
    {
       cin>>st;
       strvec.push_back(st);
    }
    cin>>instruction;

//   cout<<endl;
//   for(int i=0;i<strvec.size();i++)
//   {
//       cout<<strvec[i]<<endl;
//   }

   int start_row;
   int start_column;
   int End_row;
   int End_column;

    int row;
    int column;
   for(int i=0;i<n;i++)
   {    st=strvec[i];
       for(int j=0;j<m;j++)
       {    char myword[]={st[j]};
            string temp=myword;
           if(temp=="S"){
            start_row=i;row=i;
            start_column=j;column=j;
           }
           else if(temp=="E"){
            End_row=i;
            End_column=j;
           }
       }
   }

   //cout<<start_row<<" "<<start_column<<endl;
   //cout<<End_row<<" "<<End_column<<endl;

   int cnt=0;
   int flag;

   for(int u=0;u<=3;u++)
   {    //start_row=row;
        //start_column=column;
       for(int d=0;d<=3;d++)
       {
           for(int l=0;l<=3;l++)
           {
               for(int r=0;r<=3;r++)
               {
                   if((u!=d && u!=l && u!=r) && (l!=u&&l!=d&&l!=r) &&(r!=u&&r!=d&&r!=l)&&(d!=l&&d!=u&&d!=r))
                   {    start_row=row;
                        start_column=column;
                       for(int i=0;i<instruction.length();i++)
                       {
                           stringstream ss;
                           ss<<u;
                           string up=ss.str();/**for up**/

                            stringstream ss2;
                            ss2<<d;
                            string down=ss2.str();/**for down**/

                            stringstream ss3;
                            ss3<<l;
                            string left=ss3.str();/**for left**/

                            stringstream ss4;
                            ss4<<r;
                            string right=ss4.str();/**for right**/

                            //char myword[]={instruction[i]};
                            //cout<<" myword = "<<myword[]<<endl;
                            string temp="";
                            temp=temp+instruction[i];
                            //cout<<"temp "<<temp<<" "<<"up ="<<up<<" "<<"down = "<<down<<" "<<"left = "<<left<<" "<<"right = "<<right<<endl;
                            if(temp==up)start_row--;
                            else if(temp==down)start_row++;
                            else if(temp==left)start_column--;
                            else if(temp==right)start_column++;

                            st=strvec[start_row];
                            char tempword[]={st[start_column]};
                            temp=tempword;
                            //cout<<"star rw "<<start_row<<endl;
                            //cout<<"start colmn "<<start_column<<endl;
                            if(start_row==End_row && start_column==End_column){cnt++;break;}
                            else if(start_row<0 || start_row==n)break;
                            else if(start_column<0 || start_column==m)break;
                            else if(temp=="#")break;

                       }
                   }
               }
           }
       }
   }

   cout<<cnt<<endl;

return 0;
}
/**
for(int i=1;i<=n;i++)
   {
       cin>>st;
       strvec.push_back(st);
   }

   cout<<endl;
   for(int i=0;i<strvec.size();i++)
   {
       cout<<strvec[i]<<endl;
   }
**/


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>

#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>
#include <deque>

#include <ctime>
#include <cassert>

#define nm "C"
#define stdi freopen(nm".in","r",stdin);
#define stdo freopen(nm".out","w",stdout);
#define IO stdi stdo
#define ld long double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define pb push_back
#define p push
#define mp make_pair
#define op cout<<"BUG"<<endl;
#define in insert
#define er erase
using namespace std;
const int maxn=1e5+123,inf=1e9;
int n,m,ans;
string s[100],t;
pair<int,int> vp[4];
bool used[maxn];
int si,sj,ei,ej;
void rec(int r){
	if(r>=4){
		int i=si,j=sj;
		int tt=0;
		for(int tt=0;tt<t.size();tt++){
			int g=t[tt]-48;
			i+=vp[g].f;
			j+=vp[g].s;
			if(min(i,j)<0 || i>=n || j>=m)
				return;
			if(i==ei && j==ej){
				ans++;
				return;
			}
		}
	}else{
		for(int i=0;i<4;i++){
			if(!used[i]){
				used[i]=1;
				if(r==0)
					vp[i]=mp(1,0);
				if(r==1)
					vp[i]=mp(-1,0);
				if(r==2)
					vp[i]=mp(0,1);
				if(r==3)
					vp[i]=mp(0,-1);
				rec(r+1);
				used[i]=0;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>s[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
				si=i,sj=j;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='E')
				ei=i,ej=j;
	cin>>t;
	rec(0);
	cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>

#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>
#include <deque>

#include <ctime>
#include <cassert>

#define nm "C"
#define stdi freopen(nm".in","r",stdin);
#define stdo freopen(nm".out","w",stdout);
#define IO stdi stdo
#define ld long double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define pb push_back
#define p push
#define mp make_pair
#define op cout<<"BUG"<<endl;
#define in insert
#define er erase
using namespace std;
const int maxn=1e5+123,inf=1e9;
int n,r,x[1500];
vector<double>v[150000];
int main(){
    cin>>n>>r;
    for(int i=1;i<=n;i++){
    	cin>>x[i];
    	double mx=r,mxj=inf,ans=0;
    	for(int j=max(0,x[i]-2*r);j<=x[i]+2*r;j++){
    		if(!v[j].empty()){
    			mx=max(mx,v[j][v[j].size()-1]);
    			mxj=j;
    		}
    	}
    	//cout<<mx<<" "<<endl;

    	double l;
       	if(mxj==inf){
   			l=r;
    	}else{
    		l=4*r*r-(x[i]-mxj)*(x[i]-mxj);
       		l=sqrtl(l);
       		l+=mx;
       	}
       	cout<<fixed<<setprecision(6)<<l<<" ";
    	v[x[i]].pb(l);
    }
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>

#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>
#include <deque>

#include <ctime>
#include <cassert>

#define nm "C"
#define stdi freopen(nm".in","r",stdin);
#define stdo freopen(nm".out","w",stdout);
#define IO stdi stdo
#define ld long double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define pb push_back
#define p push
#define mp make_pair
#define op cout<<"BUG"<<endl;
#define in insert
#define er erase
using namespace std;
const int maxn=1e5+123,inf=1e9;
int n,m,ans;
string s[101],t;
pair<int,int> vp[4];
bool used[maxn];
int si,sj,ei,ej;
void rec(int r){
	if(r>=4){
		int i=si,j=sj;
		int tt=0;
		for(int tt=0;tt<t.size();tt++){
			int g=t[tt]-48;
			i+=vp[g].f;
			j+=vp[g].s;
			if(min(i,j)<0 || i>=n || j>=m && s[i][j]!='#')
				return;
			if(i==ei && j==ej){
				ans++;
				return;
			}
		}
	}else{
		for(int i=0;i<4;i++){
			if(!used[i]){
				used[i]=1;
				if(r==0)
					vp[i]=mp(1,0);
				if(r==1)
					vp[i]=mp(-1,0);
				if(r==2)
					vp[i]=mp(0,1);
				if(r==3)
					vp[i]=mp(0,-1);
				rec(r+1);
				used[i]=0;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>s[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
				si=i,sj=j;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='E')
				ei=i,ej=j;
	cin>>t;
	rec(0);
	cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>

#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>
#include <deque>

#include <ctime>
#include <cassert>

#define nm "C"
#define stdi freopen(nm".in","r",stdin);
#define stdo freopen(nm".out","w",stdout);
#define IO stdi stdo
#define ld long double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define pb push_back
#define p push
#define mp make_pair
#define op cout<<"BUG"<<endl;
#define in insert
#define er erase
using namespace std;
const int maxn=1e5+123,inf=1e9;
int n,m,ans;
string s[101],t;
pair<int,int> vp[4];
bool used[maxn];
int si,sj,ei,ej;
void rec(int r){
	if(r>=4){
		int i=si,j=sj;
		int tt=0;
		for(int tt=0;tt<t.size();tt++){
			int g=t[tt]-48;
			i+=vp[g].f;
			j+=vp[g].s;
			if(min(i,j)<0 || i>=n || j>=m || s[i][j]=='#')
				return;
			if(i==ei && j==ej){
				ans++;
				return;
			}
		}
	}else{
		for(int i=0;i<4;i++){
			if(!used[i]){
				used[i]=1;
				if(r==0)
					vp[i]=mp(1,0);
				if(r==1)
					vp[i]=mp(-1,0);
				if(r==2)
					vp[i]=mp(0,1);
				if(r==3)
					vp[i]=mp(0,-1);
				rec(r+1);
				used[i]=0;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>s[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
				si=i,sj=j;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='E')
				ei=i,ej=j;
	cin>>t;
	rec(0);
	cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>

#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>
#include <deque>

#include <ctime>
#include <cassert>

#define nm "C"
#define stdi freopen(nm".in","r",stdin);
#define stdo freopen(nm".out","w",stdout);
#define IO stdi stdo
#define ld long double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define pb push_back
#define p push
#define mp make_pair
#define op cout<<"BUG"<<endl;
#define in insert
#define er erase
using namespace std;
const int maxn=1e5+123,inf=1e9;
int n,m,ans;
string s[101],t;
pair<int,int> vp[4];
bool used[maxn];
int si,sj,ei,ej;
void rec(int r){
	if(r>=4){
		int i=si,j=sj;
		int tt=0;
		for(int tt=0;tt<t.size();tt++){
			int g=t[tt]-48;
			i+=vp[g].f;
			j+=vp[g].s;
			if(min(i,j)<0 || i>=n || j>=m || s[i][j]!='#')
				return;
			if(i==ei && j==ej){
				ans++;
				return;
			}
		}
	}else{
		for(int i=0;i<4;i++){
			if(!used[i]){
				used[i]=1;
				if(r==0)
					vp[i]=mp(1,0);
				if(r==1)
					vp[i]=mp(-1,0);
				if(r==2)
					vp[i]=mp(0,1);
				if(r==3)
					vp[i]=mp(0,-1);
				rec(r+1);
				used[i]=0;
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>s[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
				si=i,sj=j;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j]=='E')
				ei=i,ej=j;
	cin>>t;
	rec(0);
	cout<<ans;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#define ll long long
#define ld long double
#define endl "\n"
#define sysreturn {system("pause");return 0;}
#define ull unsigned long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v)   ((int)((v).size()))
#define forr(c,x,n)  for(long long c=x;c<n;c++)
#define fo(c,n,x)  for(long long c=n;c>=x;c--)
#define oo 999999999999999999
#define Noo 1000000000
#define mod 1000000007
#define Kordy ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef vector<long long> vi;
typedef set<long long>se;
/*
///////////////////////////////////////////////
this code is by Ahmed ....

**      *            **    *    **    *      *
**   **      *       **  *      **     **   **
**  **     *   *     ***        **       *****
** **    *       *   **         **          **
***     *         *  **         **          **
** **    *       *   **     *****           **
**  **     *   *     **    *    **   *      **
**   **      *       **    *    **   **   **
**      *            **     ****       ***

///////////////////////////////////////////////
*/
// start +(end-start)/2
// sum from 1 to n = (n*(n+1))/2
//A=65 // a=97  // num=48
//decimal to binary :: while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
//int value = atoi(myString.c_str());
int dimx[]= {-1, 1, 0, 0};
int dimy[]= {0, 0, 1, -1};
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    else {return gcd(b, a%b);}
}
ll lcm(ll a, ll b)
{return (a*b) / gcd(a, b);}
ll Pow(ll base,ll exponent)
{
    ll result = 1;
    while (exponent > 0)
        {
          if (exponent % 2 == 1)
              result = (result * base) % mod;
          exponent = exponent >> 1;base = (base * base) % mod;
        }
    return result;
}
ld dis(int x1,int y1,int x2,int y2)
{
    return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
bool checklucky(int n)
{
    while(n)
    {
        if(n%10!=4&&n%10!=7)return false;
        n/=10;
    }
    return true;
}
int n,m;
char a[60][60];
pair<int,int>s,e;
map<int,char>mp;
map<char,int>pm;
char arr[4]={'u','d','r','l'};
string nums,dir;
vector<string>St;
bool fool(int c,int i,int j)
{
    if(c==n||i==m||c<0||i<0)return 0;
    if(j==nums.size()&&a[c][i]=='E')return 1;
    else if(j==nums.size()&&a[c][i]!='E')return 0;
    if(a[c][i]=='#')return 0;
    if(a[c][i]=='E')return 1;
    int num=(nums[j]-48);
    if(mp[num]=='u'){dir+=mp[num];fool(c-1,i,j+1);}
    else if(mp[num]=='d'){dir+=mp[num];fool(c+1,i,j+1);}
    else if(mp[num]=='r'){dir+=mp[num];fool(c,i+1,j+1);}
    else if(mp[num]=='l'){dir+=mp[num];fool(c,i-1,j+1);}

}
int main()
{
    Kordy
    cin>>n>>m;
    forr(c,0,n)
    forr(i,0,m)
    {
        cin>>a[c][i];
        if(a[c][i]=='S')s.first=c,s.second=i;
        else if(a[c][i]=='E')e.first=c,e.second=i;
    }
    cin>>nums;
    set<int>ch;
    forr(c,0,sz(nums))
    ch.insert(nums[c]-'0');
    ll ans=0;
    forr(i0,0,4)
    {
        forr(i1,0,4)
        {
            if(i0==i1)continue;
            forr(i2,0,4)
            {
                if(i0==i2||i1==i2)continue;
                forr(i3,0,4)
                {
                    if(i0==i3||i1==i3||i2==i3)continue;
                    mp[0]=arr[i0];pm[arr[i0]]=0;
                    mp[1]=arr[i1];pm[arr[i1]]=1;
                    mp[2]=arr[i2];pm[arr[i2]]=2;
                    mp[3]=arr[i3];pm[arr[i3]]=3;
                  //  cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                    dir="";
                    bool flag=1;
                    if(fool(s.first,s.second,0)==1){  // cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                            forr(c,0,dir.size())
                            {
                                if(!ch.count(pm[dir[c]]))flag=0;
                            }
                            if(flag)
                            {
                            St.push_back(dir);
                            }
                    }
                }
            }
        }
    }/*
    int multi=ch.size();
    int po=multi;
    forr(c,0,ch.size()-1)multi*=po;
    int q=St.size();*/
    cout<<St.size()<<endl;
    // 0 u    1 d   2 r    3 l
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#define ll long long
#define ld long double
#define endl "\n"
#define sysreturn {system("pause");return 0;}
#define ull unsigned long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v)   ((int)((v).size()))
#define forr(c,x,n)  for(long long c=x;c<n;c++)
#define fo(c,n,x)  for(long long c=n;c>=x;c--)
#define oo 999999999999999999
#define Noo 1000000000
#define mod 1000000007
#define Kordy ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef vector<long long> vi;
typedef set<long long>se;
/*
///////////////////////////////////////////////
this code is by Ahmed ....

**      *            **    *    **    *      *
**   **      *       **  *      **     **   **
**  **     *   *     ***        **       *****
** **    *       *   **         **          **
***     *         *  **         **          **
** **    *       *   **     *****           **
**  **     *   *     **    *    **   *      **
**   **      *       **    *    **   **   **
**      *            **     ****       ***

///////////////////////////////////////////////
*/
// start +(end-start)/2
// sum from 1 to n = (n*(n+1))/2
//A=65 // a=97  // num=48
//decimal to binary :: while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
//int value = atoi(myString.c_str());
int dimx[]= {-1, 1, 0, 0};
int dimy[]= {0, 0, 1, -1};
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    else {return gcd(b, a%b);}
}
ll lcm(ll a, ll b)
{return (a*b) / gcd(a, b);}
ll Pow(ll base,ll exponent)
{
    ll result = 1;
    while (exponent > 0)
        {
          if (exponent % 2 == 1)
              result = (result * base) % mod;
          exponent = exponent >> 1;base = (base * base) % mod;
        }
    return result;
}
ld dis(int x1,int y1,int x2,int y2)
{
    return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
bool checklucky(int n)
{
    while(n)
    {
        if(n%10!=4&&n%10!=7)return false;
        n/=10;
    }
    return true;
}
int n,m;
char a[60][60];
pair<int,int>s,e;
map<int,char>mp;
char arr[4]={'u','d','r','l'};
string nums,dir;
vector<string>St;
bool fool(int c,int i,int j)
{
    if(c==n||i==m||c<0||i<0)return 0;
    if(j==nums.size()&&a[c][i]=='E')return 1;
    else if(j==nums.size()&&a[c][i]!='E')return 0;
    if(a[c][i]=='E')return 1;
    int num=(nums[j]-48);
    if(mp[num]=='u'){dir+=mp[num];fool(c-1,i,j+1);}
    else if(mp[num]=='d'){dir+=mp[num];fool(c+1,i,j+1);}
    else if(mp[num]=='r'){dir+=mp[num];fool(c,i+1,j+1);}
    else if(mp[num]=='l'){dir+=mp[num];fool(c,i-1,j+1);}

}
int main()
{
    Kordy
    cin>>n>>m;
    forr(c,0,n)
    forr(i,0,m)
    {
        cin>>a[c][i];
        if(a[c][i]=='S')s.first=c,s.second=i;
        else if(a[c][i]=='E')e.first=c,e.second=i;
    }
    cin>>nums;
    set<char>ch;
    forr(c,0,sz(nums))
    ch.insert(nums[c]);

    ll ans=0;
    forr(i0,0,4)
    {
        forr(i1,0,4)
        {
            if(i0==i1)continue;
            forr(i2,0,4)
            {
                if(i0==i2||i1==i2)continue;
                forr(i3,0,4)
                {
                    if(i0==i3||i1==i3||i2==i3)continue;
                    mp[0]=arr[i0];
                    mp[1]=arr[i1];
                    mp[2]=arr[i2];
                    mp[3]=arr[i3];
                    //cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                    dir="";
                    if(fool(s.first,s.second,0)==1){
                            St.push_back(dir);
                    }
                }
            }
        }
    }
    int multi=ch.size();
    int po=multi;
    forr(c,0,ch.size()-1)multi*=po;
    int q=St.size();
    cout<<min(multi,q)<<endl;
    // 0 u    1 d   2 r    3 l
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#define ll long long
#define ld long double
#define endl "\n"
#define sysreturn {system("pause");return 0;}
#define ull unsigned long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v)   ((int)((v).size()))
#define forr(c,x,n)  for(long long c=x;c<n;c++)
#define fo(c,n,x)  for(long long c=n;c>=x;c--)
#define oo 999999999999999999
#define Noo 1000000000
#define mod 1000000007
#define Kordy ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef vector<long long> vi;
typedef set<long long>se;
/*
///////////////////////////////////////////////
this code is by Ahmed ....

**      *            **    *    **    *      *
**   **      *       **  *      **     **   **
**  **     *   *     ***        **       *****
** **    *       *   **         **          **
***     *         *  **         **          **
** **    *       *   **     *****           **
**  **     *   *     **    *    **   *      **
**   **      *       **    *    **   **   **
**      *            **     ****       ***

///////////////////////////////////////////////
*/
// start +(end-start)/2
// sum from 1 to n = (n*(n+1))/2
//A=65 // a=97  // num=48
//decimal to binary :: while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
//int value = atoi(myString.c_str());
int dimx[]= {-1, 1, 0, 0};
int dimy[]= {0, 0, 1, -1};
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    else {return gcd(b, a%b);}
}
ll lcm(ll a, ll b)
{return (a*b) / gcd(a, b);}
ll Pow(ll base,ll exponent)
{
    ll result = 1;
    while (exponent > 0)
        {
          if (exponent % 2 == 1)
              result = (result * base) % mod;
          exponent = exponent >> 1;base = (base * base) % mod;
        }
    return result;
}
ld dis(int x1,int y1,int x2,int y2)
{
    return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
bool checklucky(int n)
{
    while(n)
    {
        if(n%10!=4&&n%10!=7)return false;
        n/=10;
    }
    return true;
}
int n,m;
char a[60][60];
pair<int,int>s,e;
map<int,char>mp;
char arr[4]={'u','d','r','l'};
string nums,dir;
vector<string>St;
bool fool(int c,int i,int j)
{
    if(c==n||i==m||c<0||i<0)return 0;
    if(j==nums.size()&&a[c][i]=='E')return 1;
    else if(j==nums.size()&&a[c][i]!='E')return 0;
    if(a[c][i]=='E')return 1;
    int num=(nums[j]-48);
    if(mp[num]=='u'){dir+=mp[num];fool(c-1,i,j+1);}
    else if(mp[num]=='d'){dir+=mp[num];fool(c+1,i,j+1);}
    else if(mp[num]=='r'){dir+=mp[num];fool(c,i+1,j+1);}
    else if(mp[num]=='l'){dir+=mp[num];fool(c,i-1,j+1);}

}
int main()
{
    Kordy
    cin>>n>>m;
    forr(c,0,n)
    forr(i,0,m)
    {
        cin>>a[c][i];
        if(a[c][i]=='S')s.first=c,s.second=i;
        else if(a[c][i]=='E')e.first=c,e.second=i;
    }
    cin>>nums;
    ll ans=0;
    forr(i0,0,4)
    {
        forr(i1,0,4)
        {
            if(i0==i1)continue;
            forr(i2,0,4)
            {
                if(i0==i2||i1==i2)continue;
                forr(i3,0,4)
                {
                    if(i0==i3||i1==i3||i2==i3)continue;
                    mp[0]=arr[i0];
                    mp[1]=arr[i1];
                    mp[2]=arr[i2];
                    mp[3]=arr[i3];
                    //cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                    dir="";
                    if(fool(s.first,s.second,0)==1){
                            St.push_back(dir);
                    }
                }
            }
        }
    }
    cout<<St.size()<<endl;
    // 0 u    1 d   2 r    3 l
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#define ll long long
#define ld long double
#define endl "\n"
#define sysreturn {system("pause");return 0;}
#define ull unsigned long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v)   ((int)((v).size()))
#define forr(c,x,n)  for(long long c=x;c<n;c++)
#define fo(c,n,x)  for(long long c=n;c>=x;c--)
#define oo 999999999999999999
#define Noo 1000000000
#define mod 1000000007
#define Kordy ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef vector<long long> vi;
typedef set<long long>se;
/*
///////////////////////////////////////////////
this code is by Ahmed ....

**      *            **    *    **    *      *
**   **      *       **  *      **     **   **
**  **     *   *     ***        **       *****
** **    *       *   **         **          **
***     *         *  **         **          **
** **    *       *   **     *****           **
**  **     *   *     **    *    **   *      **
**   **      *       **    *    **   **   **
**      *            **     ****       ***

///////////////////////////////////////////////
*/
// start +(end-start)/2
// sum from 1 to n = (n*(n+1))/2
//A=65 // a=97  // num=48
//decimal to binary :: while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
//int value = atoi(myString.c_str());
int dimx[]= {-1, 1, 0, 0};
int dimy[]= {0, 0, 1, -1};
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    else {return gcd(b, a%b);}
}
ll lcm(ll a, ll b)
{return (a*b) / gcd(a, b);}
ll Pow(ll base,ll exponent)
{
    ll result = 1;
    while (exponent > 0)
        {
          if (exponent % 2 == 1)
              result = (result * base) % mod;
          exponent = exponent >> 1;base = (base * base) % mod;
        }
    return result;
}
ld dis(int x1,int y1,int x2,int y2)
{
    return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
bool checklucky(int n)
{
    while(n)
    {
        if(n%10!=4&&n%10!=7)return false;
        n/=10;
    }
    return true;
}
int n,m;
char a[60][60];
pair<int,int>s,e;
map<int,char>mp;
char arr[4]={'u','d','r','l'};
string nums,dir;
vector<string>St;
bool fool(int c,int i,int j)
{
    if(c==n||i==m||c<0||i<0)return 0;
    if(j==nums.size()&&a[c][i]=='E')return 1;
    else if(j==nums.size()&&a[c][i]!='E')return 0;
    if(a[c][i]=='E')return 1;
    int num=(nums[j]-48);
    if(mp[num]=='u'){dir+=mp[num];fool(c-1,i,j+1);}
    else if(mp[num]=='d'){dir+=mp[num];fool(c+1,i,j+1);}
    else if(mp[num]=='r'){dir+=mp[num];fool(c,i+1,j+1);}
    else if(mp[num]=='l'){dir+=mp[num];fool(c,i-1,j+1);}

}
int main()
{
    Kordy
    cin>>n>>m;
    forr(c,0,n)
    forr(i,0,m)
    {
        cin>>a[c][i];
        if(a[c][i]=='S')s.first=c,s.second=i;
        else if(a[c][i]=='E')e.first=c,e.second=i;
    }
    cin>>nums;
    set<char>ch;
    forr(c,0,sz(nums))
    ch.insert(arr[nums[c]]);
    ll ans=0;
    forr(i0,0,4)
    {
        forr(i1,0,4)
        {
            if(i0==i1)continue;
            forr(i2,0,4)
            {
                if(i0==i2||i1==i2)continue;
                forr(i3,0,4)
                {
                    if(i0==i3||i1==i3||i2==i3)continue;
                    mp[0]=arr[i0];
                    mp[1]=arr[i1];
                    mp[2]=arr[i2];
                    mp[3]=arr[i3];
                  //  cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                    dir="";
                    bool flag=1;
                    if(fool(s.first,s.second,0)==1){  // cout<<mp[0]<<" "<<mp[1]<<" "<<mp[2]<<" "<<mp[3]<<endl;
                            forr(c,0,dir.size())
                            {
                                if(!ch.count(dir[c]))flag=0;
                            }
                            if(flag)
                            St.push_back(dir);
                    }
                }
            }
        }
    }/*
    int multi=ch.size();
    int po=multi;
    forr(c,0,ch.size()-1)multi*=po;
    int q=St.size();*/
    cout<<St.size()<<endl;
    // 0 u    1 d   2 r    3 l
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, starti, startj, endi, endj;
ll ans;
string s;
char a[111][111], dir[55];
int main () {
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            scanf (" %c", &a[i][j]);
            if (a[i][j] == 'S') {
                starti = i;
                startj = j;
            }
            if (a[i][j] == 'E') {
                endi = i;
                endj = j;
            }
        }
    }
    cin >> s;
    dir[0] = 'D'; dir[1] = 'L'; dir[2] = 'U'; dir[3] = 'R';
    sort (dir, dir + 4);
    do {
        int curi = starti, curj = startj, ok = 1;
        for (int i = 0; i < s.size(); ++i) {
            int num = s[i] - '0';
                if (dir[num] == 'D') {
                    curi++;
                }
                if (dir[num] == 'L') {
                    curj--;
                }
                if (dir[num] == 'U') {
                    curi--;
                }
                if (dir[num] == 'R') {
                    curj++;
                }
                if (a[curi][curj] == '#' || curi > n || curj > m) {
                    ok = 0;
                    break;
                }
                if (a[curi][curj] == 'E') {
                    break;
                }
        }
        if (!ok) {
            continue;
        }
        if (curi == endi && curj == endj) {
            ++ans;
        }
    } while (next_permutation(dir, dir + 4));
    printf ("%I64d", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, starti, startj, endi, endj;
ll ans;
string s;
char a[111][111], dir[55];
int main () {
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            scanf (" %c", &a[i][j]);
            if (a[i][j] == 'S') {
                starti = i;
                startj = j;
            }
            if (a[i][j] == 'E') {
                endi = i;
                endj = j;
            }
        }
    }
    cin >> s;
    dir[0] = 'D'; dir[1] = 'L'; dir[2] = 'U'; dir[3] = 'R';
    sort (dir, dir + 4);
    do {
        int curi = starti, curj = startj, ok = 1;
        for (int i = 0; i < s.size(); ++i) {
            int num = s[i] - '0';
                if (dir[num] == 'D') {
                    curi++;
                }
                if (dir[num] == 'L') {
                    curj--;
                }
                if (dir[num] == 'U') {
                    curi--;
                }
                if (dir[num] == 'R') {
                    curj++;
                }
                if (a[curi][curj] == '#' || curi > n || curj > m ||
                curi < 1 || curj < 1) {
                    ok = 0;
                    break;
                }
                if (a[curi][curj] == 'E') {
                    break;
                }
        }
        if (!ok) {
            continue;
        }
        if (a[curi][curj] == 'E') {
            ++ans;
        }
    } while (next_permutation(dir, dir + 4));
    printf ("%I64d", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, starti, startj, endi, endj;
ll ans;
string s;
char a[111][111], dir[55];
int main () {
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            scanf (" %c", &a[i][j]);
            if (a[i][j] == 'S') {
                starti = i;
                startj = j;
            }
            if (a[i][j] == 'E') {
                endi = i;
                endj = j;
            }
        }
    }
    cin >> s;
    dir[0] = 'D'; dir[1] = 'L'; dir[2] = 'U'; dir[3] = 'R';
    sort (dir, dir + 4);
    do {
        int curi = starti, curj = startj, ok = 1;
        for (int i = 0; i < s.size(); ++i) {
            int num = s[i] - '0';
                if (dir[num] == 'D') {
                    curi++;
                }
                if (dir[num] == 'L') {
                    curj--;
                }
                if (dir[num] == 'U') {
                    curi--;
                }
                if (dir[num] == 'R') {
                    curj++;
                }
                if (a[curi][curj] == '#' || curi > n || curj > m) {
                    ok = 0;
                    break;
                }
                if (a[curi][curj] == 'E') {
                    break;
                }
        }
        if (!ok) {
            continue;
        }
        if (curi == endi && curj == endj ||
            a[curi][curj] == 'E') {
            ++ans;
        }
    } while (next_permutation(dir, dir + 4));
    printf ("%I64d", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, starti, startj, endi, endj;
ll ans;
string s;
char a[111][111], dir[55];
int main () {
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            scanf (" %c", &a[i][j]);
            if (a[i][j] == 'S') {
                starti = i;
                startj = j;
            }
            if (a[i][j] == 'E') {
                endi = i;
                endj = j;
            }
        }
    }
    cin >> s;
    dir[0] = 'D'; dir[1] = 'L'; dir[2] = 'U'; dir[3] = 'R';
    sort (dir, dir + 4);
    do {
        int curi = starti, curj = startj, ok = 1;
        for (int i = 0; i < s.size(); ++i) {
            int num = s[i] - '0';
                if (dir[num] == 'D') {
                    curi++;
                }
                if (dir[num] == 'L') {
                    curj--;
                }
                if (dir[num] == 'U') {
                    curi--;
                }
                if (dir[num] == 'R') {
                    curj++;
                }
                if (a[curi][curj] == '#' || curi > n || curj > m ||
                   curi < 1 || curj < 1) {
                    ok = 0;
                    break;
                }
                if (a[curi][curj] == 'E') {
                    break;
                }
        }
        if (!ok) {
            continue;
        }
        if (curi == endi && curj == endj) {
            ++ans;
        }
    } while (next_permutation(dir, dir + 4));
    printf ("%I64d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
using namespace std;

const int MAX = 55;
typedef long long ll;
vector<string> mat;
int n,m;
pair<int,int> start,end;
ll ans;
vector<int> perm;
string dir;
int mrc[100][100];
bool check(pair<int,int> p){
	if(p.st >=0 and p.st < n){
		if(p.nd>=0 and p.nd < m){
			if(mat[p.st][p.nd] != '#' ){
				mrc[p.st][p.nd] = 1;
				return true;
			}
		}
	}
	return false;
}

int bfs(vector<int> p){
	queue<pair<pair<int,int>, string > > q;
	q.push(mp(start,dir));
	mrc[start.st][start.nd] = 1;
	while(!q.empty()){

		pair<pair<int,int>,string> v= q.front();
		q.pop();
		if(v.nd.size() == 0)
			continue;
		string nxt = v.nd.substr(1,v.nd.size()-1);
		if(v.st.st == end.st and v.st.nd == end.nd){
			return 1;
		}
		char go = v.nd[0];
		for(int i = 0;i<p.size();i++){
			int g = go-'0';
			if(g == p[i])	{
				// down
				if(i == 0){
					pair<int,int> point = v.st;
					point.st+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);
					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//left
				else if(i == 1){
					pair<int,int> point = v.st;
					point.nd-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//right
				else if(i==2){
					pair<int,int> point = v.st;
					point.nd+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//up
				else{
					pair<int,int> point = v.st;
					point.st-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
			}
		}
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 0;i<4;i++) perm.pb(i);

	for(int i = 0;i<n;i++){
		string s;
		cin >> s;
		for(int j = 0;j<s.size();j++){
			if(s[j] == 'S'){
				start = mp(i,j);
			}
			else if(s[j] == 'E')
				end = mp(i,j);
		}
		mat.pb(s);
	}
	cin >> dir;
	do{
		memset(mrc,0,sizeof mrc);
		ans += bfs(perm);
	}while(next_permutation(perm.begin(),perm.end()));
	printf("%d\n",ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
using namespace std;

const int MAX = 55;
typedef long long ll;
vector<string> mat;
int n,m;
pair<int,int> starti,endi;
ll ans = 0;
vector<int> perm;
string dir;
int mrc[100][100];
bool check(pair<int,int> p){
	if(p.st >=0 and p.st < n){
		if(p.nd>=0 and p.nd < m){
			if(mat[p.st][p.nd] != '#' ){
				mrc[p.st][p.nd] = 1;
				return true;
			}
		}
	}
	return false;
}

void bfs(vector<int> p){
	queue<pair<pair<int,int>, string > > q;
	q.push(mp(starti,dir));
	mrc[starti.st][starti.nd] = 1;
	while(!q.empty()){

		pair<pair<int,int>,string> v= q.front();
		q.pop();
		if(v.st.st == endi.st and v.st.nd == endi.nd){
			ans++;
			return;
		}
		if(v.nd.size() == 0)
			continue;
		string nxt = v.nd.substr(1,v.nd.size());

		char go = v.nd[0];
		for(int i = 0;i<p.size();i++){
			int g = go-'0';
			if(g == p[i])	{
				// down
				if(i == 0){
					pair<int,int> point = v.st;
					point.st+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);
					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//left
				else if(i == 1){
					pair<int,int> point = v.st;
					point.nd-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//right
				else if(i==2){
					pair<int,int> point = v.st;
					point.nd+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//up
				else{
					pair<int,int> point = v.st;
					point.st-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
			}
		}
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 0;i<4;i++) perm.pb(i);

	for(int i = 0;i<n;i++){
		string s;
		cin >> s;
		for(int j = 0;j<s.size();j++){
			if(s[j] == 'S'){
				starti = mp(i,j);
			}
			else if(s[j] == 'E')
				endi = mp(i,j);
		}
		mat.pb(s);
	}
	cin >> dir;
	do{
		memset(mrc,0,sizeof mrc);
		bfs(perm);
	}while(next_permutation(perm.begin(),perm.end()));
	printf("%lld\n",ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
using namespace std;

const int MAX = 55;
typedef long long ll;
vector<string> mat;
int n,m;
pair<int,int> starti,endi;
ll ans = 0;
vector<int> perm;
string dir;
int mrc[100][100];
bool check(pair<int,int> p){
	if(p.st >=0 and p.st < n){
		if(p.nd>=0 and p.nd < m){
			if(mat[p.st][p.nd] != '#' ){
				mrc[p.st][p.nd] = 1;
				return true;
			}
		}
	}
	return false;
}

void bfs(vector<int> p){
	queue<pair<pair<int,int>, string > > q;
	q.push(mp(starti,dir));
	mrc[starti.st][starti.nd] = 1;
	while(!q.empty()){

		pair<pair<int,int>,string> v= q.front();
		q.pop();
		if(v.nd.size() == 0)
			continue;
		string nxt = v.nd.substr(1,v.nd.size());
		if(v.st.st == endi.st and v.st.nd == endi.nd){
			ans++;
			return;
		}
		char go = v.nd[0];
		for(int i = 0;i<p.size();i++){
			int g = go-'0';
			if(g == p[i])	{
				// down
				if(i == 0){
					pair<int,int> point = v.st;
					point.st+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);
					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//left
				else if(i == 1){
					pair<int,int> point = v.st;
					point.nd-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//right
				else if(i==2){
					pair<int,int> point = v.st;
					point.nd+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
				//up
				else{
					pair<int,int> point = v.st;
					point.st-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return;
				}
			}
		}
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 0;i<4;i++) perm.pb(i);

	for(int i = 0;i<n;i++){
		string s;
		cin >> s;
		for(int j = 0;j<s.size();j++){
			if(s[j] == 'S'){
				starti = mp(i,j);
			}
			else if(s[j] == 'E')
				endi = mp(i,j);
		}
		mat.pb(s);
	}
	cin >> dir;
	do{
		memset(mrc,0,sizeof mrc);
		bfs(perm);
	}while(next_permutation(perm.begin(),perm.end()));
	printf("%lld\n",ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
using namespace std;

const int MAX = 55;
typedef long long ll;
vector<string> mat;
int n,m;
pair<int,int> starti,endi;
ll ans;
vector<int> perm;
string dir;
int mrc[100][100];
bool check(pair<int,int> p){
	if(p.st >=0 and p.st < n){
		if(p.nd>=0 and p.nd < m){
			if(mat[p.st][p.nd] != '#' ){
				mrc[p.st][p.nd] = 1;
				return true;
			}
		}
	}
	return false;
}

int bfs(vector<int> p){
	queue<pair<pair<int,int>, string > > q;
	q.push(mp(starti,dir));
	mrc[starti.st][starti.nd] = 1;
	while(!q.empty()){

		pair<pair<int,int>,string> v= q.front();
		q.pop();
		if(v.nd.size() == 0)
			continue;
		string nxt = v.nd.substr(1,v.nd.size()-1);
		if(v.st.st == endi.st and v.st.nd == endi.nd){
			return 1;
		}
		char go = v.nd[0];
		for(int i = 0;i<p.size();i++){
			int g = go-'0';
			if(g == p[i])	{
				// down
				if(i == 0){
					pair<int,int> point = v.st;
					point.st+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);
					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//left
				else if(i == 1){
					pair<int,int> point = v.st;
					point.nd-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//right
				else if(i==2){
					pair<int,int> point = v.st;
					point.nd+=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
				//up
				else{
					pair<int,int> point = v.st;
					point.st-=1;
					//printf("(%d %d)-> (%d %d)\n",v.st.st,v.st.nd,point.st,point.nd);

					if(check(point)){
						q.push( mp(point, nxt) );
					}
					else
						return 0;
				}
			}
		}
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 0;i<4;i++) perm.pb(i);

	for(int i = 0;i<n;i++){
		string s;
		cin >> s;
		for(int j = 0;j<s.size();j++){
			if(s[j] == 'S'){
				starti = mp(i,j);
			}
			else if(s[j] == 'E')
				endi = mp(i,j);
		}
		mat.pb(s);
	}
	cin >> dir;
	do{
		memset(mrc,0,sizeof mrc);
		ans += bfs(perm);
	}while(next_permutation(perm.begin(),perm.end()));
	printf("%lld\n",ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 105;
int n, m, sl, rx, ry, ex, ey, cx, cy;
long long res;
char s[MX], maze[55][55];
//Up down left right
const int DX[]{ 0,0,-1,1 }, DY[]{ -1,1,0,0 };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount();
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					for (int d = 0; d < sl; d++)
					{
						switch (s[d])
						{
						case '0':
						{
							cx += DX[i];
							cy += DY[i];
							break;
						}
						case '1':
						{
							cx += DX[o];
							cy += DY[o];
							break;
						}
						case '2':
						{
							cx += DX[p];
							cy += DY[p];
							break;
						}
						case '3':
						{
							cx += DX[j];
							cy += DY[j];
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || maze[cy][cx] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 105;
int n, m, sl, rx, ry, ex, ey, cx, cy, res;
char s[MX], maze[55][55];
//Up down left right
const int DX[]{ 0,0,-1,1 }, DY[]{ -1,1,0,0 };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount() - 1;
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			//2
			for (int p = 0; p < 4; p++)
			{
				//3
				for (int j = 0; j < 4; j++)
				{
					if ((i != o && i != p && i != j) && (o != j && o != p) && (p != j)) {
						cx = rx;
						cy = ry;
						for (int d = 0; d < sl; d++)
						{
							switch (s[d])
							{
							case '0':
							{
								cx += DX[i];
								cy += DY[i];
								break;
							}
							case '1':
							{
								cx += DX[o];
								cy += DY[o];
								break;
							}
							case '2':
							{
								cx += DX[p];
								cy += DY[p];
								break;
							}
							case '3':
							{
								cx += DX[j];
								cy += DY[j];
								break;
							}
							}
							if (cx == ex && cy == ey) {
								res++;
								break;
							}
							else if (cy > n || cx > m || maze[cy][cx] == '#') {
								break;
							}
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 105;
int n, m, sl, rx, ry, ex, ey, cx, cy;
long long res;
char s[MX], maze[55][55];
//Up down left right
const int DX[]{ 0,0,-1,1 }, DY[]{ -1,1,0,0 };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount();
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					for (int d = 0; d < sl; d++)
					{
						switch (s[d])
						{
						case '0':
						{
							cx += DX[i];
							cy += DY[i];
							break;
						}
						case '1':
						{
							cx += DX[o];
							cy += DY[o];
							break;
						}
						case '2':
						{
							cx += DX[p];
							cy += DY[p];
							break;
						}
						case '3':
						{
							cx += DX[j];
							cy += DY[j];
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || cx < 1 || cy < 1 || maze[cy-1][cx-1] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 110;
int n, m, sl, rx, ry, ex, ey, cx, cy, cd[4];
long long res = 0;
char s[MX], maze[55][55];
//Up down left right
const char DIR[]{ 'U','D','R','L' };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount();
	for (int i = 0; i < sl; i++)
	{
		s[i] -= '0';
	}
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					cd[0] = i;
					cd[1] = o;
					cd[2] = p;
					cd[3] = j;

					for (int d = 0; d < sl; d++)
					{
						switch (DIR[cd[s[d]]])
						{
						case 'U':
						{
							cy--;
							break;
						}
						case 'D':
						{
							cy++;
							break;
						}
						case 'L':
						{
							cx--;
							break;
						}
						case 'R':
						{
							cx++;
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || cx < 0 || cy < 0 || maze[cy - 1][cx - 1] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 105;
int n, m, sl, rx, ry, ex, ey, cx, cy;
long long res;
char s[MX], maze[55][55];
//Up down left right
const int DX[]{ 0,0,-1,1 }, DY[]{ -1,1,0,0 };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount();
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					for (int d = 0; d < sl; d++)
					{
						switch (s[d])
						{
						case '0':
						{
							cx += DX[i];
							cy += DY[i];
							break;
						}
						case '1':
						{
							cx += DX[o];
							cy += DY[o];
							break;
						}
						case '2':
						{
							cx += DX[p];
							cy += DY[p];
							break;
						}
						case '3':
						{
							cx += DX[j];
							cy += DY[j];
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || cx < 0 || cy < 0 || maze[cy][cx] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 110;
int n, m, sl, rx, ry, ex, ey, cx, cy, cd[4];
long long res = 0;
char s[MX], maze[55][55];
//Up down left right
const char DIR[]{ 'U','D','R','L' };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount();
	for (int i = 0; i < sl; i++)
	{
		s[i] -= '0';
	}
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					cd[0] = i;
					cd[1] = o;
					cd[2] = p;
					cd[3] = j;
					for (int d = 0; d < sl; d++)
					{
						switch (DIR[cd[s[d]]])
						{
						case 'U':
						{
							cy--;
							break;
						}
						case 'D':
						{
							cy++;
							break;
						}
						case 'L':
						{
							cx--;
							break;
						}
						case 'R':
						{
							cx++;
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || cx < 0 || cy < 0 || maze[cy][cx] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iterator>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <cctype>
#include <climits>
#include <vector>
using namespace std;
#ifdef _DEBUG_
#define DEBUG(X) { cout << #X << '\t' << X << endl; }
#else
#define DEBUG(X)
#endif // _DEBUG_
const int MX = 105;
int n, m, sl, rx, ry, ex, ey, cx, cy;
long long res;
char s[MX], maze[55][55];
//Up down left right
const int DX[]{ 0,0,-1,1 }, DY[]{ -1,1,0,0 };
int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
#ifdef _DEBUG_
	ifstream in("in.txt");
	streambuf *cinbuf = cin.rdbuf();
	cin.rdbuf(in.rdbuf());
	cout.setf(ios_base::boolalpha);
#endif
	cin >> n >> m;
	cin.getline(maze[0], 55);
	for (int i = 0; i < n; i++)
	{
		cin.getline(maze[i], 55);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			switch (maze[i][j])
			{
			case 'S':
			{
				maze[i][j] = '.';
				rx = j + 1;
				ry = i + 1;
				break;
			}
			case 'E':
			{
				ex = j + 1;
				ey = i + 1;
				break;
			}
			}
		}
	}
	cin.getline(s, MX);
	sl = cin.gcount() - 1;
	//0
	for (int i = 0; i < 4; i++)
	{
		//1
		for (int o = 0; o < 4; o++)
		{
			if (o == i) {
				continue;
			}
			//2
			for (int p = 0; p < 4; p++)
			{
				if (p == o || p == i) {
					continue;
				}
				//3
				for (int j = 0; j < 4; j++)
				{
					if (j == p || j == o || j == i) {
						continue;
					}

					cx = rx;
					cy = ry;
					for (int d = 0; d < sl; d++)
					{
						switch (s[d])
						{
						case '0':
						{
							cx += DX[i];
							cy += DY[i];
							break;
						}
						case '1':
						{
							cx += DX[o];
							cy += DY[o];
							break;
						}
						case '2':
						{
							cx += DX[p];
							cy += DY[p];
							break;
						}
						case '3':
						{
							cx += DX[j];
							cy += DY[j];
							break;
						}
						}
						if (cx == ex && cy == ey) {
							res++;
							break;
						}
						else if (cy > n || cx > m || maze[cy][cx] == '#') {
							break;
						}
					}
				}
			}
		}
	}
	cout << res;
#ifdef _DEBUG_
	cout << endl;
	cin.rdbuf(cinbuf);
	system("pause");
#endif
}


#include <conio.h>
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define f(a,m) for (ll i = a; i < m; i++)

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int finish[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = finish[0], e_y = finish[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] - '0';

          if(temp == up)s_y++;
            else if(temp == down) s_y--;
            else if(temp == right) s_x++;
            else if(temp == left) s_x--;

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_x > width) return false;

        if(arr[s_y][s_x] == '#')return false;

    }
            return false;
}

int main(){
    int n,m, c = 0;
    int finish[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S'){start[0]=j; start[1]=i;}
            if(arr[i][j] == 'E'){finish[0]=j; finish[1]=i;}

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j || k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p || p == i || p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, finish) )c++;
                }
          }
        }
    }

    cout<< c;

    return 0;

}


#include <iostream>
#include <string>

using namespace std;

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int end[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = end[0], e_y = end[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] = '0';

        switch(temp) {
            up: s_y++; break;
            down: s_y--; break;
            right: s_x++; break;
            left: s_x--; break;
        }

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_y > width) return false;

        if(arr[s_y][s_x] == '#')return false;
    }
}

int main{
    int n,m, count = 0;
    int end[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S')start[0]=j; start[1]=i;
            if(arr[i][j] == 'E')end[0]=j; end[1]=i;

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j && k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p && p == i && p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, end) )count++;
                }
          }
        }
    }

    cout<< count;

    return 0;

}


#include "bits/stdc++.h"
using namespace std;

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int end[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = end[0], e_y = end[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] = '0';

        switch(temp) {
            up: s_y++; break;
            down: s_y--; break;
            right: s_x++; break;
            left: s_x--; break;
        }

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_y > width) return false;

        if(arr[s_y][s_x] == '#')return false;
    }
}

int main(){
    int n,m, count = 0;
    int end[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S')start[0]=j; start[1]=i;
            if(arr[i][j] == 'E')end[0]=j; end[1]=i;

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j && k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p && p == i && p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, end) )count++;
                }
          }
        }
    }

    cout<< count;

    return 0;

}


#include "bits/stdc++.h"
using namespace std;

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int end[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = end[0], e_y = end[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] - '0';

        switch(temp) {
            up: s_y++; break;
            down: s_y--; break;
            right: s_x++; break;
            left: s_x--; break;
        }

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_x > width) return false;

        if(arr[s_y][s_x] == '#')return false;
    }
}

int main(){
    int n,m, count = 0;
    int end[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S')start[0]=j; start[1]=i;
            if(arr[i][j] == 'E')end[0]=j; end[1]=i;

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j && k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p && p == i && p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, end) )count++;
                }
          }
        }
    }

    cout<< count;

    return 0;

}


#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define f(a,m) for (ll i = a; i < m; i++)

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int finish[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = finish[0], e_y = finish[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] - '0';

          if(temp == up)s_y++;
            else if(temp == down) s_y--;
            else if(temp == right) s_x++;
            else if(temp == left) s_x--;

        cout<<"\t====\t"<< s_y <<"\t====\t"<<s_x<<endl<<endl;

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_x > width) return false;

        if(arr[s_y][s_x] == '#')return false;

    }
            return false;
}

int main(){
    int n,m, c = 0;
    int finish[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S'){start[0]=j; start[1]=i;}
            if(arr[i][j] == 'E'){finish[0]=j; finish[1]=i;}

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j || k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p || p == i || p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, finish) )c++;
                }
          }
        }
    }

    cout<< c;

    return 0;

}


#include "bits/stdc++.h"
using namespace std;

bool can(int up, int down, int right, int left, string str, char **arr,int width, int height, int start[], int end[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = end[0], e_y = end[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] - '0';

        switch(temp) {
            up: s_y++; break;
            down: s_y--; break;
            right: s_x++; break;
            left: s_x--; break;
        }

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0 || s_x < 0 || s_y > width) return false;

        if(arr[s_y][s_x] == '#')return false;
    }
}

int main(){
    int n,m, count = 0;
    int end[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S')start[0]=j; start[1]=i;
            if(arr[i][j] == 'E')end[0]=j; end[1]=i;

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j && k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p && p == i && p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, end) )count++;
                }
          }
        }
    }

    cout<< count;

    return 0;

}


#include <iostream>
#include <string>

using namespace std;

bool can(int up, int down, int right, int left, string s, charr **arr,int width, int height, int start[], int end[]) {
    int temp;
    int s_x = start[0], s_y = start[1];
    int e_x = end[0], e_y = end[1];

    for(int i = 0; i < str.length(); i++) {
        temp = str[i] = '0';

        switch(temp) {
            up: s_y++; break;
            down: s_y--; break;
            right: s_x++; break;
            left: s_x--; break;
        }

        if(s_y == e_y && s_x == e_x) return true;

        if(s_y > height || s_y < 0; s_x < 0 || s_y > width) return false;

        if(arr[s_y][s_x] == '#')return false;
    }
}

int main{
    int n,m, count = 0;
    int end[2], start[2];
    string str;

    cin>>n>>m;

    char **arr = new char*[n];

    for(int i = 0; i < n; i++)
        arr[i] = new char[m];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++){
            cin>>arr[i][j];
            if(arr[i][j] == 'S')start[0]=j; start[1]=i;
            if(arr[i][j] == 'E')end[0]=j; end[1]=i;

        }

    cin>> str;

    for(int i = 0; i < 4; i++) {
       for(int j = 0; j < 4; j++) {
          if(i == j)continue;
          for(int k = 0; k < 4; k++) {
            if(k == j && k == i)continue;
                for(int p = 0; p < 4; p++) {
                    if(k == p && p == i && p == j)continue;
                    if( can(i, j, k, p, str, arr, m-1, n-1, start, end) )count++;
                }
          }
        }
    }

    cout<< count;

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[50][50];
    int n,m,i,sx=-1,sy=-1,j,res=0;
    string w;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
                sx=i,sy=j;
        }
    }
    cin>>w;
    vector<int>p{0,1,2,3};
    const int dx[]={1,0,-1,0};
    const int dy[]={0,1,0,-1};
    do{
        bool fail=false;
        int x=sx,y=sy;
        for(auto c: w)
        {
            x+=dx[p[c - '0']];
            y+=dy[p[c - '0']];
            if(x>=n||x<0||y<0||y>=m||s[x][y]=='#')
            {
                fail=true;
                break;
            }
            if(s[x][y]=='E')
                break;
        }
        if(!fail && s[x][y]=='E')
            res++;
    }while (next_permutation(p.begin(),p.end()));
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[50][50];
    int n,m,i,sx=-1,sy=-1,j,res=0;
    string w;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
                sx=i,sy=j;
        }
    }
    cin>>w;
    vector<int>p{0,1,2,3};
    const int dx[]={1,0,-1,0};
    const int dy[]={0,0,-1,1};
    do{
        bool fail=false;
        int x=sx,y=sy;
        for(auto c: w)
        {
            x+=dx[p[c - '0']];
            y+=dy[p[c - '0']];
            if(x>=n||x<0||y<0||y>=m||s[x][y]=='#')
            {
                fail=true;
                break;
            }
            if(s[x][y]=='E')
                break;
        }
        if(!fail && s[x][y]=='E')
            res++;
    }while (next_permutation(p.begin(),p.end()));
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[50][50];
    int n,m,i,sx=-1,sy=-1,j,res=0;
    string w;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
                sx=i,sy=j;
        }
    }
    cin>>w;
    vector<int>p{0,1,2,3};
    const int dx[]={-1,0,1,0};
    const int dy[]={0,-1,0,1};
    do{
        bool fail=false;
        int x=sx,y=sy;
        for(auto c: w)
        {
            x+=dx[p[c - '0']];
            y+=dy[p[c - '0']];
            if(x>=n||x<0||y<0||y>=m||s[x][y]=='#')
            {
                fail=true;
                break;
            }
            if(s[x][y]=='E')
                break;
        }
        if(!fail && s[x][y]=='E')
            res++;
    }while (next_permutation(p.begin(),p.end()));
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[50][50];
    int n,m,i,sx,sy,j,res=0;
    string w;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
                sx=i;sy=j;
        }
    }
    cin>>w;
    vector<int>p={0,1,2,3};
    const int dx[]={-1,0,1,0};
    const int dy[]={0,-1,0,1};
    do{
        bool fail=false;
        int x=sx,y=sy;
        for(auto c:w)
        {
            x+=dx[p[c-'0']];
            y+=dy[p[c-'0']];
            if(x>=n||x<0||y<0||y>=m||s[x][y]=='#')
            {
                fail=true;
                break;
            }
            if(s[x][y]=='E')
                break;
        }
        if(!fail&&s[x][y]=='E')
            res++;
    }while(next_permutation(p.begin(),p.end()));
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[50][50];
    int n,m,i,sx=-1,sy=-1,j,res=0;
    string w;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='S')
                sx=i,sy=j;
        }
    }
    cin>>w;
    vector<int>p{0,1,2,3};
    const int dx[]={-1,1,0,0};
    const int dy[]={0,0,-1,1};
    do{
        bool fail=false;
        int x=sx,y=sy;
        for(auto c: w)
        {
            x+=dx[p[c - '0']];
            y+=dy[p[c - '0']];
            if(x>=n||x<0||y<0||y>=m||s[x][y]=='#')
            {
                fail=true;
                break;
            }
            if(s[x][y]=='E')
                break;
        }
        if(!fail && s[x][y]=='E')
            res++;
    }while (next_permutation(p.begin(),p.end()));
    cout<<res<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	cout<<index<<" "<<r<<endl;
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	else if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	else if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,l,r);
	else if(s[index] == r+48 && j < m-1 && arr[i][j+1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,l,r);
	if(s[index] == r+48 && j < n-1 && arr[i][j+1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	else if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	else if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,l,r);
	else if(s[index] == r+48 && j < m-1 && arr[i][j+1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	else if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	else if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,l,r);
	else if(s[index] == r+48 && j < n-1 && arr[i][j+1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	else if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	else if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,r,l);
	else if(s[index] == r+48 && j < n-1 && arr[i][j-1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include<iostream>
using namespace std;
int n,m,c,flag;
pair<int,int>x;
char arr[51][51];
string s;
void go(int index,int i,int j,int u,int d,int l,int r)
{
	if(flag == 1)
		return;
	if(arr[i][j] == 'E')
	{
		flag=1;
		c++;
		return;
	}
	if(s[index] == u+48 && i > 0 && arr[i-1][j] != '#')
		go(index+1,i-1,j,u,d,l,r);
	else if(s[index] == d+48 && i < n-1 && arr[i+1][j] !='#')
		go(index+1,i+1,j,u,d,l,r);
	else if(s[index] == l+48 && j > 0 && arr[i][j-1] != '#')
		go(index+1,i,j-1,u,d,r,l);
	else if(s[index] == r+48 && j < n-1 && arr[i][j+1] != '#')
		go(index+1,i,j+1,u,d,l,r);
}
int main()
{
	int i,j,k,l;
	cin>>n>>m;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j] == 'S')
			{
				x.first=i;
				x.second=j;
			}
		}
	cin>>s;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			for(k=0;k<4;k++)
				for(l=0;l<4;l++)
					if(i != j && i != k && i != l && j != k && j != l && k != l)
					{
						flag=0;
						go(0,x.first,x.second,i,j,k,l);
					}
	cout<<c<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;
char arr[50][50];
int n, m, d[4] = {1, 2, 3, 4};
bool valid(int i, int j, int idx){
	return i >= 0 && i < n && j >= 0 && j < m && idx < s.size();
}
ll solve(int i, int j, int idx){
	if (!valid(i, j, idx) || arr[i][j] == '#')
		return 0;
	if (arr[i][j] == 'E')
		return 1;
	if (d[s[idx] - '0'] == 1)
		return solve(i, j + 1, idx + 1);
	if (d[s[idx] - '0'] == 2)
			return solve(i, j - 1, idx + 1);
	if (d[s[idx] - '0'] == 3)
			return solve(i - 1, j, idx + 1);
	return solve(i + 1, j, idx + 1);
}
int main() {
	IO
	cin >> n >> m;
	int idx1, idx2;
	ll cnt = 0;
	for (int i  = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> arr[i][j];
			if (arr[i][j] == 'S'){
				idx1 = i;
				idx2 = j;
			}
		}
	}
	cin >> s;
	do{
		cnt += solve(idx1, idx2, 0);
	}while (next_permutation(d, d+4));
	cout << cnt;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;
char arr[50][50];
int n, m, d[4] = {1, 2, 3, 4};
bool valid(int i, int j){
	return i >= 0 && i < n && j >= 0 && j < m;
}
ll solve(int i, int j, int idx){
	if (!valid(i, j) || arr[i][j] == '#')
		return 0;
	if (idx == s.size())
		if (arr[i][j] == 'E')
				return 1;
	if (arr[i][j] == 'E')
		return 1;
	if (d[s[idx] - '0'] == 1)
		return solve(i, j + 1, idx + 1);
	if (d[s[idx] - '0'] == 2)
			return solve(i, j - 1, idx + 1);
	if (d[s[idx] - '0'] == 3)
			return solve(i - 1, j, idx + 1);
	if (d[s[idx] - '0'] == 4)
	return solve(i + 1, j, idx + 1);

}
int main() {
	IO
	cin >> n >> m;
	int idx1, idx2;
	ll cnt = 0;
	for (int i  = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> arr[i][j];
			if (arr[i][j] == 'S'){
				idx1 = i;
				idx2 = j;
			}
		}
	}
	cin >> s;
	do{
		cnt += solve(idx1, idx2, 0);
		cout << d[0] << d[1] << d[2] << d[3] << '\n';
	}while (next_permutation(d, d+4));
	//cnt = solve(idx1, idx2, 0);
	cout << cnt;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;
char arr[50][50];
int n, m, d[4] = {1, 2, 3, 4};
bool valid(int i, int j){
	return i >= 0 && i < n && j >= 0 && j < m;
}
ll solve(int i, int j, int idx){
	if (!valid(i, j) || arr[i][j] == '#')
		return 0;
	if (idx == s.size())
		if (arr[i][j] == 'E')
				return 1;
	if (arr[i][j] == 'E')
		return 1;
	if (d[s[idx] - '0'] == 1)
		return solve(i, j + 1, idx + 1);
	if (d[s[idx] - '0'] == 2)
			return solve(i, j - 1, idx + 1);
	if (d[s[idx] - '0'] == 3)
			return solve(i - 1, j, idx + 1);
	if (d[s[idx] - '0'] == 4)
	return solve(i + 1, j, idx + 1);

}
int main() {
	IO
	cin >> n >> m;
	int idx1, idx2;
	ll cnt = 0;
	for (int i  = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> arr[i][j];
			if (arr[i][j] == 'S'){
				idx1 = i;
				idx2 = j;
			}
		}
	}
	cin >> s;
	do{
		cnt += solve(idx1, idx2, 0);
		//cout << d[0] << d[1] << d[2] << d[3] << '\n';
	}while (next_permutation(d, d+4));
	//cnt = solve(idx1, idx2, 0);
	cout << cnt;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;
char arr[50][50];
int n, m, d[4] = {1, 2, 3, 4};
bool valid(int i, int j, int idx){
	return i >= 0 && i < n && j >= 0 && j < m && idx < s.size();
}
ll solve(int i, int j, int idx){
	if (!valid(i, j, idx) || arr[i][j] == '#')
		return 0;
	if (arr[i][j] == 'E')
		return 1;
	if (d[s[idx] - '0'] == 1)
		return solve(i, j + 1, idx + 1);
	if (d[s[idx] - '0'] == 2)
			return solve(i, j - 1, idx + 1);
	if (d[s[idx] - '0'] == 3)
			return solve(i - 1, j, idx + 1);
	//if (d[s[idx] - '0'] == 4)
	return solve(i + 1, j, idx + 1);

}
int main() {
	IO
	cin >> n >> m;
	int idx1, idx2;
	ll cnt = 0;
	for (int i  = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> arr[i][j];
			if (arr[i][j] == 'S'){
				idx1 = i;
				idx2 = j;
			}
		}
	}
	cin >> s;
	do{
		cnt += solve(idx1, idx2, 0);
	}while (next_permutation(d, d+4));
	cout << cnt;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void permute(string s,string temp,vector<string>& v) {
	if(s.empty()) {
		v.push_back(temp);
	} else {
		for(ll i=0;i<s.size();i++) {
			char chosen=s[i];
			s.erase(s.begin()+i);
			permute(s,temp+chosen,v);
			s.insert(s.begin()+i,chosen);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//code here;
//	ll t; cin>>t;
//	while(t--) {
		ll n,m; cin>>n>>m;
		ll count=0,stx,sty;
		char ar[n][m];
		for(ll i=0;i<n;i++) for(ll j=0;j<m;j++) {
			cin>>ar[i][j];
			if(ar[i][j]=='S') {
				stx=i;
				sty=j;
			}
		}
		string s1; cin>>s1;
		string s="0123";
		vector<string> v;
		permute(s,"",v);
		for(ll i=0;i<v.size();i++) {
			map<char,char> mp;
			mp[v[i][0]]='L';
			mp[v[i][1]]='R';
			mp[v[i][2]]='U';
			mp[v[i][3]]='D';
			ll x=stx,y=sty;
			for(ll j=0;j<s1.size();j++) {
				if(mp['2']=='R' && mp['3']=='D')
				if(x<0 || x>=n || y<0 || y>=m) break;
				else if(ar[x][y]=='#') break;
				if(mp[s1[j]]=='L') y--;
				else if(mp[s1[j]]=='R') y++;
				else if(mp[s1[j]]=='U') x--;
				else if(mp[s1[j]]=='D') x++;
				if(mp['2']=='R' && mp['3']=='D')
				if(ar[x][y]=='E') {
					count++;
					break;
				}
			}
		}
		cout<<count<<endl;
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void permute(string s,string temp,vector<string>& v) {
	if(s.empty()) {
		v.push_back(temp);
	} else {
		for(ll i=0;i<s.size();i++) {
			char chosen=s[i];
			s.erase(s.begin()+i);
			permute(s,temp+chosen,v);
			s.insert(s.begin()+i,chosen);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//code here;
//	ll t; cin>>t;
//	while(t--) {
		ll n,m; cin>>n>>m;
		ll count=0,stx,sty;
		char ar[n][m];
		for(ll i=0;i<n;i++) for(ll j=0;j<m;j++) {
			cin>>ar[i][j];
			if(ar[i][j]=='S') {
				stx=i;
				sty=j;
			}
		}
		string s1; cin>>s1;
		string s="0123";
		vector<string> v;
		permute(s,"",v);
		for(ll i=0;i<v.size();i++) {
			map<char,char> mp;
			mp[v[i][0]]='L';
			mp[v[i][1]]='R';
			mp[v[i][2]]='U';
			mp[v[i][3]]='D';
			ll x=stx,y=sty;
			for(ll j=0;j<s1.size();j++) {
				if(x<0 || x>=n || y<0 || y>=m) break;
				else if(ar[x][y]=='#') break;
				if(mp[s1[j]]=='L') y--;
				else if(mp[s1[j]]=='R') y++;
				else if(mp[s1[j]]=='U') x--;
				else if(mp[s1[j]]=='D') x++;
				if(x>=0 && x<n && y>=0 && y<m && ar[x][y]=='E') {
					count++;
					break;
				}
			}
		}
		cout<<count<<endl;
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void permute(string s,string temp,vector<string>& v) {
	if(s.empty()) {
		v.push_back(temp);
	} else {
		for(ll i=0;i<s.size();i++) {
			char chosen=s[i];
			s.erase(s.begin()+i);
			permute(s,temp+chosen,v);
			s.insert(s.begin()+i,chosen);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//code here;
//	ll t; cin>>t;
//	while(t--) {
		ll n,m; cin>>n>>m;
		ll count=0,stx,sty;
		char ar[n][m];
		for(ll i=0;i<n;i++) for(ll j=0;j<m;j++) {
			cin>>ar[i][j];
			if(ar[i][j]=='S') {
				stx=i;
				sty=j;
			}
		}
		string s1; cin>>s1;
		string s="0123";
		vector<string> v;
		permute(s,"",v);
		for(ll i=0;i<v.size();i++) {
			map<char,char> mp;
			mp[v[i][0]]='L';
			mp[v[i][1]]='R';
			mp[v[i][2]]='U';
			mp[v[i][3]]='D';
			ll x=stx,y=sty;
			for(ll j=0;j<s1.size();j++) {
				if(ar[x][y]=='E') {
					count++;
					break;
				} else if(x>=n || y>=m) break;
				else if(ar[x][y]=='#') break;
				if(mp[s1[j]]=='L') x--;
				else if(mp[s1[j]]=='R') x++;
				else if(mp[s1[j]]=='U') y++;
				else if(mp[s1[j]]=='D') y--;
			}
		}
		cout<<count<<endl;
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void permute(string s,string temp,vector<string>& v) {
	if(s.empty()) {
		v.push_back(temp);
	} else {
		for(ll i=0;i<s.size();i++) {
			char chosen=s[i];
			s.erase(s.begin()+i);
			permute(s,temp+chosen,v);
			s.insert(s.begin()+i,chosen);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//code here;
//	ll t; cin>>t;
//	while(t--) {
		ll n,m; cin>>n>>m;
		ll count=0,stx,sty;
		char ar[n][m];
		for(ll i=0;i<n;i++) for(ll j=0;j<m;j++) {
			cin>>ar[i][j];
			if(ar[i][j]=='S') {
				stx=i;
				sty=j;
			}
		}
		string s1; cin>>s1;
		string s="0123";
		vector<string> v;
		permute(s,"",v);
		for(ll i=0;i<v.size();i++) {
			map<char,char> mp;
			mp[v[i][0]]='L';
			mp[v[i][1]]='R';
			mp[v[i][2]]='U';
			mp[v[i][3]]='D';
			ll x=stx,y=sty;
			for(ll j=0;j<s1.size();j++) {
				if(x<0 || x>=n || y<0 || y>=m) break;
				else if(ar[x][y]=='#') break;
				if(mp[s1[j]]=='L') y--;
				else if(mp[s1[j]]=='R') y++;
				else if(mp[s1[j]]=='U') x--;
				else if(mp[s1[j]]=='D') x++;
				if(x<n && y<m && ar[x][y]=='E') {
					count++;
					break;
				}
			}
		}
		cout<<count<<endl;
//	}
	return 0;
}