#include #include #include #include using namespace std; typedef long long ll; const int inf = 1000000000; struct point_t { int r, c; int direction; }; const int dx[4] = { 0, 1, 0, -1 }; const int dy[4] = { -1, 0, 1, 0 }; char map[105][105]; char instructions[100]; bool visited[105][105][4]; bool finish[105][105][4]; int ans, L, R, C; void dfs(point_t p) { point_t original = p; visited[p.r][p.c][p.direction] = true; //printf("p.r=%d p.c=%d p.d=%d\n", p.r, p.c, p.direction); for (int i = 0; i < L; i++) { if (instructions[i] == 'S') { p.c += dx[p.direction]; p.r += dy[p.direction]; if (!(p.r >= 0 && p.r < R && p.c >= 0 && p.c < C) || map[p.r][p.c] == 'X') { p.c -= dx[p.direction]; p.r -= dy[p.direction]; } } else if (instructions[i] == 'L') { p.direction += 3; p.direction %= 4; } else if (instructions[i] == 'R') { p.direction += 1; p.direction %= 4; } //printf("instruction %d: p.r=%d p.c=%d p.d=%d\n", i+1, p.r, p.c, p.direction); } if (!visited[p.r][p.c][p.direction]) { dfs(p); } if (finish[p.r][p.c][p.direction]) { //printf("finish p.r=%d p.c=%d p.d=%d\n", original.r, original.c, original.direction); finish[original.r][original.c][original.direction] = true; if (original.direction == 0) { ans++; } } } int main() { while (scanf("%d%d", &R, &C) == 2) { ans = 0; int total = 0; for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { scanf(" %c", &map[r][c]); if (map[r][c] == '.') { for (int i = 0; i < 4; i++) { visited[r][c][i] = finish[r][c][i] = false; } } if (map[r][c] == 'E') { for (int i = 0; i < 4; i++) { visited[r][c][i] = finish[r][c][i] = true; } ans++; } if (map[r][c] == 'X') { for (int i = 0; i < 4; i++) { visited[r][c][i] = finish[r][c][i] = true; } } if (map[r][c] != 'X') { total++; } //printf("%c", map[r][c]); } //printf("\n"); } scanf("%d%s", &L, instructions); //printf("%s\n", instructions); for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { for (int d = 0; d < 4; d++) { if (!visited[r][c][d]) { dfs((point_t) { r,c,d }); } } } } /*for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { printf("%c", finish[r][c][0] ? '1' : '0'); } printf("\n"); }*/ if (ans == total) { printf("OK\n"); } else { printf("%d\n", ans); } } return 0; }