(*--------------------------------------------------------------*)
(* ACM ICPC 2009-2010, NEERC                                    *)
(* Northern Subregional Contest                                 *)
(* St Petersburg, October 17, 2009                              *)
(*--------------------------------------------------------------*)
(* Problem I. Image Recognition                                 *)
(*                                                              *)
(* Original idea         Mikhail Dvorkin                        *)
(* Problem statement     Mikhail Dvorkin                        *)
(* Testset               Mikhail Dvorkin                        *)
(*--------------------------------------------------------------*)
(* Checker                                                      *)
(*                                                              *)
(* Author                md                                     *)
(*--------------------------------------------------------------*)
 
uses
  testlib, SysUtils;

const
  MAX = 10;
  MAXSIZE = 20;
  EOP = '.';
  MOVES = ['U', 'D', 'L', 'R'];
  DIGITS = ['0'..'9'];
  VALID = MOVES + DIGITS + ['(', ':', ')'];

function token(c: char): string;
begin
  if c = EOP then
    token := 'end of program'
  else
    token := 'token ' + c;
end;

var
  d, hei, wid: integer;
  w: array[0..MAX-1, 1..MAXSIZE, 1..MAXSIZE] of boolean;
  p: string;
  f: ^InStream;
  image, c, x, y, t: integer;

procedure skip;
begin
  while true do
  begin
    if p[c] in MOVES + DIGITS + BLANKS then
    begin
      inc(c);
      continue;
    end;
    if (p[c] = '(') then
    begin
      inc(c);
      skip;
      if (p[c] <> ':') then
        f^.quit(_WA, 'Compilation error, unexpected ' + token(p[c]) + ' in position ' + inttostr(c));
      inc(c);
      skip;
      if (p[c] <> ')') then
        f^.quit(_WA, 'Compilation error, unexpected ' + token(p[c]) + ' in position ' + inttostr(c));
      inc(c);
      continue;
    end;
    exit;
  end;
end;

procedure process;
begin
  while true do
  begin
    if p[c] in BLANKS then
    begin
      inc(c);
      continue;
    end;
    if p[c] in MOVES then
    begin
      if (p[c] = 'U') then dec(x);
      if (p[c] = 'D') then inc(x);
      if (p[c] = 'L') then dec(y);
      if (p[c] = 'R') then inc(y);
      if (x < 1) or (y < 1) or (x > hei) or (y > wid) then
        f^.quit(_WA, 'On digit ' + inttostr(image) + ' robot leaves the image');
      inc(t);
      inc(c);
      continue;
    end;
    if (p[c] = '(') then
    begin
      inc(c);
      if (w[image][x][y]) then
      begin
        process;
        if (p[c] = ':') then
        begin
          inc(c);
          skip;
          inc(c);
        end;
        continue;
      end
      else
      begin
        skip;
        inc(c);
        process;
        if (p[c] = ')') then
        begin
          inc(c);
        end;
        continue;
      end;
    end;
    exit;
  end;
end;

function time: integer;
var
  i: integer;
begin
  p := f^.readword([], [eofChar]);
  for i := 1 to length(p) do
  begin
    if not (p[i] in BLANKS + VALID) then
      f^.quit(_PE, 'Invalid character ''' + p[i] + '''');
  end;
  p := p + EOP;
  c := 1;
  skip;
  if (p[c] <> EOP) then
    f^.quit(_WA, 'Compilation error, unexpected ' + token(p[c]) + ' in position ' + inttostr(c));
  result := 0;
  for i := 0 to d - 1 do
  begin
    image := i;
    x := 1;
    y := 1;
    c := 1;
    t := 0;
    process;
    if (p[c] <> inttostr(image)) then
      f^.quit(_WA, 'On digit ' + inttostr(image) + ' robot terminates with ''' + p[c] + '''');
    if (t > result) then result := t;
  end;
end;

var
  i, j, k, ja, ca: integer;
  s: string;

begin
  d := inf.readlongint;
  hei := inf.readlongint;
  wid := inf.readlongint;
  for k := 0 to d - 1 do
  begin
    for i := 1 to hei do
    begin
      s := inf.readword(BLANKS, BLANKS);
      for j := 1 to wid do
      begin
        w[k][i][j] := (s[j] = 'W');
      end;
    end;
  end;
  f := @ouf;
  ca := time;
  f := @ans;
  ja := time;
  if (ca > ja) then
    quit(_WA, 'Program is not optimal, ' + inttostr(ca) + ' moves instead of ' + inttostr(ja));
  if (ca < ja) then
    quit(_FAIL, 'Better than jury');
  quit(_OK, inttostr(ca) + ' moves');
end.