import java.io.*; import java.util.*; public class Karel { static int UP = 0; static int RIGHT = 1; static int DOWN = 2; static int LEFT = 3; static int stepX[] = { 0, 1, 0, -1 }; static int stepY[] = { -1, 0, 1, 0 }; public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = in.readLine()) != null) { String parts[] = line.split(" "); int H = Integer.parseInt(parts[0]); int W = Integer.parseInt(parts[1]); char map[][] = new char[H+2][W+2]; for(int j = 0; j < W+2; j++) { map[0][j] = 'X'; map[H+1][j] = 'X'; } for(int i = 1; i <= H; i++) { line = in.readLine(); map[i][0] = 'X'; map[i][W+1] = 'X'; for(int j = 0; j < W; j++) { map[i][j+1] = line.charAt(j); } } int P = Integer.parseInt(in.readLine()); char program[] = in.readLine().toCharArray(); Queue q = new LinkedList(); for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(map[i][j] == 'E') { for(int h = 0; h < 4; h++) { for(int p = 0; p < P; p++) q.add(new State(i,j,p,h)); } } } } boolean reachable[][][][] = new boolean[H+2][W+2][P][4]; while(!q.isEmpty()) { State s = q.poll(); if(reachable[s.y][s.x][s.pc][s.heading]) continue; reachable[s.y][s.x][s.pc][s.heading] = true; int prevP = (s.pc-1+P)%P; if(program[prevP] == 'L') { // System.out.printf("(%d, %d) pc=%d %d -> ", s.y, s.x, s.pc, s.heading); State next = new State(s.y, s.x, prevP, right(s.heading)); q.add(next); // System.out.println(); } else if(program[prevP] == 'R') { // System.out.printf("(%d, %d) pc=%d %d -> ", s.y, s.x, s.pc, s.heading); State next = new State(s.y, s.x, prevP, left(s.heading)); q.add(next); // System.out.println(); } else { int dH = stepY[s.heading]; int dW = stepX[s.heading]; // System.out.printf("(%d, %d) pc=%d %d -> ", s.y, s.x, s.pc, s.heading); if(map[s.y+dH][s.x+dW] == 'X') q.add(new State(s.y, s.x, prevP, s.heading)); // System.out.println(); // System.out.printf("(%d, %d) pc=%d %d -> ", s.y, s.x, s.pc, s.heading); if(map[s.y-dH][s.x-dW] != 'X') q.add(new State(s.y-dH, s.x-dW, prevP, s.heading)); // System.out.println(); } } int impossible = 0; for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(map[i][j] == 'X') continue; if(!reachable[i][j][0][UP]) impossible++; } } /* for(int i = 0; i < H+2; i++) { for(int j = 0; j < W+2; j++) { System.out.print(map[i][j]); } System.out.println(); } for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(map[i][j] == 'X') continue; for(int p = 0; p < P; p++) { for(int h = 0; h < 4; h++) { if(reachable[i][j][p][h]) { int ni=0,nj=0,np=0,nh=0; if(program[p] == 'L') { ni=i;nj=j;np=(p+1)%P;nh=left(h); } if(program[p] == 'R') { ni=i;nj=j;np=(p+1)%P;nh=right(h); } if(program[p] == 'S') { ni=i+stepY[h];nj=j+stepX[h];np=(p+1)%P;nh=h; } if(!reachable[ni][nj][np][nh] && map[ni][nj] != 'X') System.out.printf("1 State (%d,%d) pc=%d %d is unreachable from (%d,%d) pc=%d %d\n", i, j, p, h, ni, nj, np, nh); if(!reachable[i][j][np][nh] && map[ni][nj] == 'X') System.out.printf("2 State (%d,%d) pc=%d %d is unreachable\n", i, j, p, h); } else { int ni=0,nj=0,np=0,nh=0; if(program[p] == 'L') { ni=i;nj=j;np=(p+1)%P;nh=left(h); } if(program[p] == 'R') { ni=i;nj=j;np=(p+1)%P;nh=right(h); } if(program[p] == 'S') { ni=i+stepY[h];nj=j+stepX[h];np=(p+1)%P;nh=h; } if(reachable[ni][nj][np][nh] && map[ni][nj] != 'X') System.out.printf("1 State (%d,%d) pc=%d %d is reachable\n", i, j, p, h); if(reachable[i][j][np][nh] && map[ni][nj] == 'X') System.out.printf("2 State (%d,%d) pc=%d %d is reachable\n", i, j, p, h); } } } } }*/ /* while(true) { if(Integer.parseInt(parts[0]) == -1) break; parts = in.readLine().split(" "); System.out.println(reachable[Integer.parseInt(parts[0])][Integer.parseInt(parts[1])][Integer.parseInt(parts[2])][Integer.parseInt(parts[3])]); }*/ if(impossible == 0) System.out.println("OK"); else System.out.println(impossible); } } static int right(int heading) { return (heading+1)%4; } static int left(int heading) { return (heading+3)%4; } static class State { int y; int x; int pc; int heading; public State(int y, int x, int pc, int heading) { this.y=y; this.x=x; this.pc=pc; this.heading=heading; //System.out.printf("(%d, %d) pc=%d %d", y, x, pc, heading); } } }