//#include<cstdio>
//#include<iostream>
//using namespace std;

#include<stdio.h>
#include<string.h>

struct fig0{
	int w,h;
	char a[10][10];
	};

void rotate(fig0 *dst, fig0 *src){
	int i,j;
	dst->h = src->w;
	dst->w = src->h;
	for(i=0; i<src->h; i++)
		for(j=0; j<src->w; j++)
		 dst->a[j][dst->w-i-1] = src->a[i][j];
	};

struct figure{
	int rotcnt;
	fig0 f[4];
	};

figure f[25];

char board[22][22];
int n,m,k,i,j,x,y;
char s[20];

int ansv;

void solve(int i){
	int x,y,z,x2,y2;
	char b;
	if(i==k){
		b=0;
		for(y=0;y<n;y++)
			for(x=0;x<m;x++)
				b|=(board[y][x]!=1);
		if(!b){
			++ansv;
			}
		return;
		}
	for(z=0;z<f[i].rotcnt;z++){
		for(y=0;y<n-f[i].f[z].h+1;y++)
			for(x=0;x<m-f[i].f[z].w+1;x++){
				b=0;
				for(y2=0;y2<f[i].f[z].h;y2++)
					for(x2=0;x2<f[i].f[z].w;x2++){
						b|=(board[y+y2][x+x2]+=f[i].f[z].a[y2][x2])>1;
						}
				if (!b) {
					solve(i+1);
					if(ansv>10000) return;
					}
				for(y2=0;y2<f[i].f[z].h;y2++)
					for(x2=0;x2<f[i].f[z].w;x2++){
						board[y+y2][x+x2]-=f[i].f[z].a[y2][x2];
						}
				}
		}
	}

int main()
{
	freopen("G.in", "r", stdin);
	freopen("G.out", "w", stdout);
	scanf("%d%d",&n,&m);
	scanf("%d", &k);
	for(i=0;i<k;i++){
		scanf("%d%d",&(f[i].f[0].h),&(f[i].f[0].w));		
		for(y=0;y<f[i].f[0].h;y++){
			scanf("%s",s);
			for(x=0;x<f[i].f[0].w;x++)
				f[i].f[0].a[y][x]=(s[x]!='.');
			}
		rotate(&(f[i].f[1]),&(f[i].f[0]));
		if (memcmp(f[i].f[1].a,f[i].f[0].a,sizeof(char)*100)){
			rotate(&(f[i].f[2]),&(f[i].f[1]));
			if(memcmp(f[i].f[2].a,f[i].f[1].a,sizeof(char)*100)){
				rotate(&(f[i].f[3]),&(f[i].f[2]));
				f[i].rotcnt=4;
				}
			else f[i].rotcnt=2;
			}
		else f[i].rotcnt=1;
		}
	memset(board,0,sizeof(board));
	ansv=0;
	solve(0);
	if (ansv>10000) 
		printf("TOO MANY\n");
	else
		printf("%d\n",ansv);
		
	return 0;
}