Problem:
Problem F: Mysterious Maze

A robot in a two-dimensional maze again. The maze has an entrance and an exit this time, though.

Just as in the previous problem, the maze is made up of H × W grid cells, its upper side faces north, and each cell is either empty or wall. Unlike the previous, on the other hand, one of the empty cells is connected to the entrance and another to the exit.

The robot is rather complex - there is some control, but not full. It is associated with a controller that has two buttons, namely forward and turn. The forward button moves the robot forward to the next cell, if possible. The robot can not move into a wall nor outside the maze. The turn button turns the robot as programmed. Here the program is a finite sequence of N commands, each of which is either 'L' (indicating a left turn) or 'R' (a right turn). The first turn follows the first command; the second turn follows the second command; similar for the following turns. The turn button stops working once the commands are exhausted; the forward button still works in such a case though. The robot always turns by 90 degrees at once.

The robot is initially put on the entrance cell, directed to the north. Your mission is to determine whether it can reach the exit cell if controlled properly.

Input

The input is a sequence of datasets. Each dataset is formatted as follows.

H W N

s1 ... sN

c1,1c1,2...c1,W

...

cH,1cH,2...cH,W

The first line of a dataset contains three integers H, W and N (1 ≤ H, W ≤ 1,000, 1 ≤ N ≤ 1,000,000).

The second line contains a program of N commands.

Each of the following H lines contains exactly W characters. Each of these characters represents a cell of the maze. "." indicates empty, "#" indicates a wall, "S" indicates an entrance, and "G" indicates an exit. There is exactly one entrance cell and one exit cell.

The end of input is indicated by a line with three zeros.

Output

For each dataset, output whether the robot can reach the exit in a line: "Yes" if it can or "No" otherwise (without quotes).

Sample Input

2 2 1
L
G.
#S
2 2 2
RR
G.
.S
3 3 6
LLLLLL
G#.
...
.#S
0 0 0

Output for the Sample Input

Yes
No
Yes

Solution in C++14 (judge verdict: Memory Limit Exceeded)

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

constexpr int MAX = 1000;

struct State {
    int x, y, dir, n;
    State(int x, int y, int dir, int n)
        : x{x}, y{y}, dir{dir}, n{n} {}
};

int H, W;
string commands;
char field[MAX][MAX];
vector<int> dir[4];
constexpr int dx[4] = {-1, +0, +1, +0};
constexpr int dy[4] = {+0, -1, +0, +1};

bool in_field(int x, int y)
{
    return (0 <= x && x < W && 0 <= y && y < H);
}

void init()
{
    for (int i = 0; i < 4; i++) {
        dir[i].clear();
    }
}

void make(int N)
{
    init();
    int nd = 1;
    for (int i = 0; i < N; i++) {
        if (commands[i] == 'L') {
            nd = (nd + 3) % 4;
        } else {
            nd = (nd + 1) % 4;
        }
        dir[nd].emplace_back(i);
    }    
}

bool solve(int sx, int sy, int gx, int gy)
{
    queue<State> Q;
    Q.push({sx, sy, 1, 0});
    vector<vector<vector<bool>>> d(H, vector<vector<bool>>(W, vector<bool>(4, 0)));
    d[sy][sx][1] = 1;

    while (!Q.empty()) {
        State s = Q.front(); Q.pop();
        int x = s.x, y = s.y;
        if (x == gx && y == gy) return 1;
        
        // move forward
        int nx = s.x + dx[s.dir], ny = s.y + dy[s.dir];
        if (in_field(nx, ny) && field[ny][nx] != '#' && !d[ny][nx][s.dir]) {
            d[ny][nx][s.dir] = 1;
            Q.push({nx, ny, s.dir, s.n});
        }
        
        // turn
        for (int i = 0; i < 4; i++) {
            auto np = lower_bound(dir[i].begin(), dir[i].end(), s.n);
            if (np == dir[i].end()) continue;
            int nn = np - dir[i].begin();
            if (!d[y][x][i]) {
                d[y][x][i] = 1;
                Q.push({x, y, i, nn + 1});                
            }
        }
    }    
    return 0;
}

int main()
{
    int N, sx, sy, gx, gy;
    while (cin >> H >> W >> N, H) {
        cin >> commands;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> field[i][j];
                if (field[i][j] == 'S') { 
                    field[i][j] = '.';
                    sx = j; sy = i;
                } else if (field[i][j] == 'G') {
                    field[i][j] = '.';
                    gx = j; gy = i;
                }
            }
        }
        make(N);
        cout << (solve(sx, sy, gx, gy) ? "Yes" : "No") << endl;
    }
    return 0;
}