import ru.ifmo.testlib.Checker;
import ru.ifmo.testlib.InStream;
import ru.ifmo.testlib.Outcome;


public class Check implements Checker {
  private String message = null;

  private int check_path (int w, int h, int n, char[][] map, InStream r) {
    int pw = -1, ph = -1;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (map[i][j] == 'V') {
          if (pw != -1 || ph != -1)
            throw new Outcome(Outcome.Type.FAIL, "Second Veniamin in input?");
          pw = j;
          ph = i;
        }
    if (pw == -1 || ph == -1)
      throw new Outcome(Outcome.Type.FAIL, "Veniamin not found in input");

    String path = r.nextToken();
    for (int i = 0; i < path.length(); i++) {
      switch (path.charAt(i)) {
        case 'N': ph--; break;
        case 'E': pw++; break;
        case 'S': ph++; break;
        case 'W': pw--; break;
        default:
      		throw new Outcome(Outcome.Type.WA, "What should Veniamin do for " + path.charAt(i));
      }
      if (ph < 0 || pw < 0 || ph >= h || pw >= w) {
        message = "run out of field at step #" + i;
        return -1;
      }
      if (map[ph][pw] == 'T')
        n--;
    }
    if (n < 0) {
      message = "run out of passport (too many bureaucrats)";
      return -1;
    }
    if (n > 0) {
      message = "passport is'n filled (not enough bureaucrats)";
      return -1;
    }
    return path.length();
  }

  public Outcome test (InStream inf, InStream ouf, InStream ans) {
    int w = inf.nextInt();
    int h = inf.nextInt();
    int n = inf.nextInt();

    char[][] map = new char[h][w];
    for (int i = 0; i < h; i++) {
      String line = inf.nextToken();
      for (int j = 0; j < w; j++)
        map[i][j] = line.charAt(j);
    }

    int ans_jury = check_path(w, h, n, map, ans);
    if (ans_jury < 0)
      return new Outcome(Outcome.Type.FAIL, "[jury fail] " + message);
    int ans_contestant = check_path(w, h, n, map, ouf);
    if (ans_contestant < 0)
      return new Outcome(Outcome.Type.WA, message); // may be PE?
    if (ans_contestant < ans_jury)
      return new Outcome(Outcome.Type.FAIL, "participants's answer is better (" + ans_contestant + " vs " + ans_jury + ")!");
    if (ans_contestant > ans_jury)
      return new Outcome(Outcome.Type.WA, "jury's answer is better (" + ans_jury + " vs " + ans_contestant + ")");
    return new Outcome(Outcome.Type.OK, ans_jury + " moves");
  }
}


