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 PYTHON3):

Solution:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    e = LI()
    d = defaultdict(int)
    for i in e:
        d[i] += 1
    for i in d.values():
        if i != 2:
            print("no")
            break
    else:
        print("yes")
    return

#B
def B():
    n = I()
    a = LI()
    a.sort()
    ans = -float("inf")
    for c in range(n):
        for d in range(c):
            m = a[c]-a[d]
            for i in range(n)[::-1]:
                if i != c and i != d:
                    e = i
                    break
            for i in range(e)[::-1]:
                if i != c and i != d:
                    b = i
                    break
            ans = max(ans, (a[e]+a[b])/m)
    print(ans)
    return

#C
def C():
    def gcd(a,b):
        if a == 0:
            return b
        return gcd(b%a, a)

    s = input()
    n = len(s)
    if s.count("(") == 0:
        s = float(s)
        b = 10**(n-2)
        a = round(s*b)
        g = gcd(a,b)
        a //= g
        b //= g
    else:
        n = s.find("(")
        t = float(s[:n])
        b = 10**(n-2)
        a = round(t*b)
        g = gcd(a,b)
        a //= g
        b //= g
        l = (s.find("(")-s.find(".")-1)
        s = s[n+1:-1]
        m = len(s)
        c = round(float(s))
        d = (10**m-1)*10**l
        g = gcd(c,d)
        c //= g
        d //= g
        a = a*d+b*c
        b = b*d
        g = gcd(a,b)
        a //= g
        b //= g
    print(str(a)+"/"+str(b))
    return

#D
def D():
    return

#E
def E():
    def dfs(d,k,s):
        if d == n:
            ans.append(k)
        else:
            b = [block[d][i] for i in range(8)]
            if sum(b[:4]) == 0:
                b = b[4:]+[0,0,0,0]
            dfs(d+1,*check(b,k,s))
            if not (b[0]|b[1]|b[4]|b[5]):
                b_ = [b[2],b[3],0,0,b[6],b[7],0,0]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[1]|b[3]|b[5]|b[7]):
                b_ = [0,b[0],0,b[2],0,b[4],0,b[6]]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[2]|b[3]|b[6]|b[7]):
                b_ = [0,0,b[0],b[1],0,0,b[4],b[5]]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[0]|b[2]|b[4]|b[6]):
                b_ = [b[1],0,b[3],0,b[5],0,b[7],0]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[1]|b[2]|b[3]|b[5]|b[6]|b[7]):
                b_ = [0,0,0,b[0],0,0,0,b[4]]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[0]|b[2]|b[3]|b[4]|b[6]|b[7]):
                b_ = [0,0,b[1],0,0,0,b[5],0]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[0]|b[1]|b[3]|b[4]|b[5]|b[7]):
                b_ = [0,b[2],0,b[6],0,0,0,0]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
            if not (b[0]|b[1]|b[2]|b[4]|b[5]|b[6]):
                b_ = [b[3],0,0,0,b[7],0,0,0]
                if sum(b_):
                    dfs(d+1,*check(b_,k,s))
    def check(b,k,s):
        i = 19
        k_ = k
        s_ = [[s[i][j] for j in range(4)] for i in range(20)]
        while i >= 1 and not ((b[0]&s[i-1][0])|(b[1]&s[i-1][1])|(b[2]&s[i-1][2])|(b[3]&s[i-1][3])|(b[4]&s[i][0])|(b[5]&s[i][1])|(b[6]&s[i][2])|(b[7]&s[i][3])):
            i -= 1
        s_[i] = [s_[i][j]+b[j] for j in range(4)]
        s_[i+1] = [s_[i+1][j]+b[j+4] for j in range(4)]
        i = 0
        while i < 20:
            if sum(s_[i]) == 4:
                k_ += 1
                for j in range(i,19):
                    s_[j] = [s_[j+1][l] for l in range(4)]
                s_[19] = [0,0,0,0]
                i -= 1
            i += 1
        return k_,s_

    while 1:
        h,n = LI()
        if h == n == 0:
            break
        s = [[0,0,0,0] for i in range(20)]
        for i in range(h):
            c = S()
            c += S()
            s[i] = [1 if c[j] == "#" else 0 for j in range(4)]
        block = [[0,0,0,0,0,0,0,0] for i in range(n)]
        for i in range(n):
            b = S()
            b += S()
            b += S()
            b += S()
            block[i] = [1 if b[j] == "#" else 0 for j in range(8)]
        ans = []
        dfs(0,0,s)
        print(max(ans))
    return

#F
def F():
    def dfs(d,k):
        if d == len(s):
            t.append(k)
        else:
            if s[d] == ".":
                for i in range(2):
                    dfs(d+1,k+[str(i)])
            else:
                dfs(d+1,k+[s[d]])

    def parse_expr(s,i):
        if f[s[i]]:
            n,i = parse_num(s,i)
            if i < len(s) and s[i] in "+-*":
                op,i = parse_op(s,i)
                e2,i = parse_expr(s,i)
                return calc(op,n,e2),i
            else:
                return n,i
        else:
            if s[i] == "(":
                i += 1
                e1,i = parse_expr(s,i)
                op,i = parse_op(s,i)
                e2,i = parse_expr(s,i)
                i += 1
            else:
                e1,i = parse_expr(s,i)
                op,i = parse_op(s,i)
                e2,i = parse_expr(s,i)
            return calc(op,e1,e2),i


    def parse_num(s,i):
        n = int(s[i])
        i += 1
        while i < len(s) and f[s[i]]:
            n *= 2
            n += int(s[i])
            i += 1
        return n,i

    def parse_op(s,i):
        return s[i],i+1

    def calc(op,e1,e2):
        if op == "+":
            return e1+e2
        elif op == "-":
            return e1-e2
        else:
            return e1*e2

    s = S()
    f = defaultdict(lambda : 0)
    for i in range(2):
        f[str(i)] += 1
    t = []
    dfs(0,[])
    for s in t:
        print(s)
        print(parse_expr(s,0))
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    E()