Problem: Falling Block Puzzle

Block drop

You are playing a falling block puzzle. As shown in the figure below, the field of this puzzle has a shape in which cubic cells are arranged in 2 squares x 2 squares in each row, and the rows are infinitely arranged on the top.

<image>

Each cell either has one block that fits snugly in the cell, or nothing. This puzzle proceeds as follows.

1. Some blocks are installed in the initial state.
2. Drop a block that fits in 2 squares x 2 squares x 2 squares from above. However, before dropping, the block can be translated horizontally so that the block does not stick out of the field.
3. When the bottom surface of one of the dropped blocks reaches the bottom of the block or field that has already been placed, all the blocks will stop falling and stop.
4. For each stage, if all 4 squares are filled, the blocks in that stage will disappear, and the blocks above it will fall one by one. Even if there are no blocks in the cell below each block after the fall, it will not fall any further.
5. Return to 2.



Since the blocks placed in the initial state and some lumps to be dropped are given, create a program to find out how many steps can be erased by dropping all the lumps in the order given.

Input

The input consists of 100 or less datasets. Each dataset has the following form.

> (Initial block height H) (Number of lumps to drop N)
> (1st stage in the initial state)
> ...
> (H stage in the initial state)
> (1st drop)
> ...
> (Nth drop)

The first line of each dataset specifies the initial block height H (1 ≤ H ≤ 10) and the number of lumps to drop N (1 ≤ N ≤ 3). Then, the information of each stage in the initial state is given in the following format.

> c11 c12
> c21 c22

cij represents the information of each cell,'#' indicates that a block exists, and'.' Indicates that there is no block. It can be assumed that there are no blocks in all cells or no blocks in all cells for each of the first to H rows. Then, the information of each drop mass is given in the following format.

> b111 b112
> b121 b122
> b211 b212
> b221 b222

As in the initial state format,'#' indicates that a block exists, and'.' Indicates that there is no block. Each block contains at least one block. The blocks contained in the block may be touched only by the corners and sides, and are not necessarily connected by the surface.

Refer to the figure below for the correspondence of the input subscripts of the initial state and block block.

<image>

The end of the input is indicated by a single line of two zeros.

The figure below shows the first data set in the Sample Input shown later. In this dataset, one step can be erased by translating a block of blocks diagonally and then dropping them.

<image>

Output

For each dataset, output on one line how many columns can be erased at the maximum.

Sample Input


1 1

..
..
..
..
..
1 1
. #
..
..
..
..
. #
twenty two

..

..
..
. #

..
..
..
..
..
13
..
..












10 3

..

..

. #

. #
..

..

. #

. #

. #
..
. #
..
..
. #
..
. #
..
..
..
..
..
..
..
..
10 3

. #

..

..
. #
..

. #
. #

. #

..


. #
..
. #
..
..
. #
..
..
..
..
..
..
..
..
. #
0 0

Output for Sample Input


1
0
3
6
6
0





Example

Input

1 1
##
#.
..
..
#.
..
1 1
.#
#.
#.
..
..
.#
2 2
##
#.
##
#.
..
.#
##
#.
#.
..
#.
..
1 3
#.
..
##
##
##
##
##
##
##
##
##
##
##
##
10 3
##
#.
##
#.
##
.#
##
.#
#.
##
#.
##
.#
##
.#
##
.#
#.
.#
#.
#.
.#
#.
.#
#.
..
#.
..
#.
..
#.
..
10 3
##
.#
##
..
##
#.
.#
..
##
.#
.#
##
.#
##
#.
##
##
.#
..
.#
#.
#.
.#
#.
#.
#.
#.
..
..
..
..
.#
0 0


Output

