program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n,m,k,t1,t2,t3,t31,t32: integer;
  a: array[1..10,1..10] of char;
  kw,kh: array[1..24] of integer;
  ka: array[1..24,1..10,1..10] of char;
  sum: int64;
  F: textfile;

function fakt(ff: integer):integer;
begin
  if (ff=1)or(ff=0) then fakt:=1 else
    fakt:=ff*fakt(ff-1);
end;

begin
  AssignFile(F,'G.in');
  Reset(F);
  Readln(F,n,m);
  Readln(F,k);

  for t1:=1 to k do
  begin
    readln(F,kh[t1],kw[t1]);
    for t2:=1 to kh[t1] do
    begin
      for t3:=1 to kw[t1] do
        read(F,ka[t1,t2,t3]);
      readln(F);
    end;
  end;
  CloseFile(F);

  sum:=1;
  t31:=0;
  t32:=0;
  for t1:=1 to 24 do if ((kw[t1]=1) and (kh[t1]=1)) then inc(t31);
  for t1:=1 to 24 do if ((kw[t1]=2) and (kh[t1]=1)) or ((kw[t1]=1) and (kh[t1]=2)) then inc(t32);

  if (t31>9)or(t32>9)or(t31+t32>14) then
  begin
    AssignFile(F,'G.out');
    rewrite(F);
    writeln(F,'TOO MANY');
    closefile(F);
    halt;
  end
  else
    sum:=sum*fakt(t31)*fakt(t32)*4;
  AssignFile(F,'G.out');
  rewrite(F);
  writeln(F,sum);
  closefile(F);
end.
