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

int get_y(vector<vector<string>>& block, vector<vector<int>>& top) {
    int res = 0;
    for(int h=0; h<2; ++h) {
        for(int i=0; i<2; ++i) {
            for(int j=0; j<2; ++j) {
                if(block[h][i][j] == '#') {
                    res = max(res, top[i][j] + (h == 0));
                }
            }
        }
    }
    return res;
}

int solve(int id, vector<vector<vector<string>>> const& blocks, vector<vector<string>> now) {
    if(id == blocks.size()) {
        return 0;
    }
    vector<vector<int>> top(2, vector<int>(2));
    for(int h=0; h<20; ++h) {
        for(int i=0; i<2; ++i) {
            for(int j=0; j<2; ++j) {
                if(now[h][i][j] == '#') {
                    top[i][j] = h;
                }
            }
        }
    }
    auto block = blocks[id];
    int res = 0;
    for(int dx=-1; dx<=1; ++dx) {
        for(int dy=-1; dy<=1; ++dy) {
            vector<vector<string>> b(2, vector<string>(2, ".."));
            bool ok = true;
            for(int i=0; i<2; ++i) {
                for(int j=0; j<2; ++j) {
                    for(int k=0; k<2; ++k) {
                        if(block[i][j][k] == '.') {
                            continue;
                        }
                        int ny = j + dy;
                        int nx = k + dx;
                        if(ny < 0 || 2 <= ny || nx < 0 || 2 <= nx) {
                            ok = false;
                            continue;
                        }
                        b[i][ny][nx] = '#';
                    }
                }
            }
            if(!ok) {
                continue;
            }
            int h = get_y(b, top);
            auto next = now;
            for(int k=0; k<2; ++k) {
                for(int i=0; i<2; ++i) {
                    for(int j=0; j<2; ++j) {
                        if(b[k][i][j] == '.') {
                            continue;
                        }
                        next[h+k][i][j] = b[k][i][j];
                    }
                }
            }
            int t = 0;
            vector<int> shift(20, 0);
            for(int i=h; i+1<20; ++i) {
                bool erase = true;
                for(int j=0; j<2; ++j) {
                    for(int k=0; k<2; ++k) {
                        erase &= next[i][j][k] == '#';
                    }
                }
                if(erase) {
                    shift[i+1]--;
                    t++;
                }
                shift[i] += shift[i-1];
            }
            for(int i=0; i<20; ++i) {
                next[i + shift[i]] = next[i];
                if(shift[i] != 0) {
                    next[i] = vector<string>(2, "..");
                }
            }
            res = max(res, solve(id+1, blocks, next) + t);
        }
    }
    return res;
}

int main() {
    int H, N;
    while(cin >> H >> N, H) {
        vector<vector<string>> v(20, vector<string>(2, ".."));
        for(int i=0; i<H; ++i) {
            vector<string> t(2);
            for(int j=0; j<2; ++j) {
                cin >> t[j];
            }
            v[i+1] = t;
        }

        vector<vector<vector<string>>> block(N);
        for(int i=0; i<N; ++i) {
            vector<vector<string>> b(2, vector<string>(2));
            for(int i=0; i<2; ++i) {
                for(int j=0; j<2; ++j) {
                    cin >> b[i][j];
                }
            }
            block[i] = b;
        }
        cout << solve(0, block, v) << endl;
    }
}


Measured memory_bytes: 3144