#include using namespace std; #define REP(i, n) for(int i=0; i<(n); ++i) #define FOR(i, a, b) for(int i=(a); i<=(b); ++i) #define FORD(i, a, b) for(int i=(a); i>=(b); --i) #define vi vector #define pii pair #define mp make_pair const int INF = 1<<29; #define MAXN 1111 int N, M; char m[MAXN][MAXN]; int Q; char q[MAXN]; bool ok[111][111][11][4]; int D[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; void dfs(int x, int y, int l, int d) { ok[x][y][l][d] = true; int ll = l ? l - 1 : Q - 1; FOR(xx, x - 1, x + 1) FOR(yy, y - 1, y + 1) REP(dd, 4) { if (m[xx][yy] == 'X' || ok[xx][yy][ll][dd]) continue; int nx = xx, ny = yy, nd = dd; if (q[ll] == 'R') nd = (nd + 1) % 4; if (q[ll] == 'L') nd = (nd + 3) % 4; if (q[ll] == 'S') { nx += D[nd][0]; ny += D[nd][1]; if (m[nx][ny] == 'X') { nx = xx; ny = yy; } } if (nx == x && ny == y && nd == d) dfs(xx, yy, ll, dd); } } int main() { while (scanf("%d%d", &N, &M) == 2) { REP(i, N) scanf("%s", m[i + 1] + 1); REP(i, N + 2) m[i][0] = m[i][M + 1] = 'X'; REP(j, M + 2) m[0][j] = m[N + 1][j] = 'X'; int ex = -1, ey = -1; REP(i, N + 2) REP(j, M + 2) if (m[i][j] == 'E') { ex = i; ey = j; m[i][j] = '.'; } scanf("%d", &Q); scanf("%s", q); int ans = 0, total = 0; REP(i, N + 3) REP(j, M + 3) REP(k, Q + 2) REP(d, 4) ok[i][j][k][d] = false; REP(k, Q) REP(d, 4) dfs(ex, ey, k, d); REP(i, N + 2) REP(j, M + 2) if (m[i][j] == '.') { ans += ok[i][j][0][0]; // if (ok[i][j][0][0]) printf("x: %d y: %d\n", i, j); total++; } if (ans == total) printf("OK\n"); else printf("%d\n", ans); } return 0; }