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

#define rep(i,x,y) for(int i=(x); i < (y); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " L:" << __LINE__ << ")" << std::endl;
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

using i64=int64_t;

/*
vector<vector<vector<char>>> move_right(vector<vector<vector<char>>> v){
	vector<vector<vector<char>>> res(2,vector<vector<char>>(2,vector<char>(2,'.')));
	if(v.empty()){
		res.clear();
		return res;
	}

	rep(h,0,2) rep(y,0,2) if(v[h][1][y]=='#'){
		res.clear();
		return res;
	}

	rep(h,0,2) rep(y,0,2) res[h][1][y]=v[h][0][y];
	return res;
}

vector<vector<vector<char>>> move_up(vector<vector<vector<char>>> v){
	vector<vector<vector<char>>> res(2,vector<vector<char>>(2,vector<char>(2,'.')));
	if(v.empty()){
		res.clear();
		return res;
	}

	rep(h,0,2) rep(x,0,2) if(v[h][x][1]=='#'){
		res.clear();
		return res;
	}

	rep(h,0,2) rep(x,0,2) res[h][x][1]=v[h][x][0];
	return res;
}
*/

vector<vector<vector<char>>> move(vector<vector<vector<char>>> v,int dx,int dy){
	vector<vector<vector<char>>> res(2,vector<vector<char>>(2,vector<char>(2,'.')));
	if(v.empty()){
		res.clear();
		return res;
	}

	rep(h,0,2){
		rep(x,0,2){
			rep(y,0,2){
				int x1=x+dx,y1=y+dy;
				if(x1<0 or 2<=x1 or y1<0 or 2<=y1){
					if(v[h][x][y]=='#'){
						res.clear();
						return res;
					} 
				}else res[h][x1][y1]=v[h][x][y];
			}
		}
	}
	return res;
}

bool ok(vector<vector<vector<char>>>& c,vector<vector<vector<char>>>& v,int h){
	if(h<=-2) return false;
	rep(hd,0,2) rep(x,0,2) rep(y,0,2){
		if(v[hd][x][y]=='.') continue;
		if(h+hd<0) return false;
		else if(c[h+hd][x][y]=='#') return false;
	}
	return true;
}

vector<vector<vector<char>>> merge(vector<vector<vector<char>>>& c,vector<vector<vector<char>>>& v,int h){
	vector<vector<vector<char>>> nc=c;
	rep(hd,0,2) rep(x,0,2) rep(y,0,2){
		//assert(nc[h+hd][x][y]=='.' or v[hd][x][y]=='.');
		if(v[hd][x][y]=='#') nc[h+hd][x][y]='#';
	}
	return nc;
}

void shift(vector<vector<vector<char>>> &c,int h){
	rep(i,h,63){
		rep(x,0,2){
			rep(y,0,2){
				c[i][x][y]=c[i+1][x][y];
			}
		}
	}
	rep(x,0,2) rep(y,0,2) c[63][x][y]='.';
}

pair<int,vector<vector<vector<char>>>> erase(vector<vector<vector<char>>> &c){
	int cnt=0;
	auto nc=c;
	int top=-1;
	for(int h=63; h>=0; --h) rep(x,0,2) rep(y,0,2) if(nc[h][x][y]=='#'){
		top=h;
		goto A;
	}
	A:
	print(top);
	for(int h=top; h>=0; --h){
		bool f=true;
		rep(x,0,2) rep(y,0,2) if(nc[h][x][y]=='.'){
			f=false;
			break;
		}
		if(f){
			shift(nc,h);
			++cnt;
		}
	}
	return make_pair(cnt,nc);
}

void solve(int H,int N){
	vector<vector<vector<char>>> c(64,vector<vector<char>>(2,vector<char>(2,'.')));
	rep(h,0,H) rep(x,0,2) rep(y,0,2) cin >> c[h][x][y];
	vector<vector<vector<vector<char>>>> b(N,vector<vector<vector<char>>>(2,vector<vector<char>>(2,vector<char>(2,'.'))));
	rep(i,0,N){
		rep(h,0,2) rep(x,0,2) rep(y,0,2) cin >> b[i][h][x][y];
	}

	function<int(vector<vector<vector<char>>>&,int)> dfs=[&](vector<vector<vector<char>>>& c,int i){
		if(i==N) return 0;
		int res=0;
		rep(dx,-1,2){
			rep(dy,-1,2){
				auto nb=move(b[i],dx,dy);
				if(!nb.empty()){
					print(dx);
					print(dy);
					int h=32;
					assert(ok(c,nb,h));
					while(ok(c,nb,h)) --h;
					print(h);
					++h;
					assert(ok(c,nb,h));
					auto nc=merge(c,nb,h);
					/*
					rep(i,0,4){
						rep(x,0,2){
							rep(y,0,2){
								cerr << nc[i][x][y];
							}
							cerr << endl;
						}
						cerr << endl;
					}
					*/
					auto p=erase(nc);
					print(p.first);	
					/*
					rep(i,0,4){
						rep(x,0,2){
							rep(y,0,2){
								cerr << p.second[i][x][y];
							}
							cerr << endl;
						}
						cerr << endl;
					}
					*/
					res=max(res,p.first+dfs(p.second,i+1));
				}			
			}
		}
		/*
		{
			auto nb=b[i];
			if(!nb.empty()){
				int h=32;
				while(ok(c,nb,h)) --h;
				++h;
				auto nc=merge(c,nb,h);
				auto p=erase(nc);
				res=max(res,p.first+dfs(p.second,i+1));
			}	
		}
		{
			auto nb=move_right(b[i]);
			if(!nb.empty()){
				int h=32;
				while(ok(c,nb,h)) --h;
				++h;
				auto nc=merge(c,nb,h);
				auto p=erase(nc);
				res=max(res,p.first+dfs(p.second,i+1));
			}
		}
		{
			auto nb=move_up(b[i]);
			if(!nb.empty()){
				int h=32;
				while(ok(c,nb,h)) --h;
				++h;
				auto nc=merge(c,nb,h);
				auto p=erase(nc);
				res=max(res,p.first+dfs(p.second,i+1));
			}
		}
		{
			auto nb=move_up(move_right(b[i]));
			if(!nb.empty()){
				int h=32;
				while(ok(c,nb,h)) --h;
				++h;
				auto nc=merge(c,nb,h);
				auto p=erase(nc);
				res=max(res,p.first+dfs(p.second,i+1));
			}
		}
		*/
		return res;
	};

	cout << dfs(c,0) << endl;
}

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(16);
	for(;;){
		int H,N;
		cin >> H >> N;
		if(H==0 and N==0) break;
		solve(H,N);
	}
	return 0;
}

Measured memory_bytes: 3196