use std::io::*; use std::str::FromStr; use std::cmp::{min, max}; use std::rc::Rc; use std::mem::swap; fn read() -> T { let stdin = stdin(); let stdin_lock = stdin.lock(); let s = stdin_lock .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::(); s.parse::().ok().unwrap() } static DX: &'static [i32] = &[0, 0, 1, -1]; static DY: &'static [i32] = &[1, -1, 0, 0]; fn main() { let mut h: usize = read(); let mut w: usize = read(); let input: Vec> = (0..h).map(|_| read::().chars().map(|c| if c == '?' { -1 } else { c.to_digit(10).unwrap() as i32 }).collect()).collect(); let map = if h < w { let mut tmp = vec![vec![0 as i32; h]; w]; for i in 0..h { for j in 0..w { tmp[j][i] = input[i][j]; } } swap(&mut h, &mut w); tmp } else { input }; let mut dist = vec![vec![1010101010; w]; h]; dist[0][0] = 0; for i in 0..h { for j in 0..w { if i != 0 { dist[i][j] = min(dist[i][j], dist[i - 1][j]); } if j != 0 { dist[i][j] = min(dist[i][j], dist[i][j - 1]); } dist[i][j] += max(1, map[i][j]); } } println!("{}", dist[h - 1][w - 1]); let mut dp = vec![0 as u64; 1 << w]; dp[1] = 1; let m = 201712111u64; for i in 0..h { for j in 0..w { let mut dp2 = vec![0 as u64; 1 << w]; for k in 0..(1 << w) { let mut f = false; let c = if map[i][j] == -1 { 1 } else { map[i][j] }; if (k >> j) & 1 == 1 && (i == 0 || dist[i - 1][j] + c == dist[i][j]) { f = true; } if j != 0 && (k >> (j - 1)) & 1 == 1 && dist[i][j - 1] + c == dist[i][j] { f = true; } if f { dp2[k | (1 << j)] += dp[k]; dp2[k | (1 << j)] %= m; if map[i][j] == -1 { dp2[k & !(1 << j)] += dp[k] * 8; dp2[k & !(1 << j)] %= m; } } else { dp2[k & !(1 << j)] += dp[k] * if map[i][j] == -1 { 9 } else { 1 }; dp2[k & !(1 << j)] %= m; } } dp = dp2; } } let mut ans = 0u64; for i in 1 << (w - 1)..1 << w { ans += dp[i]; ans %= m; } println!("{}", ans); }