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

const int HEIGHT=20;
using Plane=array<array<int,2>,2>;
using Block=array<Plane,2>;
using Tower=array<Plane,HEIGHT>;
Plane newPlane(){
    Plane res;
    for(int i=0;i<2;i++)for(int j=0;j<2;j++) res[i][j]=0;
    return res;
}
Block newBlock(){
    Block res;
    res[0]=newPlane(); res[1]=newPlane();
    return res;
}
Tower newTower(){
    Tower res;
    for(int i=0;i<HEIGHT;i++) res[i]=newPlane();
    return res;
}
Block normalize(Block b){
    bool emp=true;
    for(int i=0;i<2;i++)for(int j=0;j<2;j++) emp=emp&&(!b[0][i][j]);
    if(emp) b[0]=b[1],b[1]=newPlane();
    return b;
}
bool isRange(int x,int y){
    return 0<=x && x<2 && 0<=y && y<2;
}
Block slide(Block b,int dx,int dy){
    Block res=newBlock();
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            for(int k=0;k<2;k++){
                if(b[i][j][k]){
                    if(!isRange(j+dx,k+dy)) throw "Range";
                    res[i][j+dx][k+dy]=true;
                }
            }
        }
    }
    return res;
}
vector<Block> generate(Block b){
    b=normalize(b);
    vector<Block> res;
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            try{
                res.push_back(slide(b,dx,dy));
            }catch(const char* msg){}
        }
    }
    return res;
}
int flesh(Tower &tower){
    for(int i=0;i<HEIGHT;i++){
        bool isFlesh=true;
        for(int j=0;j<2;j++){
            for(int k=0;k<2;k++){
                isFlesh=isFlesh && tower[i][j][k];
            }
        }
        if(isFlesh){
            for(int h=i;h+1<HEIGHT;h++) tower[h]=tower[h+1];
            return 1+flesh(tower);
        }
    }
    return 0;
}
void drop(Tower &tower,Block b){
    int base=[&](){
        for(int res=HEIGHT-2;res>=0;res--){
            for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                    for(int k=0;k<2;k++){
                        if(tower[res+i][j][k] && b[i][j][k]){
                            return res+1;
                        }
                    }
                }
            }
        }
        return 0;
    }();
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            for(int k=0;k<2;k++){
                tower[base+i][j][k]|=b[i][j][k];
            }
        }
    }
}
Plane inputPlane(){
    Plane res=newPlane();
    for(int i=0;i<2;i++){
        string s;
        cin>>s;
        for(int j=0;j<2;j++){
            res[i][j]=(s[j]=='#');
        }
    }
    return res;
}
int solve(int h,int n){
    Tower tower=newTower();
    for(int i=0;i<h;i++){
        tower[i]=inputPlane();
    }
    vector<Block> b(n,newBlock());
    for(int i=0;i<n;i++){
        for(int j=0;j<2;j++) b[i][j]=inputPlane();
    }
    function<int(int,Tower)> dfs=[&b,n,&dfs](int idx,Tower tower){
        if(idx==n) return 0;
        auto bs=generate(b[idx]);
        int res=0;
        for(auto x:bs){
            Tower nextTower=tower;
            drop(nextTower,x);
            int cnt=flesh(nextTower);
            res=max(res,cnt+dfs(idx+1,nextTower));
        }
        return res;
    };
    return dfs(0,tower);
}
int main(){
    int n,h;
    while(cin>>h>>n,n){
        cout<<solve(h,n)<<endl;
    }
    return 0;
}
