#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>

using Block = std::vector<std::vector<std::vector<int>>>;

const std::vector<std::vector<int>> empface{{0, 0}, {0, 0}};

Block input(int h) {
    Block ret(h);
    for (auto& f : ret) {
        f.resize(2);
        for (auto& v : f) {
            v.resize(2);
            for (auto& x : v) {
                char c;
                std::cin >> c;
                x = (c == '#');
            }
        }
    }
    return ret;
}

int fall(Block& board) {
    int h = board.size();

    while (true) {
        bool ok = true;

        for (int z = 0; z < h; ++z) {
            for (int x = 0; x < 2; ++x) {
                for (int y = 0; y < 2; ++y) {
                    if (board[z][x][y] != 2) continue;
                    if (z == 0 || board[z - 1][x][y] == 1) ok = false;
                }
            }
        }

        if (!ok) break;

        for (int z = 1; z < h; ++z) {
            for (int x = 0; x < 2; ++x) {
                for (int y = 0; y < 2; ++y) {
                    if (board[z][x][y] != 2) continue;
                    board[z - 1][x][y] = 2;
                    board[z][x][y] = 0;
                }
            }
        }
    }

    int score = 0;
    for (int z = 0; z < h; ++z) {
        while (true) {
            int cnt = 0;
            for (int x = 0; x < 2; ++x) {
                for (int y = 0; y < 2; ++y) {
                    board[z][x][y] = (board[z][x][y] > 0);
                    cnt += board[z][x][y];
                }
            }
            if (cnt != 4) break;

            ++score;

            for (int nz = z; nz + 1 < h; ++nz) {
                board[nz] = board[nz + 1];
            }
            board.back() = empface;
        }
    }

    return score;
}

bool solve() {
    int h, n;
    std::cin >> h >> n;
    if (h == 0) return false;

    auto sboard = input(h);
    for (int i = 0; i < 10; ++i) sboard.push_back(empface);
    h = sboard.size();

    std::vector<Block> drops(n);
    for (auto& drop : drops) drop = input(2);

    std::function<int(const Block&, int)>
        dfs = [&](const Block& board, int d) {
            if (d == n) return 0;

            int ret = 0;
            const auto& drop = drops[d];

            for (int dx = -1; dx <= 1; ++dx) {
                for (int dy = -1; dy <= 1; ++dy) {
                    auto nboard = board;

                    bool valid = true;
                    for (int z = 0; z < 2; ++z) {
                        for (int x = 0; x < 2; ++x) {
                            for (int y = 0; y < 2; ++y) {
                                if (drop[z][x][y] == 0) continue;

                                int nx = x + dx, ny = y + dy,
                                    nz = h - 2 + z;
                                if (nx < 0 || 2 <= nx ||
                                    ny < 0 || 2 <= ny) {
                                    valid = false;
                                    continue;
                                }

                                nboard[nz][nx][ny] = 2;
                            }
                        }
                    }
                    if (!valid) continue;

                    int score = fall(nboard);
                    score += dfs(nboard, d + 1);

                    ret = std::max(ret, score);
                }
            }
            return ret;
        };

    std::cout << dfs(sboard, 0) << std::endl;

    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while (solve()) {}

    return 0;
}



Measured memory_bytes: 3136