#include<iostream>
#include<vector>
#include<queue>

using namespace std;

int w,h;
vector<vector<int> >  b;
int sx,sy,gx,gy;
int ans;

void test(int x,int y,int n){
  int bx,by;
  int xx=x,yy=y;
  if(++n>=ans) return;

  while(b[y][x+1]==0){
    x++;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if((x!=xx)&&(x+1!=w-1)){
    bx=x+1; by=y;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y][x-1]==0){
    x--;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(x!=xx&&x-1!=0){
    bx=x-1; by=y;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y+1][x]==0){
    y++;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(y!=yy&&y+1!=h-1){
    bx=x; by=y+1;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;

  while(b[y-1][x]==0){
    y--;
    if(x==gx&&y==gy){
      ans=n;
      return;
    }
  }
  if(y!=yy&&y-1!=0){
    bx=x; by=y-1;
    b[by][bx]=0;
    test(x,y,n);
    b[by][bx]=1;
  }
  x=xx;y=yy;
}

int main(){
  while(cin>>w>>h,w+h){
    w+=2;h+=2;
    ans=11;
    b=vector<vector<int> >(h+2,vector<int>(w+2,1));
    for(int i=1;i<h-1;i++){
      for(int j=1;j<w-1;j++){
        cin >> b[i][j];
        if(b[i][j]==2){
          b[i][j]=0;
          sx=j;sy=i;
        }else if(b[i][j]==3){
          b[i][j]=0;
          gx=j;gy=i;
        }
      }
    }
    test(sx,sy,0);
    if(ans==11) cout << -1 << endl;
    else cout << ans << endl;
  }
  return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define HMAX 20
#define WMAX 20
using namespace std;

int w,h;

int dd[5]={0,1,0,-1,0};

void dfs_aux(int sx, int sy, int map[WMAX][HMAX],int *min,int count){
  rep(r,4){
    int nx=sx,ny=sy;
    if(map[nx+dd[r]][ny+dd[r+1]]==1)
      continue;
    while(0<=nx+dd[r] && nx+dd[r]<w && 0<=ny+dd[r+1] && ny+dd[r+1]<h){
      nx+=dd[r];
      ny+=dd[r+1];
      if(map[nx][ny]==3)
	if(*min > count){
	  *min = count;
	  break;
	}
      if(map[nx][ny]==1){
	break;
      }
    }
    
    if(map[nx][ny]==1 && count <10){
      map[nx][ny]=0;
      nx-=dd[r];ny-=dd[r+1];
      dfs_aux(nx,ny,map,min,count+1);
      nx+=dd[r];ny+=dd[r+1];
      map[nx][ny]=1;
    }
  }
}  

int dfs(int sx, int sy, int map[WMAX][HMAX]){
  int min=11;
  dfs_aux(sx,sy,map,&min,1);
  if(min>10)
    return -1;
  return min;
}

int main(){
  for(;;){
    scanf("%d %d",&w,&h);
    if(!(w+h)) break;
    int map[WMAX][HMAX],sx,sy;
    rep(i,h){
      rep(j,w){
	scanf("%d",&map[j][i]);
	if(map[j][i]==2){
	  sx=j;
	  sy=i;
	}
      }
    }
    printf("%d\n",dfs(sx,sy,map));
  }
}