#include #include #include using namespace std; struct node { int x,y,num,step; char mp[15][15]; bool friend operator<(node a,node b) { return a.step>b.step; } } t; int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}}; char s[15][15]; int vis[15][15][55]; int n,m,ex,ey,flag; int bfs(int x,int y) { priority_queueq; int i,j; for(i=0; i=0 && t.x=0 && t.y0) { vis[t.x][t.y][t.num]++; t.step++; t.num--; t.mp[t.x][t.y]='.'; q.push(t); } else if(t.mp[t.x][t.y]>='1' && t.mp[t.x][t.y]<='9') { vis[t.x][t.y][t.num]++; t.num+=t.mp[t.x][t.y]-'0'; t.mp[t.x][t.y]='.'; q.push(t); } } t=q.top(); } q.pop(); } return -1; } int main() { int i,j,x1,y1; while(~scanf("%d%d",&n,&m) && n) { for(i=0; i