#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

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

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

int w,h;
char field[22][22];
int ans,sx,sy;

inline bool inside(char x, char y){
  return (!(x<0 || x>=w || y<0 || y>=h));
}

void dfs(char x, char y, char cnt){
  char tx,ty;
  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( !inside(tx+dx[k],ty+dy[k]) || field[ty+dy[k]][tx+dx[k]]==1 )continue;
    REP(j,1,30){
      tx += dx[k];
      ty += dy[k];
      if( !inside(tx,ty) ) break;
      if( field[ty][tx]==1 ){
	field[ty][tx] = 0;
	dfs(tx-dx[k], ty-dy[k], cnt+1);
	field[ty][tx] = 1;
	break;
      }else if( field[ty][tx]==3 ){
	ans = cnt+1;
	return;
      }
    }
  }
}

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