#include <stdio.h>

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

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

int w,h;
int field[20][20];
int ans,sx,sy;

void dfs(int x, int y, int cnt){
  int tx,ty,i,j,k;
  int *p;
  if( cnt>=ans ) return;
  
#if DEB
  printf("(%d,%d)  cnt:%d\n",x,y,cnt);
  rep(i,h){
    rep(j,w){
      printf("%d ",field[i][j]);
    }
    puts("");
  }
#endif

  rep(k,4){
    tx = x;
    ty = y;
    if( tx<0 || ty<0 || tx>=w || ty>=h ||
	field[ty+dy[k]][tx+dx[k]]==1 )continue;
    for(;;){
      tx += dx[k];
      ty += dy[k];
      if( tx<0 || ty<0 || tx>=w || ty>=h ) break;
      p = &field[ty][tx];
      if( *p==1 ){
	*p = 0;
	dfs(tx-dx[k], ty-dy[k], cnt+1);
	*p = 1;
	break;
      }else if( *p==3 ){
	ans = cnt+1;
	return;
      }
    }
  }
}

int main(){
  int i,j;
  while(scanf("%d%d",&w,&h),w|h){
    ans = 11;
    rep(i,h){
      rep(j,w){
	scanf("%d",&field[i][j]);
	if( field[i][j]==2 ){
	  sx=j; sy=i;
	}
      }
    }
    dfs(sx,sy,0);
    printf("%d\n",ans==11?-1:ans);
  }
  return 0;
}