#include <iostream>
#include <cassert>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <stack>

int memo[55][55][6][1030];

std::string map[51];
std::string prog;
int back[1010];

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

int main() {
    int h, w;
    int y, x, gy, gx;

    std::cin >> h >> w;

    for(int i = 0; i < h; ++i) {
        std::cin >> map[i];

        for(int j = 0; j < w; ++j) {
            if(map[i][j] == 's') {
                y = i, x = j;
            }else if(map[i][j] == 'g') {
                gy = i, gx = j;
            }
        }
    }
    std::stack<int> st;
    std::cin >> prog;

    for(int i = 0; i < prog.size(); ++i) {
        if(prog[i] == '{' || prog[i] == '[') {
            st.push(i);
        }else if(prog[i] == '}' || prog[i] == ']') {
            auto s = st.top();
            st.pop();

            back[i] = s;
            back[s] = i;
        }
    }

    int ptr = 0;
    int dir = 0;

    auto calcState = [&](char c) {
        if(c == 'C') {
            return map[y + dy[dir]][x + dx[dir]] == '#';
        }else if(c == 'T') {
            return true;
        }else if(c == 'N'){
            return dir == 0;
        }else if(c == 'E'){
            return dir == 1;
        }else if(c == 'S'){
            return dir == 2;
        }else if(c == 'W'){
            return dir == 3;
        }
        assert(false);
        return false;
    };
    int step = 0;
    while(1) {
        // std::cout << y << " " << x << " " << dir << " " << ptr << std::endl;
        if(map[y][x] == 'g') {
            std::cout << step << std::endl;

            return 0;
        }
        if(ptr == prog.size()) {
            std::cout << -1 << std::endl;

            return 0;
        }
        if(memo[y][x][dir][ptr] == 1) {
            std::cout << -1 << std::endl;
            return 0;
        }
        memo[y][x][dir][ptr] = 1;

        if(prog[ptr] == '^') {
            ++step;
            if(map[y + dy[dir]][x + dx[dir]] == '#') {
                goto END;
            }
            y += dy[dir];
            x += dx[dir];
        }else if (prog[ptr] == 'v') {
            ++step;
            if(map[y - dy[dir]][x - dx[dir]] == '#') {
                goto END;
            }
            y -= dy[dir];
            x -= dx[dir];
        }else if (prog[ptr] == '<') {
            dir = (dir - 1 + 4) % 4;
            ++step;
        }else if (prog[ptr] == '>') {
            dir = (dir + 1) % 4;
            ++step;
        }else if (prog[ptr] == '[' || prog[ptr] == '{') {
            bool stat;
            int prev = ptr;
            if (prog[ptr + 1] == '~') {
                stat = !calcState(prog[ptr + 2]);
                ptr += 2;
            }else {
                stat = calcState(prog[ptr + 1]);
                ptr += 1;
            }

            if(!stat) {
                ptr = back[prev];
            }
        }else if (prog[ptr] == '}') {
            ptr = back[ptr] - 1;
        }

        END:;
        ++ptr;
    }
}
