#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

//第9回課題
//Curling
//問題:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1144&lang=jp
// 中央＊前

int W,H;
int memox[4] = {1, -1, 0, 0};
int memoy[4] = {0, 0, 1, -1};
int map[50][50];
int ans=11;


void dfs(int x,int y,int count){
	if(count+1>=ans)return;
	
	rep(dir,4){
	    int nx=x+memox[dir];
	    int ny=y+memoy[dir];
	    if(map[ny][nx]==1)continue;
	    while(1){
	        if(nx < 0 || ny < 0 || nx >= W || ny >= H)break;
	        if(map[ny][nx]==3){
	            ans=count+1;
	            return;
	        }
	        if(map[ny][nx]==1){
	            map[ny][nx]=0;
	            dfs(nx-memox[dir],ny-memoy[dir],count+1);
	            //printf("%d %d %d!\n",nx-memox[dir],ny-memoy[dir],count+1);
	            map[ny][nx]=1;
	            break;
	        }
	        nx+=memox[dir];
	        ny+=memoy[dir];
	    }
	}
	
	return;
}

int main(){
	while(1){
	    scanf("%d %d",&W,&H);
	    if(W==0)break;
		int sx,sy;
		rep(y,H){
			rep(x,W){
				scanf("%d",&map[y][x]);
				if(map[y][x] == 2){sx=x;sy=y;map[y][x]=0;}
			}
		}
		ans = 11;
		dfs(sx,sy,0);	
		if(ans<=10)printf("%d\n",ans);
		else printf("-1\n");
	}
	return 0;
}

