#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll H, N;
ll ans = 0;

bool field[20][2][2];
bool Input[20][2][2];
bool b[3][2][2][2];
vector<int> v;

bool Delete() {
    for(int i = 0; i < 20; i++) {
        bool Occupy = true;
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                if(!field[i][j][k]) Occupy = false;
            }
        }
        if(Occupy) {
            //cerr << "Delete!: " << i << endl;
            for(int j = 0; j < 2; j++) {
                for(int k = 0; k < 2; k++) {
                    field[i][j][k] = false;
                }
            }
            for(int j = i + 1; j < 20; j++) {
                swap(field[j], field[j-1]);
            }
            return true;
        }
    }
    return false;
}

void print() {
    return;
    cerr << "-----------------" << endl;
    for(int i = 0; i <= 10; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                cerr << field[i][j][k];
            }
            cerr << endl;
        }
        cerr << endl;
    }
    cerr << "----------------" << endl;
}

void Put(int height, int idx, int pos) {
    int dh = (pos / 3) - 1;
    int dw = (pos % 3) - 1;
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                if(!b[idx][i][j][k]) continue;
                int newj = j + dh;
                int newk = k + dw;
                assert(!field[height+i][newj][newk]);
                field[height+i][newj][newk] = true;
            }
        }
    }
}

bool canput(int height, int idx, int pos) {
    int dh = (pos / 3) - 1;
    int dw = (pos % 3) - 1;
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                if(!b[idx][i][j][k]) continue;
                int newj = j + dh;
                int newk = k + dw;
                if(height + i < 0 or field[height+i][newj][newk]) {
                    //cerr << height << " " << idx << " " << dh << " " << dw << " " << i << " " << j << " " << k << endl;
                    return false;
                }
            }
        }
    }
    return true;
}

void f() {
    for(int i = 0; i < 20; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                field[i][j][k] = Input[i][j][k];
            }
        }
    }
    for(int i = 0; i < N; i++) {
        int dh = (v[i] / 3) - 1;
        int dw = (v[i] % 3) - 1;
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                for(int l = 0; l < 2; l++) {
                    if(!b[i][j][k][l]) continue;
                    if(k + dh < 0 or k + dh > 1) return;
                    if(l + dw < 0 or l + dw > 1) return;
                }
            }
        }
    }
    /*
    cerr << "f: ";
    for(auto val : v) {
        cerr << (val / 3) - 1 << " " << (val % 3) - 1 << " ";
    }
    cerr << endl;
    */
    ll now = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 18; j >= -3; j--) {
            if(!canput(j, i, v[i])) {
                Put(j + 1, i, v[i]);
                //cerr << "Put: " << j + 1 << endl;
                break;
            }
        }
        while(true) {
            print();
            if(!Delete()) break;
            now++;
        }
    }
    ans = max(ans, now);
}

void dfs(int idx) {
    if(idx == v.size()) {
        f();
        return;
    }
    for(int i = 0; i < 9; i++) {
        v[idx] = i;
        dfs(idx + 1);
    }
}

void solve() {
    ans = 0;
    for(int i = 0; i < 20; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                Input[i][j][k] = false;
            }
        }
    }
    for(int h = 0; h < H; h++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                char tmp;
                cin >> tmp;
                if(tmp == '#') Input[h][j][k] = true;
            }
        }
    }
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                for(int l = 0; l < 2; l++) {
                    char tmp;
                    cin >> tmp;
                    if(tmp == '#') b[i][j][k][l] = true;
                    else b[i][j][k][l] = false;
                }
            }
        }
    }
    v.resize(N);
    dfs(0);
    cout << ans << endl;
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> H >> N) {
        if(H == 0 and N == 0) break;
        solve();
    }
    return 0;
}


Measured memory_bytes: 3184