#include<bits/stdc++.h>
using namespace std;

using pint=pair<int,int>;
const int SIZE=16;

void print_tower(vector<int> &tower)
{
		for(int i=0;i<SIZE;i++) cout<<i<<' '<<(bitset<4>)tower[i]<<endl;
		cout<<endl;
}
void print_block(pint b)
{
		cout<<(bitset<4>)b.first<<' '<<(bitset<4>)b.second<<endl;
}

/*
vector<pint> make_cand(pint b)
{
	vector<pint> res;
	res.push_back(b);
	return res;
}
*/

vector<pint> make_cand(pint b)
{
                vector<pint> res;

                int block=b.first|b.second;
                int cnt=__builtin_popcount(block);
                if(cnt>=3){ res.push_back(b); return res; }
                if(cnt==2 and (block==6 or block==9)){ res.push_back(b); return res; }

                int x=__builtin_popcount(b.first);
                int y=__builtin_popcount(b.second);
                if(cnt==1){
                        for(int i=0;i<4;i++){
                                pint tmp;
                                if(x==1) tmp.first|=1<<i;
                                if(y==1) tmp.second|=1<<i;
                                res.push_back(tmp);
                        }
                }else{
                        assert(block==3 or block==5 or block==10 or block==12);
                        res.push_back(b);
                        if(block==3){
                                int s=b.first<<2;
                                int t=b.second<<2;
                                res.push_back({s,t});
                        }
                        if(block==12){
                                int s=b.first>>2;
                                int t=b.second>>2;
                                res.push_back({s,t});
                        }
                        if(block==5){
                                int s=b.first<<1;
                                int t=b.second<<1;
                                res.push_back({s,t});
                        }
                        if(block==10){
                                int s=b.first>>1;
                                int t=b.second>>1;
                                res.push_back({s,t});
                        }
                }
                return res;
}

void solve(int H,int N)
{
		vector<int> tower(SIZE+1);
		for(int i=0;i<H;i++){
				for(int j=0;j<4;j++){
						char c; cin>>c;
						int t=(c=='#');
						tower[i]|=t<<j;
				}
		}
		vector<pint> b(N);
		for(int i=0;i<N;i++){
				for(int j=0;j<4;j++){
						char c; cin>>c;
						int t=(c=='#');
						b[i].first|=t<<j;
				}
				for(int j=0;j<4;j++){
						char c; cin>>c;
						int t=(c=='#');
						b[i].second|=t<<j;
				}
				if(b[i].first==0) b[i].first=b[i].second,b[i].second=0;
		}
		// print_tower(tower);
		// for(int i=0;i<N;i++) print_block(b[i]);

		int ans=0;
		function<void(int,int)> rec=[&](int pos,int cnt)
		{
				if(pos==N){ ans=max(ans,cnt); return; }
				vector<pint> cand=make_cand(b[pos]);
				vector<int> tmp_tower=tower; 
				// for(auto I:cand){ cout<<(bitset<4>)I.first<<' '<<(bitset<4>)I.second<<endl; }
				for(auto I:cand){
					int bottom=SIZE;
					while(bottom>0){
						if((I.first&tower[bottom-1])==0 and (I.second&tower[bottom])==0) bottom--;
						else break;
					}
					// print_block(b[i]);
					// cout<<bottom<<endl;
					tower[bottom]|=I.first;
					tower[bottom+1]|=I.second;
					// cout<<"before"<<endl; cout<<pos<<' '<<cnt<<endl;
					// print_tower(tower);
					if(tower[bottom]==15 and tower[bottom+1]==15){
						tower[bottom]=tower[bottom+1]=0;
						for(int j=bottom+1;j<SIZE;j++) tower[j]=tower[j+1];
						// cout<<"after"<<endl; print_tower(tower);
						rec(pos+1,cnt+2);
						tower=tmp_tower;

					}else if(tower[bottom]==15){
						tower[bottom]=0;
						for(int j=bottom;j<SIZE;j++) tower[j]=tower[j+1];
						// cout<<"after"<<endl; print_tower(tower);
						rec(pos+1,cnt+1);
						tower=tmp_tower;

					}else if(tower[bottom+1]==15){
						tower[bottom+1]=0;
						for(int j=bottom+1;j<SIZE;j++) tower[j]=tower[j+1];
						// cout<<"after"<<endl; print_tower(tower);
						rec(pos+1,cnt+1);
						tower=tmp_tower;
					}else{
						rec(pos+1,cnt);
						// cout<<"after"<<endl; print_tower(tower);
						tower=tmp_tower;
					}
				}
		};
		rec(0,0);
		cout<<ans<<endl;
}

int main()
{
	while(true){
			int H,N; cin>>H>>N;
			if(H==0 and N==0) break;
			solve(H,N);
	}
	return 0;
}



Measured memory_bytes: 3120