var a:array[-100..100,-100..100] of char;
    b,d:array[0..50,0..5,0..100,0..100] of char;
    c,s:array[0..100] of longint;
    w,h:array[0..100,0..100] of longint;
    i,j,k,l,m,n,u,ans,e,t:longint;
procedure rec(x,y,f,g:longint);
var i,j,l,q,z,kol:longint;
begin
   c[f]:=1;kol:=0;
     begin
       l:=0;
      for i:=1 to h[f,g] do
       for j:=1 to w[f,g] do
      if (a[x+i-1,y+j-1]<>'.') and (b[f,g,i,j]<>'.') then begin l:=1; break; end;
       if (l=1) then c[f]:=0 else
        begin
         for i:=1 to h[f,g] do
          for j:=1 to w[f,g] do
           begin
            d[f,g,i,j]:=a[x+i-1,y+j-1];
            a[x+i-1,y+j-1]:=b[f,g,i,j];
           end;
            for i:=1 to n do
             for j:=1 to m do
              if (a[i,j]<>'.') then inc(kol);
             if (kol=n*m) then
              begin
                inc(ans);
                 if (ans>10000) then begin writeln('TOO MANY'); close(output); halt(0); end;
              end;
          for i:=1 to n do
           for j:=1 to m do
          if (a[i,j]='.') then
           begin
            for q:=1 to k do
             if (c[q]=0) then
              begin
               for z:=1 to s[q] do
                rec(i,j,q,z);
              end;
           end;
          for i:=1 to h[f,g] do
           for j:=1 to w[f,g] do
            a[x+i-1,y+j-1]:=d[f,g,i,j];
           c[f]:=0;
        end;
     end;
end;
begin
  assign(input,'G.in');   reset(input);
  assign(output,'G.out'); rewrite(output);
    readln(n,m); readln(k);
     ans:=0;
    for i:=1 to n do
     for j:=1 to m do
    a[i,j]:='.';
   for i:=1 to k do
    begin
      readln(h[i,1],w[i,1]);
     for j:=1 to h[i,1] do
      begin
        for l:=1 to w[i,1] do
         read(b[i,1,j,l]);
        readln;
      end;
     for j:=1 to h[i,1] do
      for l:=1 to w[i,1] do
      b[i,2,l,h[i,1]-j+1]:=b[i,1,j,l];
       u:=0;s[i]:=1;h[i,2]:=w[i,1]; w[i,2]:=h[i,1];
     if (h[i,1]=w[i,1]) then 
     begin
      for j:=1 to h[i,1] do
       for l:=1 to w[i,1] do
        if (b[i,1,j,l]=b[i,2,j,l]) then inc(u);
        if (u<h[i,1]*w[i,1]) then inc(s[i]);
     end else inc(s[i]);

      for j:=1 to h[i,1] do
       for l:=1 to w[i,1] do
      b[i,s[i]+1,h[i,1]-j+1,w[i,1]-l+1]:=b[i,1,j,l]; u:=0;


     if (h[i,1]=w[i,1]) then
     begin
     for j:=1 to h[i,1] do
      for l:=1 to w[i,1] do
     if (b[i,1,j,l]=b[i,s[i]+1,j,l]) then inc(u);
      if (u<h[i,1]*w[i,1]) then inc(s[i]);
     end else inc(s[i]); 
      h[i,3]:=h[i,1]; w[i,3]:=w[i,1];

      for j:=1 to h[i,1] do
       for l:=1 to w[i,1] do
      b[i,s[i]+1,w[i,1]-l+1,j]:=b[i,1,j,l]; u:=0;


      if (h[i,1]=w[i,1]) then 
      begin 
      for j:=1 to h[i,1] do
       for l:=1 to w[i,1] do
      if (b[i,1,j,l]=b[i,s[i]+1,j,l]) then inc(u);
        if (u<h[i,1]*w[i,1]) then inc(s[i]);
      end else inc(s[i]);
       h[i,4]:=w[i,1]; w[i,4]:=h[i,1];
    end;
     fillchar(c,sizeof(c),0);
    for t:=1 to k do
     for e:=1 to s[t] do
      if (c[t]=0) then rec(1,1,t,e);
     writeln(ans);
   
   close(output);
end.
