#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;
queue< pair<int,int> > q1,q2;
bool inq[55][55];
int dir[1003][2];
bool map[55][55];//0 notgo,1 go
//north={-1,0} south={1,0} west={0,-1} east={0,1}
int n,dircnt,r,c,stx,sty;
void bfs(int sx,int sy)
{
q1.push( make_pair(sx,sy) );
inq[sx][sy]=1;
int i=1;
while(i<=n)
{
// cout<<1;
while(q1.size())//move
{
// cout<<2;
pair<int,int> tmpp=q1.front();
int tmpx=tmpp.first,tmpy=tmpp.second;
q1.pop();inq[tmpx][tmpy]=0;
int jx=tmpx+dir[i][0],jy=tmpy+dir[i][1];
while( map[jx][jy] && jx>0 && jx<=r && jy>0 && jy<=c)
{
// cout<<i<<' '<<dir[i][0]<<' '<<dir[i][1]<<endl;
if(!inq[jx][jy])
{
q2.push( make_pair(jx,jy) );
inq[jx][jy]=1;
}
jx=jx+dir[i][0];jy=jy+dir[i][1];
}
}
while(q2.size())//copy q2 to q1
{
// cout<<4;
q1.push( q2.front() );
q2.pop();
}
i++;
}
}
int main()
{
cin>>r>>c;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
char tc;
cin>>tc;
if(tc=='.')
{
map[i][j]=1;
}
else if(tc=='X')
{
map[i][j]=0;
}
else //'*'
{
stx=i;sty=j;
map[i][j]=1;
}
}
}
cin>>n;
string tstr;
for(int i=1;i<=n;i++)
{
cin>>tstr;
if(tstr[0]=='N')//north
{
dir[++dircnt][0]=-1,dir[dircnt][1]=0;
}
else if(tstr[0]=='S')//south
{
dir[++dircnt][0]=1,dir[dircnt][1]=0;
}
else if(tstr[0]=='W')//west
{
dir[++dircnt][0]=0,dir[dircnt][1]=-1;
}
else if(tstr[0]=='E')//east
{
dir[++dircnt][0]=0,dir[dircnt][1]=1;
}
}
bfs(stx,sty);
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
if(inq[i][j])
{
cout<<'*';
}
else if(map[i][j]==1)
{
cout<<'.';
}
else if(map[i][j]==0)
{
cout<<'X';
}
}
cout<<endl;
}/*
for(int i=1;i<=dircnt;i++)
{
cout<<dir[i][0]<<' '<<dir[i][1]<<endl;
}*/
return 0;
}
why?