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

using i64 = int64_t;

using Block = vector<vector<char>>;

int dx[9] = {0,-2,-1,1,3,2,1,-1,-3};

void print_field(const vector<vector<char>> &v){
    cout << "***field***" << endl;
    for(int i=v.size()-1;i>=0;--i){
        for(int j=0;j<v[i].size();++j)cout << v[i][j];
        cout << endl;
    }
    cout << "******" << endl;
}

void print_blocks(const vector<Block> &v){
    cout << "***blocks***" << endl;
    for(int i=0;i<v.size();++i){
        for(int j=1;j>=0;--j){
            for(int k=0;k<4;++k)cout << v[i][j][k];
            cout << endl;
        }
        cout << "---" << endl;
    }
}

void drop_blocks(int m, vector<vector<char>> &v){
    for(int i=m+1;i<v.size();++i){
        for(int j=0;j<4;++j){
            if(v[i-1][j] == '.'){
                v[i-1][j] = v[i][j];
                v[i][j] = '.';
            }
        }
    }
}

int erase_blocks(vector<vector<char>> &v){
    int res = 0;
    for(int i=0;i<v.size();++i){
        bool flag = true;
        for(int j=0;j<4;++j){
            if(v[i][j] == '.'){
                flag = false;
                break;
            }
        }
        if(flag){
            for(int j=0;j<4;++j){
                v[i][j] = '.';
            }
            res++;
            drop_blocks(i, v);
            i--;
        }
    }
    return res;
}

void set_block(int h, vector<vector<char>> &v, Block block){
    bool flag = false;
    for(int i=h;i>=0;--i){
        for(int j=0;j<2;++j){
            for(int k=0;k<4;++k){
                if(block[j][k] == '#' && v[i+j][k] == '#'){
                    flag = true;
                    break;
                }
            }
            if(flag)break;
        }
        if(flag){
            for(int j=1;j<=2;++j){
                for(int k=0;k<4;++k){
                    if(v[i+j][k] == '#' || block[j-1][k] == '#')v[i+j][k] = '#';
                    else v[i+j][k] = '.';
                }
            }
            break;
        }
    }
    if(flag)return;
    bool one = false;
    for(int i=0;i<4;++i){
        if(block[0][i] == '#'){
            one = true;
            break;
        }
    }
    if(one){
        for(int i=0;i<2;++i){
            for(int j=0;j<4;++j){
                if(v[i][j] == '#' || block[i][j] == '#')v[i][j] = '#';
                else v[i][j] = '.';
            }
        }
    }else{
        for(int i=0;i<4;++i){
            if(v[0][i] == '#' || block[1][i] == '#')v[0][i] = '#';
            else v[0][i] = '.';
        }
    }
}

int simulate(int h, const vector<vector<char>> &v, vector<Block> blocks){
    int res = 0;
    auto field = v;
    //print_field(field);
    //print_blocks(blocks);
    for(int i=0;i<blocks.size();++i){
        set_block(h, field, blocks[i]);
        res += erase_blocks(field);
        //print_field(field);
        //cout << "res:" << res << endl;
    }
    return res;
}

int solve(int i, vector<Block> blocks, const int n, const int h, const vector<vector<char>> &v, const vector<Block> &blocks_orig){
    if(i == n){
        return simulate(h, v, blocks);
    }
    Block cur_block = blocks_orig[i];
    int res = 0;
    for(int d=0;d<9;++d){
        Block block(2, vector<char>(4, '.'));
        bool ok = true;
        for(int j=0;j<2;++j){
            for(int k=0;k<4;++k){
                if(cur_block[j][k] == '.')continue;
                if(k+dx[d] < 0 || k+dx[d] > 3 || d == 2 && k != 2 || d == 6 && k != 1 || d == 3 && k == 1 || d == 7 && k == 2){
                    ok = false;
                    break;
                }
                block[j][k+dx[d]] = cur_block[j][k];
            }
            if(!ok)break;
        }
        if(!ok)continue;
        blocks.push_back(block);
        int tmp = solve(i+1, blocks, n, h, v, blocks_orig);
        res = max(res, tmp);
        blocks.pop_back();
    }
    return res;
}

int main(){
    int h, n;
    while(cin >> h >> n, h > 0){
        vector<vector<char>> v(h+16, vector<char>());
        for(int i=0;i<h;++i){
            for(int j=0;j<4;++j){
                char c;
                cin >> c;
                v[i].push_back(c);
            }
        }
        for(int i=h;i<h+16;++i){
            for(int j=0;j<4;++j){
                v[i].push_back('.');
            }
        }
        vector<Block> blocks;
        for(int i=0;i<n;++i){
            Block block(2, vector<char>());
            for(int j=0;j<2;++j){
                for(int k=0;k<4;++k){
                    char b;
                    cin >> b;
                    block[j].push_back(b);
                }
            }
            blocks.push_back(block);
        }
        cout << solve(0, {}, n, h+8, v, blocks) << endl;
    }

    return 0;
}



Measured memory_bytes: 3172