#include <bits/stdc++.h>
using namespace std;
int bv[7][9] = {{},
                {4},
                {2, 6},
                {2, 4, 6},
                {0, 2, 6, 8},
                {0, 2, 4, 6, 8},
                {0, 2, 3, 5, 6, 8}};
int bh[7][9] = {{},
                {4},
                {0, 8},
                {0, 4, 8},
                {0, 2, 6, 8},
                {0, 2, 4, 6, 8},
                {0, 1, 2, 6, 7, 8}};
int cv[7] = {0};
int ch[7] = {0};
char s[2036][2036];
bool h[2036][2036];
bool v[2036][2036];
int MOD = 1000000007;
int f(int first, int second) {
  int res = 0;
  int sx = first * 4 + 1, sy = second * 4 + 1;
  int pos = 0;
  for (int i = (0); i < (int)(3); i++)
    for (int j = (0); j < (int)(3); j++) {
      if (s[sx + i][sy + j] == 'O') {
        res |= 1 << pos;
      }
      ++pos;
    }
  return res;
}
long long doub[536];
long long sing[536];
long long tmp[536];
int main() {
  for (int k = (0); k < (int)(7); k++) {
    for (int i = (0); i < (int)(k); i++) {
      cv[k] |= 1 << bv[k][i];
      ch[k] |= 1 << bh[k][i];
    }
  }
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (int)(n * 4); i++) scanf("%s", s[i]);
  memset(h, 0, sizeof(h));
  memset(v, 0, sizeof(v));
  for (int i = (0); i < (int)(n); i++)
    for (int j = (0); j < (int)(m); j++) {
      int cod = f(i, j);
      for (int k = (0); k < (int)(7); k++) {
        h[i][j] |= ch[k] == cod;
        v[i][j] |= cv[k] == cod;
      }
    }
  for (int j = (0); j < (int)(m); j++) {
    sing[j] = n % 2 == 0;
    for (int i = (0); i < (int)(n); i++) sing[j] &= v[i][j];
  }
  for (int j = (1); j < (int)(m); j++) {
    memset(tmp, 0, sizeof(tmp));
    for (int i = (0); i < (int)(n); i++) {
      if (i >= 1) {
        if (v[i][j - 1] && v[i][j] && v[i - 1][j - 1] && v[i - 1][j]) {
          tmp[i] += (i - 2 == -1 ? 1 : tmp[i - 2]);
        }
      }
      if (h[i][j - 1] && h[i][j]) {
        tmp[i] += (i - 1 == -1 ? 1 : tmp[i - 1]);
      }
      tmp[i] %= MOD;
    }
    doub[j] = tmp[n - 1];
    if (sing[j] && sing[j - 1]) {
      doub[j] = (doub[j] + MOD - 1) % MOD;
    }
  }
  memset(tmp, 0, sizeof(tmp));
  for (int j = (0); j < (int)(m); j++) {
    if (j >= 1) {
      if (doub[j]) {
        tmp[j] += doub[j] * (j == 1 ? 1 : tmp[j - 2]) % MOD;
        tmp[j] %= MOD;
      }
    }
    if (sing[j]) {
      tmp[j] += sing[j] * (j == 0 ? 1 : tmp[j - 1]) % MOD;
      tmp[j] %= MOD;
    }
  }
  printf("%d\n", (int)tmp[m - 1]);
  return 0;
}
