


fun main(args: Array<String>) {
    val shift = (-1 .. 1).flatMap{x -> (-1 .. 1).map{y -> Block(x, y)}}
    loop {
        val (h, n) = readLine()!!.trim().split(' ').map(String::toInt)
        if (h == 0 && n == 0) return
        val state = List(h){readBlock()}
        val blocks = List(n){List(2){readBlock()}.filter{it.isNotEmpty()}}
        val results = blocks.fold(listOf(state)) { states, block ->
            shift.map { db ->
                block.map { it.map(db::plus) }
            }.filter { it.all{it.all{it.x in 0..1 && it.y in 0..1 }}}.flatMap{b -> states.map{it.dropBlock(b)}}
        }
        println(((state.sumBy { it.size }) + blocks.sumBy { it.sumBy { it.size } } - results.map{it.sumBy { it.size }}.min()!!) shr 2)
    }
}
inline fun loop(func: (loopCount: Int) -> Unit): Unit {
    var count = 0
    while (true) {
        func(count++)
    }
}
data class Block(val x: Int, val y: Int) {
    operator fun plus(that: Block): Block = Block(x + that.x, y + that.y)
}
fun readBlock(): List<Block> {
    val first = readLine()!!.trim()
    val second = readLine()!!.trim()
    return listOf(first, second).withIndex().flatMap { (x, line) ->
        line.withIndex().mapNotNull{(y, char) ->
            when(char) {
                '#' -> Block(x, y)
                else -> null
            }
        }
    }
}

fun List<List<Block>>.dropBlock(blocks: List<List<Block>>): List<List<Block>> {
    for (h in indices.reversed()) {
        if (blocks.indices.any{i -> i + h in indices && blocks[i].any{it in this[i + h]}}) {
            return merge(blocks, h + 1)
        }
    }
    return merge(blocks, 0)
}
fun List<List<Block>>.merge(blocks: List<List<Block>>, bottom: Int): List<List<Block>> {
    val result = toMutableList()
    for (i in blocks.indices) {
        if (i + bottom in result.indices) {
            result[i + bottom] += blocks[i]
        }else {
            result.add(i + bottom, blocks[i])
        }
    }
    return result.filterNot{ it.size == 4 }
}
