#include <stdio.h>
#include <string.h>

int run(char map[20][20],int w,int h,int x,int y,int n) {
	static const int dxy[4][2]={
		{0,1},{1,0},{0,-1},{-1,0}
	};
	int d;
	int res=10000;
	if(n>=10)return res;
	for(d=0;d<4;d++) {
		int nx=x,ny=y;
		nx+=dxy[d][0];
		ny+=dxy[d][1];
		if(nx<0 || nx>=w || ny<0 || ny>=h || map[ny][nx]==1)continue;
		for(;;) {
			if(map[ny][nx]==3)return 1;
			nx+=dxy[d][0];
			ny+=dxy[d][1];
			if(nx<0 || nx>=w || ny<0 || ny>=h)break;
			if(map[ny][nx]==1) {
				int nowres;
				map[ny][nx]=0;
				nowres=run(map,w,h,nx-dxy[d][0],ny-dxy[d][1],n+1)+1;
				if(nowres<res)res=nowres;
				map[ny][nx]=1;
				break;
			}
		}
	}
	return res>100 && n==0?-1:res;
}

int main(void) {
	int w,h;
	while(scanf("%d%d",&w,&h)==2 && (w|h)!=0) {
		int i,j;
		int sx,sy;
		char map[20][20];
		for(i=0;i<h;i++) {
			for(j=0;j<w;j++) {
				int buf;
				if(scanf("%d",&buf)!=1)return 1;
				map[i][j]=(char)buf;
				if(buf==2) {
					map[i][j]=0;
					sx=j;
					sy=i;
				}
			}
		}
		printf("%d\n",run(map,w,h,sx,sy,0));
	}
	return 0;
}