#include <bits/stdc++.h>
using namespace std;
const long long N = 2000, M = 255, mod = 1e9 + 7;
char str[N][N];
map<string, long long> H, V;
string get(long long i, long long j) {
  long long x = 4 * (i - 1) + 1, y = 4 * (j - 1) + 1;
  string a;
  for (long long k = 1; k <= 3; k++)
    for (long long l = 1; l <= 3; l++) a += str[k + x][l + y];
  return a;
}
long long h[M][M], v[M][M], p[M][M], d[M][M], dp[M];
int32_t main() {
  H["........."] = 1;
  V["........."] = 1;
  H["....O...."] = 1;
  V["....O...."] = 1;
  H["O...O...O"] = 1;
  V["..O.O.O.."] = 1;
  H["O.O...O.O"] = 1;
  V["O.O...O.O"] = 1;
  H["O.O.O.O.O"] = 1;
  V["O.O.O.O.O"] = 1;
  V["O.OO.OO.O"] = 1;
  H["OOO...OOO"] = 1;
  H["O.......O"] = 1;
  V["..O...O.."] = 1;
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= 4 * n + 1; i++) cin >> str[i] + 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      h[i][j] = H[get(i, j)];
      v[i][j] = V[get(i, j)];
    }
  }
  for (long long i = 1; i <= m; i++) {
    p[n + 1][i] = 1;
    d[n + 1][i] = 1;
  }
  for (long long j = 1; j <= m; j++) {
    for (long long i = n; i >= 1; i--) {
      p[i][j] |= p[i + 2][j] & (v[i][j] && v[i + 1][j]);
    }
  }
  for (long long j = 1; j < m; j++) {
    for (long long i = n; i >= 1; i--) {
      d[i][j] = (h[i][j] && h[i][j + 1]) * d[i + 1][j] +
                (v[i][j] && v[i + 1][j] && v[i][j + 1] && v[i + 1][j + 1]) *
                    d[i + 2][j];
      d[i][j] %= mod;
    }
    if (p[1][j] && p[1][j + 1]) d[1][j] = (d[1][j] + mod - 1) % mod;
  }
  dp[m + 1] = 1;
  for (long long i = m; i >= 1; i--) {
    dp[i] = p[1][i] * dp[i + 1] + (d[1][i] * dp[i + 2]) % mod;
    dp[i] %= mod;
  }
  cout << dp[1] << endl;
}
