#include<iostream>

#define rep(i,n) for(int i=0; i<n; i++)

int w,h;
int f[20][20];
int ans;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};

void rec(int n,int x, int y){

	if(n > 10)return;

	rep(i,4){
		int mx = x + dx[i];
		int my = y + dy[i];

		if(f[my][mx] == 1)continue;
		if(0>mx || mx>=w)continue;
		if(0>my || my>=h)continue;

		while(1){
			if(f[my][mx] == 3){
				if(n < ans)ans = n;
				return;
			}

			mx += dx[i], my += dy[i];
			if(0>mx || mx>=w)break;
			if(0>my || my>=h)break;

			if(f[my][mx] == 1){
				f[my][mx]=0;
				rec(n+1,mx-dx[i],my-dy[i]);
				f[my][mx]=1;
				break;
			}
		}
	}
}

int main(){

	int sx,sy;

	while(std::cin >> w >> h, (w||h)){

		rep(i,h){
			rep(j,w){
				std::cin >> f[i][j];
				if(f[i][j] == 2){
					sx = j, sy = i;
				}
			}
		}
		ans = 11;
		rec(1,sx,sy);
		if(ans > 10)ans = -1;
		std::cout << ans << std::endl;
	}
	return 0;
}