#include <iostream>
#include <algorithm>
using namespace std ;
const int N = 55 ;
char g[N][N] ;
int sx , sy ;
int dx[5] = {0 , 1 , 0 , -1 , 0} ;
int dy[5] = {0 , 0 , -1 , 0 , 1} ;
int d[5] ;
int n , m , len;
string s ;
bool dfs(int u , int ex , int ey)
{
if(g[ex][ey] == 'E') return true ;
if(u == len) return false ;
int t = d[s[u] - '0' + 1] ;
int tx = ex + dx[t] , ty = ey + dy[t] ;
if(tx < 1 || ty < 1 || tx > n || ty > m || g[tx][ty] == '#') return false ;
return dfs(u + 1 , tx , ty) ;
}
int main(void)
{
int T ;
scanf("%d" ,&T) ;
while(T --)
{
scanf("%d%d", &n ,&m) ;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++)
{
cin >> g[i][j] ;
if(g[i][j] == 'S')
sx = i , sy = j ;
}
cin >> s ;
len = s.length() ;
int cnt = 0 ;
for(int i = 1 ; i <= 4; i ++) d[i] = i ;
do
{
if(dfs(0 , sx , sy)) cnt ++ ;
} while (next_permutation(d + 1 , d + 5)) ;
cout << cnt << endl ;
}
}