{$apptype console}
{$s-,i-,r-,q-,v-,d-,l-,o+}
uses math;
const
	maxn = 10; maxk = 24;
type
	pento = array[1..maxn,1..maxn]of longint;
var
	n,m,k: longint;
	p: array[1..maxk,1..4]of pento;
	w,h: array[1..maxk,1..4]of longint;
	f: array[1..maxk,1..4]of boolean;
	was: array[1..maxk]of boolean;
	a: array[1..maxn,1..maxn]of longint;
	ans: int64;
	xu,yu: array[1..maxk,1..4]of longint;
	s: ansistring;
	z,i,j,ii,jj: longint;

procedure rotate(x,y: longint);
var
	i,j: longint;
begin
        h[x,y] := w[x,y - 1];
        w[x,y] := h[x,y - 1];

	for i := 1 to h[x,y - 1] do
		for j := 1 to w[x,y - 1] do
			p[x,y][j,h[x,y - 1] - i + 1] := p[x,y - 1][i,j];
			
end;

function cmp(t,x,y: longint): boolean;
var
	i,j: longint;
begin
	result := true;
	if (h[t,x] <> h[t,y]) or (w[t,x] <> w[t,y]) then result := false
	else begin
		for i := 1 to h[t,x] do
			for j := 1 to w[t,x] do
				if p[t,x][i,j] <> p[t,y][i,j] then begin
					result := false; exit
				end;
	end;
end;

function canput(z,zz,x,y: longint): boolean;
var
	i,j: longint;
begin
	result := true;
	if (h[z,zz] - xu[z,zz] + x > n) or (w[z,zz] - yu[z,zz] + y > m) then result := false else
	if (1 - xu[z,zz] + x < 1) or (1 - yu[z,zz] + y < 1) then result := false else
	begin
		for i := 1 to h[z,zz] do
			for j := 1 to w[z,zz] do
				if (a[i - xu[z,zz] + x,j - yu[z,zz] + y] = 1) and (p[z,zz][i,j] = 1) then begin
					result := false; exit
				end;
	end;
end;

procedure put(z,zz,x,y: longint);
var
	i,j: longint;
begin
	for i := 1 to h[z,zz] do
		for j := 1 to w[z,zz] do
			a[i - xu[z,zz] + x,j - yu[z,zz] + y] := max(a[i - xu[z,zz] + x,j - yu[z,zz] + y], p[z,zz][i,j]);
end;

procedure rem(z,zz,x,y: longint);
var
	i,j: longint;
begin
	for i := 1 to h[z,zz] do
		for j := 1 to w[z,zz] do
			if p[z,zz][i,j] = 1 then
				a[i - xu[z,zz] + x,j - yu[z,zz] + y] := 0;
end;

procedure generate();
var
	x,y,i,j,z,zz: longint;
begin
	x := 0; if ans > 10000 then exit;

	for i := 1 to n do if x = 0 then
		for j := 1 to m do
			if a[i,j] = 0 then begin
				x := i;
				y := j;
				break;
			end;

	if x = 0 then begin
		ans := ans + 1; exit
	end;

	for z := 1 to k do
		if not was[z] then
			for zz := 1 to 4 do if f[z,zz] then begin
				if canput(z,zz,x,y) then begin
					was[z] := true; put(z,zz,x,y);
					generate();
					was[z] := false; rem(z,zz,x,y);
				end;
			end;
end;

begin
	assign(input,'g.in');reset(input);assign(output,'g.out');rewrite(output);
	readln(n,m,k);

	for z := 1 to k do begin
		readln(h[z,1],w[z,1]);
		for i := 1 to h[z,1] do begin
			readln(s);
			for j := 1 to w[z,1] do
				p[z,1][i,j] := ord(s[j] <> '.');
		end;
		
		rotate(z,2);
		rotate(z,3);
		rotate(z,4);

		f[z,1] := true;
		for i := 2 to 4 do begin
			f[z,i] := true;
			for j := 1 to i - 1 do
				if cmp(z,j,i) then begin
					f[z,i] := false;
					break;
				end;
		end;

		for i := 1 to 4 do
			if f[z,i] then begin
         	xu[z,i] := -1;
				for ii := 1 to h[z,i] do if xu[z,i] < 0 then
					for jj := 1 to w[z,i] do
						if p[z,i][ii,jj] = 1 then begin
							xu[z,i] := ii; yu[z,i] := jj; break 
						end;
			end;
	end;

	fillchar(was,sizeof(was),false); ans := 0;
	generate();

	if ans > 10000 then writeln('TOO MANY') else	writeln(ans);
	close(input);close(output);
end.