#include <bits/stdc++.h>
using namespace std;
int N, M, a[250][250], mod = 1000000007, DP[250][250][2];
string s[4 * 250 + 1];
map<string, int> mp;
int ok(int v1, int v2, int o) {
  if (v1 == 0) {
    if (v2 == 0)
      return 1;
    else
      return v2 == o;
  } else {
    if (v2 == 0)
      return v1 == o;
    else
      return v1 == v2 && v2 == o;
  }
}
int check(int c, int r) {
  for (int i = 0; i + 1 <= r; i += 2)
    if (!ok(a[i][c], a[i + 1][c], 1)) return 0;
  return 1 && (r & 1);
}
int go(int i, int j, int b) {
  if (j == M) return 1;
  if (i == N) return go(0, j + (b ? 2 : 1), 0);
  if (DP[i][j][b] != -1) return DP[i][j][b];
  int ret = 0;
  if (j + 1 < M && ok(a[i][j], a[i][j + 1], -1) &&
      (b == 1 || check(j + 1, i - 1)))
    ret = (ret + go(i + 1, j, 1)) % mod;
  if (i + 1 < N && ok(a[i][j], a[i + 1][j], 1) &&
      (b == 0 || ok(a[i][j + 1], a[i + 1][j + 1], 1)))
    ret = (ret + go(i + 2, j, b)) % mod;
  return DP[i][j][b] = ret;
}
int main() {
  mp["........."] = 0;
  mp["....O...."] = 0;
  mp["O.O...O.O"] = 0;
  mp["O.O.O.O.O"] = 0;
  mp["..O...O.."] = 1;
  mp["..O.O.O.."] = 1;
  mp["O.OO.OO.O"] = 1;
  mp["O.......O"] = -1;
  mp["O...O...O"] = -1;
  mp["OOO...OOO"] = -1;
  cin >> N >> M;
  for (int i = 0; i < 4 * N + 1; i++) cin >> s[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      string rep = "";
      for (int x = 0; x < 3; x++)
        for (int y = 0; y < 3; y++) rep += s[4 * i + 1 + x][4 * j + 1 + y];
      a[i][j] = mp[rep];
    }
  memset(DP, -1, sizeof(DP));
  cout << go(0, 0, 0) << endl;
  return 0;
}
