#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


char add_block[3][2][2][3];
int h,n;

int dfs(char block[20][2][3], int t=0){
  int ret = 0;

  if(t == n) return 0;
  
  for(int x=-1;x<2;x++){
    if(x==-1 && (add_block[t][0][0][0] == '#' || add_block[t][0][1][0] == '#')) continue;
    if(x==-1 && (add_block[t][1][0][0] == '#' || add_block[t][1][1][0] == '#')) continue;
    if(x==1  && (add_block[t][0][0][1] == '#' || add_block[t][0][1][1] == '#')) continue;
    if(x==1  && (add_block[t][1][0][1] == '#' || add_block[t][1][1][1] == '#')) continue;
    for(int y=-1;y<2;y++){
      if(y==-1 && (add_block[t][0][0][0] == '#' || add_block[t][0][0][1] == '#')) continue;
      if(y==-1 && (add_block[t][1][0][0] == '#' || add_block[t][1][0][1] == '#')) continue;
      if(y==1  && (add_block[t][0][1][0] == '#' || add_block[t][0][1][1] == '#')) continue;
      if(y==1  && (add_block[t][1][1][0] == '#' || add_block[t][1][1][1] == '#')) continue;

      char new_block[20][2][3];
      int this_ret = 0;
      
      for(int i=0;i<20;i++){
        for(int j=0;j<2;j++){
          for(int k=0;k<3;k++){
            new_block[i][j][k] = block[i][j][k];
          }
        }
      }

      int p_place = 18;
      
      for(int i=17;i>=0;i--){
        bool flag = true;
        
        for(int z=0;z<2;z++){
          
          for(int j=max(0,0-y);j<min(2,2-y);j++){
            for(int k=max(0,0-x);k<min(2,2-x);k++){
              if(new_block[i+z][j+y][k+x] == '#' && add_block[t][z][j][k] == '#'){
                flag = false;
              }
            }
          }
        }

        if(flag == false){
          break;
        }else{
          p_place = i;
        }
      }
      
      for(int i=0;i<2;i++){
        for(int j=max(0,0-y);j<min(2,2-y);j++){
          for(int k=max(0,0-x);k<min(2,2-x);k++){
            if(add_block[t][i][j][k] == '#')
              new_block[p_place+i][j+y][k+x] = '#';
          }
        }
      }

      for(int i=18;i>=0;i--){
        if(new_block[i][0][0] == '#' &&
           new_block[i][0][1] == '#' &&
           new_block[i][1][0] == '#' &&
           new_block[i][1][1] == '#'){

          this_ret ++;
          
          for(int j=i;j<19;j++){
            for(int p=0;p<2;p++){
              for(int q=0;q<2;q++){
                new_block[j][p][q] = new_block[j+1][p][q];
                new_block[j+1][p][q] = '.';
              }
            }
          }
        }
      }

      ret = max(ret,dfs(new_block,t+1)+ this_ret); 
    }
  }

  return ret;
}

bool solve(){
  char block[20][2][3];
  
  scanf("%d%d",&h,&n);
  
  if(n == 0) return false;

  for(int i=0;i<19;i++){
    for(int j=0;j<2;j++){
      block[i][j][0] = block[i][j][1] = '.';
      block[i][j][2] = '\0';
    }
  }
  
  for(int i=0;i<h;i++){
    for(int j=0;j<2;j++){
      scanf("%s",block[i][j]);
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<2;j++){
      for(int k=0;k<2;k++){
        scanf("%s",add_block[i][j][k]);
      }
    }

    if(add_block[i][0][0][0] == '.' &&
       add_block[i][0][0][1] == '.' &&
       add_block[i][0][1][0] == '.' &&
       add_block[i][0][1][1] == '.'){

      add_block[i][0][0][0] = add_block[i][1][0][0];
      add_block[i][0][0][1] = add_block[i][1][0][1];
      add_block[i][0][1][0] = add_block[i][1][1][0];
      add_block[i][0][1][1] = add_block[i][1][1][1];
      add_block[i][1][0][0] = '.';
      add_block[i][1][0][1] = '.';
      add_block[i][1][1][0] = '.';
      add_block[i][1][1][1] = '.';

    }
  }

  h = 16;
  
  printf("%d\n",dfs(block));
  
  return true;

}

int main(){

  while(solve());
  
  return 0;
}

Measured memory_bytes: 3132