1
0
3
6
6
0

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AOJ2701 solver = new AOJ2701();
        solver.solve(1, in, out);
        out.close();
    }

    static class AOJ2701 {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            while (true) {
                int h = in.nextInt(), n = in.nextInt();
                int l = h + 30;
                if (h == 0 && n == 0) {
                    return;
                }
                AOJ2701.Plain[] board = new AOJ2701.Plain[l];
                for (int i = 0; i < l; i++) {
                    board[i] = new AOJ2701.Plain();
                }
                Arrays.fill(board[0].state[0], true);
                Arrays.fill(board[0].state[1], true);
                Arrays.fill(board[1].state[0], true);
                Arrays.fill(board[1].state[1], true);
                for (int i = 0; i < h; i++) {
                    board[i + 2].parse(in);
                }

            /*{
                System.out.println("INITIAL BORAD " + board[0].cnt());
                int idx = 0;
                while (board[idx].cnt() > 0) {
                    board[idx].print();
                    idx++;
                }
            }*/

                AOJ2701.Cube[] cubes = new AOJ2701.Cube[n];
                int[] cnt = new int[n];
                int m = 1;
                for (int i = 0; i < n; i++) {
                    cubes[i] = new AOJ2701.Cube();
                    cubes[i].parse(in);
                    cnt[i] = cubes[i].getCandidates().size();
                    m *= cnt[i];
                }

                int ans = 0;
                for (int i = 0; i < m; i++) {
                    //System.out.println("START m="+i);

                    int res = 0;

                    AOJ2701.Plain[] b = new AOJ2701.Plain[l];
                    for (int j = 0; j < l; j++) {
                        b[j] = board[j].myclone();
                    }

                    int t = i;
                    for (int j = 0; j < n; j++) {
                        int cand = t % cnt[j];
                        t /= cnt[j];

                        AOJ2701.Cube fall = cubes[j].getCandidates().get(cand);
                        int cur = l - 2;
                        while (!b[cur].collidesWith(fall, 0) && !b[cur + 1].collidesWith(fall, 1)) {
                            cur--;
                        }
                        cur++;

                        b[cur].merge(fall, 0);
                        b[cur + 1].merge(fall, 1);

                        if (cur + 1 >= 2 && b[cur + 1].cnt() == 4) {
                            res++;
                            for (int k = cur + 2; k < b.length; k++) {
                                b[k - 1] = b[k];
                            }
                            b[b.length - 1] = new AOJ2701.Plain();
                        }
                        if (cur >= 2 && b[cur].cnt() == 4) {
                            res++;
                            for (int k = cur + 1; k < b.length; k++) {
                                b[k - 1] = b[k];
                            }
                            b[b.length - 1] = new AOJ2701.Plain();
                        }
                    
                    /*
                    System.out.println("Board printing n="+j);
                    int idx = 0;
                    while (b[idx].cnt() > 0) {
                        b[idx].print();
                        idx++;
                    }*/
                    }
                    ans = Math.max(ans, res);
                }
                out.println(ans);
            }
        }

        private static class Plain {
            boolean[][] state = new boolean[2][2];

            void parse(Scanner in) {
                for (int j = 0; j < 2; j++) {
                    String s = in.next();
                    state[j][0] = s.charAt(0) == '#';
                    state[j][1] = s.charAt(1) == '#';
                }
            }

            boolean collidesWith(AOJ2701.Cube cube, int h) {
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        if (state[i][j] && cube.state[h][i][j]) {
                            return true;
                        }
                    }
                }
                return false;
            }

            void merge(AOJ2701.Cube cube, int h) {
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        if (cube.state[h][i][j]) {
                            state[i][j] = true;
                        }
                    }
                }
            }

            int cnt() {
                int c = 0;
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        if (state[i][j]) {
                            c++;
                        }
                    }
                }
                return c;
            }

            AOJ2701.Plain myclone() {
                AOJ2701.Plain clone = new AOJ2701.Plain();
                clone.state = new boolean[2][2];
                for (int i = 0; i < 2; i++) {
                    System.arraycopy(state[i], 0, clone.state[i], 0, 2);
                }
                return clone;
            }

        }

        private static class Cube {
            boolean[][][] state = new boolean[2][2][2];
            List<AOJ2701.Cube> candidates;

            void parse(Scanner in) {
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        String s = in.next();
                        state[i][j][0] = s.charAt(0) == '#';
                        state[i][j][1] = s.charAt(1) == '#';
                    }
                }
            }

            List<AOJ2701.Cube> getCandidates() {
                if (candidates != null) return candidates;
                List<AOJ2701.Cube> a = new ArrayList<>();
                for (int i = -1; i <= 1; i++) {
                    for (int j = -1; j <= 1; j++) {
                        try {
                            AOJ2701.Cube d = new AOJ2701.Cube();
                            for (int k = 0; k < 2; k++) {
                                for (int l = 0; l < 2; l++) {
                                    for (int m = 0; m < 2; m++) {
                                        if (state[k][l][m]) {
                                            d.state[k][l + i][m + j] = true;
                                        }
                                    }
                                }
                            }
                            a.add(d);
                        } catch (ArrayIndexOutOfBoundsException ex) {
                        }
                    }
                }
                return candidates = a;
            }

        }

    }
}