#include <iostream>
using namespace std;
#define INF (1<<28)

int ans = 0;
int cur = 0;
int mp[22][22];

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

int dfs(int x,int y,int lim){
	if( cur >= ans )return 0;
	
	if( lim <= 0 ) return 0;
	
	for(int i = 0 ; i < 4 ; i++){
		int X = x+dx[i] , Y = y+dy[i];
		if( mp[Y][X] == 1 ) continue;
		int f = 1;
		while(1){
			if(mp[Y][X] == -1){ f=false;break; }
			else if(mp[Y][X] == 2 || mp[Y][X] == 0){ X+=dx[i],Y+=dy[i]; }
			else if(mp[Y][X] == 1){ X-=dx[i],Y-=dy[i];break;  }
			else if(mp[Y][X] == 3){
				if( ans > cur+1 ){
					ans = cur+1;
				}
				return 0;
			}
		}
		if( f ){
			cur++;
			mp[Y+dy[i]][X+dx[i]] = 0;
			dfs(X,Y,lim-1);
			cur--;
			mp[Y+dy[i]][X+dx[i]] = 1;
		}
	}
	return 0;
}

int main(){
	int w,h;
	while(cin >> w >> h && w){
		for(int i = 0 ; i < 22 ; i++)
			for(int j = 0 ; j < 22 ; j++)
				mp[i][j] = -1;
		int sx , sy;
		for(int i = 0 ; i < h ; i++){
			for(int j = 0 ; j < w ; j++){
				cin >> mp[i+1][j+1];
				if( mp[i+1][j+1] == 2 ){
					sx = j+1;
					sy = i+1;
				}
			}
		}
		
		ans = INF;
		dfs(sx,sy,10);
		
		cout << (ans==INF?-1:ans) << endl;
	}
}