#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
const long long MOD = 1000 * 1000 * 1000 + 7;
int n, m;
bool canV[MAXN][MAXN];
bool canH[MAXN][MAXN];
string vert[3][7] = {{"...", "...", "..O", "..O", "O.O", "O.O", "O.O"},
                     {"...", ".O.", "...", ".O.", "...", ".O.", "O.O"},
                     {"...", "...", "O..", "O..", "O.O", "O.O", "O.O"}};
string hor[3][7] = {
    {"...", "...", "O..", "O..", "O.O", "O.O", "OOO"},
    {"...", ".O.", "...", ".O.", "...", ".O.", "..."},
    {"...", "...", "..O", "..O", "O.O", "O.O", "OOO"},
};
void readRow(int row) {
  string empty;
  cin >> empty;
  string s[3];
  for (int i = 0; i < 3; ++i) cin >> s[i];
  for (int i = 0; i < m; ++i) {
    for (int digit = 0; digit <= 6; ++digit) {
      bool ok = true;
      for (int p = 0; p < 3; ++p)
        for (int q = 0; q < 3; ++q)
          if (vert[p][digit][q] != s[p][i * 4 + q + 1]) ok = false;
      if (ok) canV[row][i] = true;
    }
    for (int digit = 0; digit <= 6; ++digit) {
      bool ok = true;
      for (int p = 0; p < 3; ++p)
        for (int q = 0; q < 3; ++q)
          if (hor[p][digit][q] != s[p][i * 4 + q + 1]) ok = false;
      if (ok) canH[row][i] = true;
    }
  }
}
void init() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    readRow(i);
  }
}
long long ans[MAXN];
void solve() {
  for (int i = 0; i < MAXN; ++i) ans[i] = 0;
  ans[0] = 1;
  for (int col = 0; col < m; ++col) {
    bool can_all_v = (n % 2 == 0);
    for (int i = 0; i < n; ++i)
      if (!canV[i][col]) can_all_v = false;
    if (can_all_v) {
      ans[col + 1] = (ans[col + 1] + ans[col]) % MOD;
    }
    if (col == m - 1) continue;
    for (int i = 0; i < n; ++i)
      if (!canV[i][col + 1]) can_all_v = false;
    long long tmp[MAXN];
    for (int i = 0; i <= n; ++i) tmp[i] = 0;
    tmp[0] = 1;
    for (int i = 0; i < n; ++i) {
      if (canH[i][col] && canH[i][col + 1])
        tmp[i + 1] = (tmp[i + 1] + tmp[i]) % MOD;
      if (i == n - 1) continue;
      if (canV[i][col] && canV[i][col + 1] && canV[i + 1][col] &&
          canV[i + 1][col + 1])
        tmp[i + 2] = (tmp[i + 2] + tmp[i]) % MOD;
    }
    long long mn = tmp[n];
    if (can_all_v) {
      mn -= 1;
      if (mn < 0) mn += MOD;
    }
    ans[col + 2] = (ans[col + 2] + mn * ans[col]) % MOD;
  }
  cout << ans[m] << endl;
}
int main() {
  init();
  solve();
  return 0;
}
