#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
using namespace std;

int w, h;
int minStep;

void solve(vector<vector<int> >& board, int sy, int sx, int step)
{
	if(step + 1 >= minStep)
		return;

	int dy[] = {0, 1, 0, -1};
	int dx[] = {1, 0, -1, 0};
	int answer = -1;

	for(int i=0; i<4; ++i){
		if(board[sy+dy[i]][sx+dx[i]] == 1)
			continue;
		int y = sy;
		int x = sx;
		while(board[y+dy[i]][x+dx[i]] == 0){
			y += dy[i];
			x += dx[i];
		}
		if(board[y+dy[i]][x+dx[i]] == 3){
			minStep = step + 1;
			break;
		}
		if(board[y+dy[i]][x+dx[i]] == 1){
			board[y+dy[i]][x+dx[i]] = 0;
			solve(board, y, x, step+1);
			board[y+dy[i]][x+dx[i]] = 1;
		}
	}
}

int main()
{
	for(;;){
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;

		vector<vector<int> > board(h+2, vector<int>(w+2, -1));
		int sy, sx;
		for(int i=1; i<=h; ++i){
			for(int j=1; j<=w; ++j){
				cin >> board[i][j];
				if(board[i][j] == 2){
					sy = i;
					sx = j;
					board[i][j] = 0;
				}
			}
		}
		minStep = 11;
		solve(board, sy, sx, 0);
		if(minStep == 11)
			cout << -1 << endl;
		else
			cout << minStep << endl;
	}

	return 0;
}