/*
   All this dirty code written by avm.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <string>
using namespace std;
#define FILE_NAME "covering"
const int MAX_W = 9;
const int MAX_H = 9;
char s[256];
int h,w,piece;
int *mask[3],*buf[3];
int full_row,w3;
int TestNo;
char a[3][3];

//const int HASH_SIZE = 12582893;//60Mb
const int inf = 0x3fffffff;
const int HASH_SIZE = 3355439;//16Mb
int hash[HASH_SIZE];
unsigned char e[HASH_SIZE];


#ifdef D00
void print(int a)
{
for(int j=2;j>=0;j--)
  {
  int i;
  for(i=0;i<w;i++) if (a & (1 << (j*w+i))) putchar('X');else putchar('.');
  putchar('\n');
  }

}
#endif

int f(int j,int a)
//j - cur row
//a - 3 row bitset
{
#ifdef D00
print(a);
printf("j=%d\n",j);
#endif
if (j >= h) return 0;
if ((a & full_row) == full_row) return f(j+1,a >> w);
int hc = a + (j << w3);
int hidx = hc % HASH_SIZE;
if (hash[hidx] == hc)
  {
  return e[hidx];
  }
int res = inf;
for(int i=0;i<w;i++)
  {
  int o = 1 << i;
  if (o & a) continue;
  //(j,i) - first uncovered cell
#ifdef D00
printf("i = %d\n",i);
#endif
  for(int y=0;y<3;y++)
    for(int x=i-2;x<=i;x++)
      {
      int b = a | mask[y][x];
      if (o & b)
        {
        if ((a | o) == b) continue;
        //cell is covered
        int v = f(j,b) + 1;
        if (res > v) res = v;
        }
      }
  if (res == inf) res = f(j,a | o) + 1;
  break;
  }

hash[hidx] = hc;
e[hidx] = res;
return res;
}
bool Test()
{
char *p;
int i,j,x,y,xcount,n,piece;
if (0 == fgets(s,sizeof(s),stdin)) return false;
assert(strlen(s) < sizeof(s) - 2);
p = strtok(s,"\t\n\r ");
assert(p != 0);
w = (int) strtol(p,&p,10);w3 = 3 * w;
assert(w >= 1 && w <= MAX_W);
p = strtok(0,"\t\n\r ");
h = (int) strtol(p,&p,10);
assert(h >= 1 && h <= MAX_H);
p = strtok(0,"\t\n\r ");
assert(p == 0);
#ifdef D00
fprintf(stderr,"%d %d\n",w,h);
#endif
piece = 0;
xcount = 0;
for(j=0;j<3;j++)
  {
  buf[j] = new int[4+w];
  mask[j] = buf[j] + 2;
  }
piece = 0;
for(j=2;j>=0;j--)
  {
  fgets(s,sizeof(s),stdin);
  assert(strlen(s) == 4);//3 + newline
  for(i=0;i<3;i++)
    {
    if (s[i] == 'X')
      {
      a[j][i] = 1;
      piece |= 1 << (j * w + i);
      xcount++;
      }
    else
      {
      assert(s[i] == '.');
      a[j][i] = 0;
      }
    }
  }
#ifdef D00
print(piece);
#endif
for(y=0;y<3;y++)
  {
  for(x=-2;x<w;x++)
    {
    mask[y][x] = 0;
    for(j=0;j<3;j++)
      {
      int Y = y - 2 + j;
      if (Y < 0) continue;
      assert(Y < 3);
      for(i=0;i<3;i++) if (a[j][i])
        {
        int X = x + i;
        if (X >= 0 && X < w) mask[y][x] |= 1 << (Y * w + X);
        }
      }
#ifdef D00
printf("y = %d x = %d\n",y,x);
print(mask[y][x]);
#endif

    }
  }
assert(xcount == 6);
full_row = (1 << w) - 1;
if (TestNo++) puts("");
memset(hash,0xff,sizeof(hash));
printf("%d\n",f(0,0));
for(j=0;j<3;j++) delete[] buf[j];
fgets(s,sizeof(s),stdin);
return true;
}
int main()
{
int t,nt;
freopen(FILE_NAME ".in","r",stdin);
freopen(FILE_NAME ".out","w",stdout);
TestNo = 0;
while(Test());

return 0;
}
