#include #include using namespace std; enum cell_t { EMPTY = 0, OBSTRACT, START, GOAL, }; int w, h; int sx, sy, gx, gy; int field[20][20]; int rec(int field[20][20], int step, int ix, int iy) { static int dx[4] = {0, 1, 0, -1}; static int dy[4] = {1, 0, -1, 0}; int min_step = 128; if (step > 10) { return min_step; } for (int i = 0; i < 4; i++) { int x = ix, y = iy; if (field[x+dx[i]][y+dy[i]] == OBSTRACT) { continue; } while (true) { if (x+dx[i] < 0 || y+dy[i] < 0 || x+dx[i] >= w || y+dy[i] >= h) { break; } if (field[x+dx[i]][y+dy[i]] == GOAL) { return step; } if (field[x+dx[i]][y+dy[i]] == OBSTRACT) { field[x+dx[i]][y+dy[i]] = EMPTY; min_step = min(min_step, rec(field, step+1, x, y)); field[x+dx[i]][y+dy[i]] = OBSTRACT; break; } x += dx[i]; y += dy[i]; } } return min_step; } int main() { while (true) { int min_step; cin >> w >> h; if (w == 0 && h == 0) { break; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> field[j][i]; if (field[j][i] == START) { sx = j; sy = i; field[j][i] = EMPTY; } } } min_step = rec(field, 1, sx, sy); if (min_step > 10) { cout << -1 << endl; } else { cout << min_step << endl; } } return 0; }