import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum MO = 201_712_111L; enum INF = 10^^9; int H, W; string[] A; void main() { try { for (; ; ) { H = readInt(); W = readInt(); A = new string[H]; foreach (x; 0 .. H) { A[x] = readToken(); } if (H < W) { auto AT = A; swap(H, W); A = new string[H]; foreach (x; 0 .. H) { foreach (y; 0 .. W) { A[x] ~= AT[y][x]; } } } debug { writefln("H = %s, W = %s, A = %s", H, W, A); } assert(W <= 17); auto dist = new int[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { dist[x][y] = INF; if (x == 0 && y == 0) { dist[x][y] = 0; } if (x > 0) { chmin(dist[x][y], dist[x - 1][y]); } if (y > 0) { chmin(dist[x][y], dist[x][y - 1]); } dist[x][y] += (A[x][y] == '?') ? 1 : (A[x][y] - '0'); } debug { writeln(" dist = ", dist); } auto dp = new long[1 << W]; dp[1 << 0] = 1; foreach (x; 0 .. H) foreach (y; 0 .. W) { if (x == 0 && y == 0) { continue; } auto dpNext = new long[1 << W]; foreach (p; 0 .. 1 << W) { int c = INF; if (x > 0 && ((p >> y) & 1)) { chmin(c, dist[x - 1][y]); } if (y > 0 && ((p >> (y - 1)) & 1)) { chmin(c, dist[x][y - 1]); } if (A[x][y] == '?') { if (dist[x][y] == c + 1) { dpNext[p & ~(1 << y)] += dp[p] * 8; dpNext[p | (1 << y)] += dp[p]; } else { dpNext[p & ~(1 << y)] += dp[p] * 9; } } else { if (dist[x][y] == c + (A[x][y] - '0')) { dpNext[p | (1 << y)] += dp[p]; } else { dpNext[p & ~(1 << y)] += dp[p]; } } } dpNext[] %= MO; dp = dpNext; } long ans; foreach (p; 0 .. 1 << W) { if ((p >> (W - 1)) & 1) { ans += dp[p]; } } ans %= MO; writeln(dist[H - 1][W - 1]); writeln(ans); } } catch (EOFException e) { } }