#include using namespace std; int w, h; static const int EMPTY = 0; static const int WALL = 1; static const int START = 2; static const int GOAL = 3; static const int inf = 9999; int table[24][24]; int goal_x; int goal_y; int start_x; int start_y; int min_cnt; void rec(int x, int y, int cnt){ if (cnt >= 10) return; int xx, yy; for (int i = -1; i <= 1; ++i){ for (int j = -1; j <= 1; ++j){ int xx = x; int yy = y; if (i == 0 and j == 0) continue; if (i != 0 and j != 0) continue; int xxx = xx + i; int yyy = yy + j; if (xxx < 0 || w <= xxx || yyy < 0 || h <= yyy || table[xxx][yyy] == WALL){ continue; } for ( ; ; ){ xx += i; yy += j; if (xx < 0 || w <= xx || yy < 0 || h <= yy) break; if (table[xx][yy] == GOAL){ min_cnt = min(min_cnt, cnt+1); return; } if (table[xx][yy] == WALL){ table[xx][yy] = EMPTY; rec(xx-i, yy-j, cnt+1); table[xx][yy] = WALL; break; } } } } } int main(){ while (cin >> w >> h && (w and h)){ for (int i = 0; i < h; ++i){ for (int j = 0; j < w; ++j){ cin >> table[j][i]; if (table[j][i] == START){ start_x = j; start_y = i; table[j][i] = EMPTY; } } } min_cnt = inf; rec(start_x, start_y, 0); int result = min_cnt; if (result < inf){ cout << result << endl; }else{ cout << -1 << endl; } } }