#include using namespace std; using VS = vector; using LL = long long; using VI = vector; using VVI = vector; using PII = pair; using PLL = pair; using VL = vector; using VVL = vector; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--) #define debug(x) cerr << #x << ": " << x << endl const int INF = 1e9; const LL LINF = 1e16; const LL MOD = 1000000007; const double PI = acos(-1.0); int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 }; /* ----- 2018/08/24 Problem: yukicoder 226 / Link: http://yukicoder.me/problems/no/226 ----- */ /* ------問題------ -----問題ここまで----- */ /* -----解説等----- DPかとおもったけどお絵かきしたらおーってなった ----解説ここまで---- */ templatevoid rotate2dR(vector>& a) { // rorateToR vector> res(a[0].size(), vector(a.size())); FOR(i, 0, (int)a.size())FOR(j, 0, (int)a[0].size())res[j][a.size() - i - 1] = a[i][j]; a = res; } LL decideTopGrid(const vector>& a) { LL ret = 1; // e int H = SZ(a), W = SZ(a[0]); // 2^W個ありえます FOR(j, 0, W) { int state = 0; FOR(b, 0, 2) { char top = '0' + b; if (isdigit(a[0][j]) && a[0][j] != top)continue; bool ok = 1; FOR(i, 1, H) { if (a[i][j] == '?')continue; if (i & 1)ok &= (a[i][j] != top); else ok &= (a[i][j] == top); } state += ok; } ret *= state; ret %= MOD; } return ret; } // [0,2] 市松模様の個数 LL countCheckeredPattern(const vector>& a) { int H = SZ(a), W = SZ(a[0]); LL ret = 0; FOR(b, 0, 2) { bool ok = 1; char top = '0' + b; if (a[0][0] == '?' || a[0][0] == top) { FOR(j, 0, W) { FOR(i, 0, H) { if (a[i][j] == '?')continue; if ((i^j) & 1) { ok &= (a[i][j] != top); } else { ok &= (a[i][j] == top); } } } ret += ok; } } return ret; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int H, W; cin >> H >> W; vector>a(H, vector(W)); FOR(i, 0, H) { FOR(j, 0, W) { cin >> a[i][j]; } } LL ans = decideTopGrid(a); rotate2dR(a); ans += decideTopGrid(a); ans %= MOD; ans -= countCheckeredPattern(a); ans += MOD; ans %= MOD; cout << ans << "\n"; return 0; }