#include <iostream>
#include <algorithm>

using namespace std;

int w, h, ans;
int map[20][20];
int vx[4] = {-1,0,0,1};
int vy[4] = {0,-1,1,0};

int solve(int count, pair<int, int> pos){
	if(count > 10) return 999;
	for(int i=0; i<4; i++){
		if(pos.first+vy[i]<0 || h<=pos.first+vy[i] || pos.second+vx[i]<0 || w<=pos.second+vx[i]) continue;
		if(map[pos.first+vy[i]][pos.second+vx[i]] == 1) continue;
		pair<int, int> p = pos;
		while(1){
			p.first += vy[i];
			p.second += vx[i];
			//if(map[p.first][p.second] == 3)	return min(ans, count);
			if(p.first<0 || h<=p.first || p.second<0 || w<=p.second) break;
			else if(map[p.first][p.second] == 3) return count;
			else if(map[p.first][p.second] == 1){
				map[p.first][p.second] = 0;
				p.first -= vy[i];
				p.second -= vx[i];
				ans = min(ans, solve(count+1, p));
				map[p.first+vy[i]][p.second+vx[i]] = 1;
				break;
			}
		}
	}
	return ans;
}

int main(){
	while(1){
		cin >> w >> h;
		if(!w && !h) break;
		fill(map[0], map[20], 0);
		pair<int, int> pos;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cin >> map[i][j];
				if(map[i][j] == 2){
					pos = make_pair(i, j);
					map[i][j] = 0;
				} 
			}
		}
		ans = 999;
		ans = solve(1, pos);
		if(ans == 999) ans =-1;
		cout << ans << endl;
	}
	return 0;
}
