package adv2017; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class N611 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int h = ni(), w = ni(); if(!(4L*h*w <= 1211))throw new AssertionError("4HW<=1211"); char[][] map = nm(h, w); for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ if(!(map[i][j] >= '1' && map[i][j] <= '9' || map[i][j] == '?'))throw new AssertionError("1~9 or ?"); } } if(w > h){ map = trans(map); int d = h; h = w; w = d; } int[][] ds = makeds(map); out.println(ds[h-1][w-1]); int[][] rds = makeds(rot(rot(map))); boolean[][] shortest = new boolean[h][w]; for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ shortest[i][j] = ds[i][j] + rds[h-1-i][w-1-j] == ds[h-1][w-1] + d1(map[i][j]); } } int mod = 201712111; long[] dp = new long[1< 0 && k<<~0<0 && ds[i][j-1] + d1(map[i][j]) == ds[i][j] || i > 0 && k<<~(w-1)<0 && ds[i-1][j] + d1(map[i][j]) == ds[i][j]) && shortest[i][j]){ // left or up is shortest and self is shortest { int nk = k<<1&(1<= mod)ndp[nk] -= mod; } if(map[i][j] == '?'){ int nk = k<<1&(1<= 0){ d = Math.min(d, ds[i-1][j]); } if(j-1 >= 0){ d = Math.min(d, ds[i][j-1]); } ds[i][j] = d + d1(map[i][j]); } } } return ds; } int d1(char c) { if(c == '?')return 1; return c-'0'; } public static char[][] rot(char[][] a) { int n = a.length, m = a[0].length; char[][] b = new char[m][n]; for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ b[j][n-1-i] = a[i][j]; } } return b; } public static char[][] trans(char[][] a) { if(a.length == 0)return new char[0][0]; int n = a.length, m = a[0].length; char[][] ret = new char[m][n]; for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++){ ret[i][j] = a[j][i]; } } return ret; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new N611().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }