require 'set'

class Set
  def move(dx, dy, dz)
    each do |a|
      a[0] += dx
      a[1] += dy
      a[2] += dz
    end
  end

  def invalid?(other = nil)
    if other
      return true if any? {|a|
        # other.include?(a)  Bug?
        other.to_a.include?(a)
      }
    end
    any? {|x, y, z| x < 0 || x > 1 || y < 0 || y > 1 || z < 0 }
  end

  def cleanup
    k = 0
    g = group_by(&:last)
    g.keys.sort.each do |i|
      if g[i].size == 4
        k += 1
        g[i].each do |a|
          delete(a)
        end
      else
        g[i].each do |a|
          a[2] -= k
        end
      end
    end
    k
  end

  def height
    empty? ? 0 : (map(&:last).max + 1)
  end
end

loop do
  initial_h, n = gets.split.map(&:to_i)
  break if n == 0

  initial_block = Set.new
  initial_h.times do |k|
    (0..1).each do |i|
      s = gets
      (0..1).each do |j|
        initial_block << [i, j, k] if s[j] == ?#
      end
    end
  end

  new_blocks = []
  n.times do
    new_block = Set.new
    2.times do |k|
      (0..1).each do |i|
        s = gets
        (0..1).each do |j|
          new_block << [i, j, k] if s[j] == ?#
        end
      end
    end
    new_blocks << new_block
  end

  diffs = [-1, 0, 1].repeated_permutation(2).to_a
  best = 0
  (0..8).to_a.repeated_permutation(n) do |pattern|
    block = Marshal.load(Marshal.dump(initial_block))
    h = initial_h
    cleared = 0

    (0..n-1).each do |i|
      new_block = Marshal.load(Marshal.dump(new_blocks[i]))
      new_block.move(0, 0, h)
      new_block.move(*diffs[pattern[i]], 0)
      if new_block.invalid?
        cleared = 0
        break
      end

      new_block.move(0, 0, h)
      until new_block.invalid?(block)
        new_block.move(0, 0, -1) 
      end
      new_block.move(0, 0, 1)

      block += new_block
      cleared += block.cleanup
      h = block.height
    end

    best = cleared if cleared > best
  end

  p best
end

