#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
#include <set>
#include <vector>
#include <map>
#include <deque>
#include <stack>
#include <algorithm>
#include <string>
#include <time.h>

using namespace std;

struct par{
	int x,y;
};

char mas[10][10];
char polys[24][4][10][10];
int num[24][4];
par siz[24][4];
int n,m,k;
int h,w;
char temp[10][10];
set<string> kop;
bool used[24];
int iter = 0;
int tim;

string identity()
	{
	string res = "";
	for(int i=0; i<n; i++)
	for(int j=0; j<m; j++)
		res+=mas[i][j];
	return res;
	}

bool check(int x, int y, int fig, int v)
	{
	int dy =num[fig][v] ;
	if(y-dy<0||n-x+1<siz[fig][v].x||m-y+dy+1<siz[fig][v].y) return false;
	for(int i = x; i<x+siz[fig][v].x; i++)
	for(int j = y-dy; j<y-dy+siz[fig][v].y; j++)
		if(mas[i][j]!='.'&&polys[fig][v][i-x][j-y+dy]!='.')
			return false;
	return true;
	}

void place(int x, int y, int fig, int v)
	{
	int dy =num[fig][v] ;
	for(int i = x; i<x+siz[fig][v].x; i++)
	for(int j = y-dy; j<y-dy+siz[fig][v].y; j++)
		if(polys[fig][v][i-x][j+dy-y]!='.') mas[i][j] = polys[fig][v][i-x][j-y+dy];
	}	
void displace(int x, int y, int fig, int v)
	{
	int dy =num[fig][v] ;
	for(int i = x; i<x+siz[fig][v].x; i++)
	for(int j = y-dy; j<y-dy+siz[fig][v].y; j++)
		if(polys[fig][v][i-x][j+dy-y]!='.') mas[i][j] = '.';
	}	


void process()
	{
	iter++; 
	if(iter>800000)
		{
		cout<<"TOO MANY"<<endl;
		exit(0);
		}
	int x,y;
	
	for(x=0; x<n; x++)
	for(y=0; y<m; y++)
		if(mas[x][y]=='.')
			goto A;

	kop.insert(identity());
//	cout<<"Found: "<<identity()<<endl;
//	cout<<"Size: "<<kop.size()<<endl;
	if(kop.size()>10000) 
		{
		cout<<"TOO MANY"<<endl;
		exit(0);
		}
	A:
	for(int i=0; i<k; i++)
		if(!used[i])
			for(int v = 0; v<4; v++)
				{
				if(check(x,y,i,v)) 
					{
					used[i] = true;
					place(x,y,i,v);
					process();
					displace(x,y,i,v);
					used[i] = false;
					}
				}	

	}

int main()
{
freopen("G.in", "r", stdin);
freopen("G.out", "w", stdout);
cin>>n>>m>>k;
tim = time(0);
//cout<<"Main part: "<<n<<" "<<m<<" "<<k<<endl;
for(int i=0; i<k; i++)
	{
	cin>>h>>w;
//	cout<<"Entered: "<<h<<" "<<w<<endl;
	int l=100,r=-1,t=100,b=-1;

	for(int j=0; j<h; j++)
	for(int ii=0; ii<w; ii++)
		{
		cin>>temp[j][ii];
//		cout<<temp[j][ii]<<" detected"<<endl;
		if(temp[j][ii]!='.')
			{
			if(j<t) t=j;
			if(j>b) b=j;
			if(ii<l) l=ii;
			if(ii>r) r=ii;
			}
		}
// 	cout<<"Left: "<<l<<endl;
//	cout<<"Right: "<<r<<endl;
// 	cout<<"Top: "<<t<<endl;
// 	cout<<"Bottom: "<<b<<endl;
	h = siz[i][0].x = siz[i][1].y = siz[i][2].x = siz[i][3].y = b-t+1;
	w = siz[i][0].y = siz[i][1].x = siz[i][2].y = siz[i][3].x = r-l+1;
//	cout<<"Height: "<<h<<endl;
//	cout<<"Width: "<<w<<endl;
	for(int j=t; j<=b; j++)
	for(int ii=l; ii<=r; ii++)
		{
		polys[i][0][j-t][ii-l] = temp[j][ii];
		polys[i][1][ii-l][h-j+t-1] = temp[j][ii];
		polys[i][2][h-j+t-1][w-ii+l-1] = temp[j][ii];
		polys[i][3][w-ii+l-1][j-t] = temp[j][ii];
		}
	for(int c=0; c<4; c++)
		{
		int tmp  =0;
		while(polys[i][c][0][tmp]=='.') tmp++;
		num[i][c] = tmp;
		}
/*          
	for(int c=0; c<4; c++)
		{
		for(int j=0; j<siz[i][c].x; j++)
			{
			for(int ii=0; ii<siz[i][c].y; ii++)
				cout<<polys[i][c][j][ii]<<" ";
			cout<<endl;
			}			
		cout<<"-----------------------------------------"<<endl;
		}
*/
	}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
	mas[i][j] = '.';
process();
cout<<kop.size()<<endl;
		
return 0;
}
