#include #include #include #include #include using namespace std; unsigned w,h; struct state{ unsigned x,y,n; unsigned map[20][20]; }; state s; unsigned c(){ int v[][2]={ {1,0}, {0,1}, {-1,0}, {0,-1} }; queueq; q.push(s); while(!q.empty()){ state s = q.front(); q.pop(); if(s.n == 11)continue; for(int i=0;i<4;++i){ unsigned tx = s.x,ty = s.y; for(;;){ if(ty+v[i][0] >= h || tx+v[i][1] >= w )break; unsigned&c = s.map[ty+v[i][0]][tx+v[i][1]]; if(c == 3)return s.n; if(c == 1){ if(tx == s.x && ty == s.y)break; s.n += 1; c = 0; unsigned ttx=s.x,tty=s.y; s.x = tx;s.y=ty; q.push(s); s.x = ttx;s.y=tty; c = 1; s.n -= 1; break; } ty += v[i][0]; tx += v[i][1]; } } } return -1; } int main(){ ifstream cin("./D1"); // ofstream cout("./od.txt"); while(cin>>w>>h,w|h){ for(int i=0;i> s.map[i][j]; if(s.map[i][j]==2) s.x = j,s.y=i,s.n=1; } cout << (int)c() << endl; } }