#include using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef pair pll; typedef vector vi; typedef vector vl; int n,m,k; bool blocked[1001][1001] = {0}; string seq; int startx, starty; int dist[1001][1001] = {0}; bool seen[1001][1001] = {0}; int DX[4] = {0, -1, 0, 1}; int DY[4] = {1, 0, -1, 0}; int char_to_index(char ch){ if(ch == 'E')return 0; if(ch == 'N')return 1; if(ch == 'W')return 2; return 3; } int main() { cin >> m >> n; rep(c1,0,n){ string s; cin >> s; rep(c2,0,m){ blocked[c1][c2] = (s[c2] == '#'); if(s[c2] == 'S'){ startx = c1; starty = c2; } } } cin >> seq; k = sz(seq); // BFS to find distances queue Q; Q.push({startx,starty}); while(!Q.empty()){ pii p = Q.front(); Q.pop(); if(!seen[p.first][p.second]){ seen[p.first][p.second] = 1; int d = dist[p.first][p.second]; if(d < k){ rep(c1,0,4){ int x = p.first + DX[c1]; int y = p.second + DY[c1]; if(!blocked[x][y] && !seen[x][y] && char_to_index(seq[d]) != c1){ dist[x][y] = dist[p.first][p.second] + 1; Q.push({x, y}); } } } } } rep(c1,0,n){ rep(c2,0,m){ if(blocked[c1][c2]){ cout << "#"; } else{ if(seen[c1][c2] && dist[c1][c2] == k){ cout << "!"; } else{ if(c1 == startx && c2 == starty){ cout << "S"; } else{ cout << "."; } } } } cout << "\n"; } return 0; }