#include #include #include #include using namespace std; constexpr int mod = 201712111; struct Modint { int n; Modint(int n = 0) : n(n) {} }; Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - mod : a.n); } Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + mod : a.n); } Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % mod); } Modint &operator+=(Modint &a, Modint b) { return a = a + b; } Modint &operator-=(Modint &a, Modint b) { return a = a - b; } Modint &operator*=(Modint &a, Modint b) { return a = a * b; } Modint dp[303][1 << 17]; char g[1000][1000]; int main() { int h, w; cin >> h >> w; for (int i = 0; i < h; i++) { scanf("%s", g[i]); } if (h < w) { for (int i = 0; i < h; i++) { for (int j = i; j < w; j++) { swap(g[i][j], g[j][i]); } } swap(h, w); } vector> dist(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int val = g[i][j] == '?' ? 1 : g[i][j] - '0'; if (i == 0 && j == 0) { dist[i][j] = val; } else if (i == 0) { dist[i][j] = dist[i][j - 1] + val; } else if (j == 0) { dist[i][j] = dist[i - 1][j] + val; } else { dist[i][j] = min(dist[i - 1][j], dist[i][j - 1]) + val; } } } cout << dist[h - 1][w - 1] << endl; dp[1][0] = 1; for (int i = 1; i < h * w; i++) { for (int j = 0; j < 1 << w; j++) { int y = i / w; int x = i % w; // a // b c if (y == 0) { if (j >> x - 1 & 1) { dp[i + 1][j | 1 << x] += dp[i][j]; } else { dp[i + 1][j] += dp[i][j]; } // change if (g[y][x] == '?') { dp[i + 1][j | 1 << x] += dp[i][j] * 8; } } else if (x == 0) { dp[i + 1][j] += dp[i][j]; // change if (g[y][x] == '?') { dp[i + 1][j | 1 << x] += dp[i][j] * 8; } } else { int val = g[y][x] == '?' ? 1 : g[y][x] - '0'; int a = dist[y - 1][x] + (j >> x & 1); int b = dist[y][x - 1] + (j >> x - 1 & 1); if (dist[y][x] < min(a, b) + val) { dp[i + 1][j | 1 << x] += dp[i][j]; } else { dp[i + 1][j & ~(1 << x)] += dp[i][j]; } // change if (g[y][x] == '?') { dp[i + 1][j | 1 << x] += dp[i][j] * 8; } } } } Modint ans = 0; for (int i = 0; i < 1 << w; i++) { if (~i >> (w - 1) & 1) { ans += dp[h*w][i]; } } cout << ans.n << endl; }