#include using namespace std; int ice[22][22]; int w, h; namespace simulate { const int NONE = 0; const int BLOCK = 1; const int START = 2; const int GOAL = 3; const int WALL = 4; const int dr[] = {0, -1, 0, 1}; const int dc[] = {-1, 0, 1, 0}; int slide(int row, int col, int level) { int min = 11; if( level > 10 ) return min; for( int i = 0 ; i < 4 ; ++i ) { int r = row + dr[i]; int c = col + dc[i]; if( ice[r][c] != BLOCK ) { while( ice[r][c] == NONE ) { r += dr[i]; c += dc[i]; } if( ice[r][c] == GOAL ) return level; if( ice[r][c] == WALL ) continue; ice[r][c] = NONE; int result = slide(r - dr[i], c - dc[i], level + 1); min = (min > result) ? result : min; ice[r][c] = BLOCK; } } return min; } }; int main(void) { while( cin >> w >> h ) { if( (w | h) == 0 ) break; int startRow, startColumn; for( int i = 0 ; i < h + 2 ; ++i ) ice[i][0] = ice[i][w + 1] = simulate::WALL; for( int i = 0 ; i < w + 2 ; ++i ) ice[0][i] = ice[h + 1][i] = simulate::WALL; for( int i = 1 ; i <= h ; ++i ) { for( int j = 1 ; j <= w ; ++j ) { cin >> ice[i][j]; if( ice[i][j] == simulate::START ) { startRow = i; startColumn = j; ice[i][j] = simulate::NONE; } } } int result = simulate::slide(startRow, startColumn, 1); cout << (( result > 10 ) ? (-1) : result) << endl; } return 0; }