#include <bits/stdc++.h>
using namespace std;
string dom_h[7] = {".........", "....O....", "O.......O", "O...O...O",
                   "O.O...O.O", "O.O.O.O.O", "OOO...OOO"};
string dom_v[7] = {".........", "....O....", "..O...O..", "..O.O.O..",
                   "O.O...O.O", "O.O.O.O.O", "O.OO.OO.O"};
set<string> domino[2];
vector<string> field;
long long res[251];
long long dp[251][251];
int v[250];
int n, m;
string cell(int y, int x) {
  string res;
  y = 1 + y * 4, x = 1 + x * 4;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) res = res + field[y + i][x + j];
  return res;
}
long long count(int col, int lev) {
  if (lev == n) return dp[col][lev] = 1;
  if (dp[col][lev] != -1) return dp[col][lev];
  long long res = 0;
  string c1 = cell(lev, col), c2 = cell(lev, col + 1);
  if (domino[0].count(c1) && domino[0].count(c2))
    res = count(col, lev + 1) % 1000000007;
  if (lev + 1 < n) {
    string c3 = cell(lev + 1, col), c4 = cell(lev + 1, col + 1);
    if (domino[1].count(c1) && domino[1].count(c2) && domino[1].count(c3) &&
        domino[1].count(c4))
      res = (res + count(col, lev + 2)) % 1000000007;
  }
  return dp[col][lev] = res;
}
int main() {
  int i, j;
  cin >> n >> m;
  string s;
  for (i = 0; i < 4 * n + 1; i++) {
    cin >> s;
    field.push_back(s);
  }
  domino[0].insert(dom_h, dom_h + 7);
  domino[1].insert(dom_v, dom_v + 7);
  memset(dp, 255, sizeof(dp));
  for (i = 0; i + 1 < m; i++) count(i, 0);
  if (n % 2 == 0) {
    for (j = 0; j < m; j++) {
      v[j] = 1;
      for (i = 0; i + 1 < n; i++) {
        string c1 = cell(i, j), c2 = cell(i + 1, j);
        v[j] &= (domino[1].count(c1) && domino[1].count(c2)) ? 1 : 0;
      }
    }
  }
  res[0] = 1, res[1] = v[0];
  for (i = 2; i <= m; i++)
    res[i] = (res[i - 2] * dp[i - 2][0] + v[i - 1] * res[i - 1] -
              v[i - 1] * v[i - 2] * res[i - 2]) %
             1000000007;
  cout << res[m] << endl;
  return 0;
}
