# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <queue>
# define cls(a) memset(a, 0, sizeof(a))
# define oo 2147483647
# define ll long long
using namespace std;
struct node
{
int x, y, d;
} s;
int n, m,tot;
int map[51][51],boo[51][51][1001],step[1001];
int dx[5] = {0, -1, 1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};
queue <node> Q;
inline void bfs()
{
boo[s.x][s.y][1] = 1;
s.d = 1; //移动的方向
Q.push(s);//把s压入队列中
while(!Q.empty()){//如果队列不为空
node v=Q.front(); //把v变成与node定义相同的结构体,并使结构体v中各元素的值与node中各元素的值相同;
Q.pop();//弹出队首元素
v.x+=dx[step[v.d]];//从开始点开始项规定方向移动(这是第v.d次的移动,v.d代表移动方向);
v.y+=dy[step[v.d]];//同上;
if(v.x>0&&v.x<=n&&v.y>0&&v.y<=m&&!boo[v.x][v.y][v.d]&&map[v.x][v.y]) {//移动不超范围,并且在该方向下没走到过,并且没有围墙
if(v.d == tot) map[v.x][v.y] = 2;//如果走完了,把该点记录为能走到的最后一个点
boo[v.x][v.y][v.d] = 1;//否则标记该点走过
Q.push(v);//把结构体v压入队列;
if(v.d + 1 <= tot)
v.d++,
Q.push(v);//如果没走完走,继续走
}
}
}
int main()
{
scanf("%d%d", &n, &m);//读入n,m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++){
char ch;
scanf(" %c", &ch); //输入字符
if(ch == '.') map[i][j] = 1;//用map数组将字符数组转化为数字数组;
if(ch == '*') s.x = i, s.y = j, map[i][j] = 1;//s.x记录下开始点的横坐标,s.y记录下开始点的纵坐标,开始点可走;
}
scanf("%d", &tot);//输入所走方向数;
for(int i = 1; i <= tot; i++){
char ch[10];
scanf(" %s", ch);//输入方向
if(ch[0] == 'N') step[i] = 1;
if(ch[0] == 'S') step[i] = 2;
if(ch[0] == 'W') step[i] = 3;
if(ch[0] == 'E') step[i] = 4;//用1,2,3,4四个数代替北南西东,用step数组存方向
}
bfs();//开始搜索 (重点)
for(int i = 1; i <= n; i++, printf("\n"))
for(int j = 1; j <= m; j++){
char ch = 'X';
if(map[i][j] == 2) ch = '*';
if(map[i][j] == 1) ch = '.';
printf("%c", ch);//输出结束后的状态图;
}
return 0;
}