(*--------------------------------------------------------------*)
(* ACM ICPC 2011-2012, NEERC                                    *)
(* Northern Subregional Contest                                 *)
(* St Petersburg, October 29, 2011                              *)
(*--------------------------------------------------------------*)
(* Problem E. Electrification                                   *)
(*                                                              *)
(* Original idea         Mikhail Dvorkin                        *)
(* Problem statement     Mikhail Dvorkin                        *)
(* Testset               Mikhail Dvorkin                        *)
(*--------------------------------------------------------------*)
(* Checker                                                      *)
(*                                                              *)
(* Author                Mikhail Dvorkin                        *)
(*--------------------------------------------------------------*)
 
{$R+,Q+}
{$APPTYPE CONSOLE}
uses
  testlib, SysUtils;

const
  maxsize = 1000;
  maxn = 10;

var
  a, b, h, w, i, j, k, si, so: longint;
  ok: boolean;
  s: string;
  d: array[1..maxn, 1..maxn] of boolean;
  f: array[0..maxsize+1, 0..maxsize+1] of char;
  u, v: array[0..maxsize+1, 1..maxn] of boolean;
  ai, ao: array[0..maxsize, 0..maxsize] of byte;


begin
  h := ouf.readlongint;
  w := ouf.readlongint;
  if (h < 1) or (h > maxsize) or (w < 1) or (w > maxsize) then
    quit(_pe, format('Invalid field dimensions: %dx%d', [h, w]));
  fillchar(f, sizeof(f), '.');
  fillchar(ai, sizeof(ai), 0);
  fillchar(ao, sizeof(ao), 0);
  for i := 1 to h do
  begin
    s := ouf.readword(blanks, blanks);
    if (length(s) <> w) then
      quit(_pe, format('%d-th line has length %d that doesn''t equal %d', [i, length(s), w]));
    for j := 1 to w do
    begin
      f[i][j] := s[j];
      //      j-1    j
      // i-1
      //           *
      //  i
      // ai, ao = Actual number of Input and Output wires
      case f[i][j] of
      '.': begin
      end;
      '/': begin
        inc(ao[i][j - 1]);
        inc(ai[i - 1][j]);
      end;
      '\': begin
        inc(ao[i - 1][j - 1]);
        inc(ai[i][j]);
      end;
      'X': begin
        inc(ao[i][j - 1]);
        inc(ao[i - 1][j - 1]);
        inc(ai[i][j]);
        inc(ai[i - 1][j]);
      end;
      'L': begin
        inc(ao[i][j - 1], 2);
        inc(ai[i - 1][j - 1]);
        inc(ai[i][j]);
      end;
      '7': begin
        inc(ao[i][j - 1]);
        inc(ao[i - 1][j - 1]);
        inc(ai[i - 1][j], 2);
      end;
      else quit(_pe, format('Invalid character field[%d][%d] = #%d', [i, j, ord(f[i][j])]));
      end;
    end;
  end;

  a := inf.readlongint;
  b := inf.readlongint;
  for i := 1 to a do
  begin
    s := inf.readword(blanks, blanks);
    for j := 1 to b do
    begin
      d[i][j] := (s[j] = '1');
    end;
    u[i - 1][i] := true;
  end;
  
  for i := 0 to h do
  begin
    for j := 0 to w do
    begin
      //        j    j+1
      //  i
      //           *
      //  i+1
      // si, so = how many Should be Input and Output wires
      si := -1;
      so := -1;
      ok := true;
      if (f[i][j + 1] = 'L') then
      begin
        si := 1; so := 2;
      end;
      if (f[i + 1][j] = '7') then
      begin
        if (si <> -1) then ok := false;
        si := 2; so := 1;
      end;
      if (j = 0) or (j = w) then
      begin
        if (si <> -1) then ok := false;
        si := 0; so := 0;
      	if (j = 0) and (i < a) then
      	begin
          so := 1;
      	end;
      	if (j = w) and (i < b) then
      	begin
          si := 1;
      	end;
      end;

      if (si = -1) then // no special restriction
      begin
        ok := ok and (((ai[i][j] = 0) and (ao[i][j] = 0)) or ((ai[i][j] = 1) and (ao[i][j] = 1)));
      end
      else
      begin
        ok := ok and (ai[i][j] = si) and (ao[i][j] = so);
      end;
      if not ok then
        quit(_pe, format('Invalid connection at (%d, %d) - I/O is (%d, %d) instead of (%d, %d)', [i, j, ai[i][j], ao[i][j], si, so]));
    end;
  end;

  for j := 1 to w do
  begin
    for i := h downto 1 do
    begin
      //      j-1    j
      // i-1
      //           *
      //  i
      for k := 1 to a do
      begin
        case f[i][j] of
        '.': begin
        end;
        '/': begin
          v[i - 1][k] := u[i][k];
        end;
        '\': begin
          v[i][k] := u[i - 1][k];
        end;
        'X': begin
          v[i - 1][k] := u[i][k];
          v[i][k] := u[i - 1][k];
        end;
        'L': begin
          u[i - 1][k] := u[i][k];
          v[i][k] := u[i][k];
        end;
        '7': begin
          v[i - 1][k] := u[i - 1][k] xor u[i][k];
        end;
        end;
      end;
    end;
    for i := 0 to h do
    begin
      for k := 1 to a do
      begin
        u[i][k] := v[i][k];
        v[i][k] := false;
      end;
    end;
  end;

  for i := 1 to b do
  begin
    for k := 1 to a do
    if (u[i - 1][k] <> d[k][i]) then
      quit(_wa, format('Wrong dependency of %d-th lamp on the %d-th switch', [i, k]));
  end;
  quit(_ok, format('Correct field %dx%d', [h, w]));
end.
