#include using namespace std; int N,M; int v[1210][1210]; char s[1210]; typedef long long LL; const int MOD = 201712111; int f0[1210][20]; int f[10][20][1<<17]; bool L[1210][20],U[1210][20]; inline int gg(int x){ return x==-1?1:x; } void upd(int &x){ if (x >= MOD) x-= MOD; } void print(int v) { for (int i=0;i<2;i++) { printf("%d",v%2); v/=2; } } int main() { scanf("%d%d",&N,&M); for (int i=1;i<=N;i++) { scanf("%s",s + 1); for (int j=1;j<=M;j++) if (s[j] == '?') v[i][j] = -1; else v[i][j] = s[j] - '0'; } if (N < M) { for (int i=1;i<=M;i++) for (int j=1;j<=i;j++) swap(v[i][j],v[j][i]); swap(N,M); } /* printf("%d %d\n",N,M); for (int i=1;i<=N;i++) { for (int j=1;j<=M;j++)printf("%d ",v[i][j]); puts(""); } puts("");*/ assert(M <= 17); memset(f0,60,sizeof(f0)); f0[1][1] = gg(v[1][1]); for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) { if (i>1) f0[i][j] = min(f0[i][j],f0[i-1][j] + gg(v[i][j])); if (j>1) f0[i][j] = min(f0[i][j],f0[i][j-1] + gg(v[i][j])); } printf("%d\n",f0[N][M]); for (int i=1;i<=N;i++) { for (int j=1;j<=M;j++) { L[i][j] = (f0[i][j] == f0[i][j-1] + gg(v[i][j])); U[i][j] = (f0[i][j] == f0[i-1][j] + gg(v[i][j])); //printf("%d",U[i][j]); } } int up = (1 << M); int ans = 0; memset(f,0,sizeof(f)); f[0][M][1] = 1; U[1][1] = true; int now = 1,last = 0; for (int i=1;i<=N;i++) { for (int k=0;k