#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int w,h;
  while(cin>>w>>h,w+h){
    int Map[h+5][w+5]={};
    int a,b;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>Map[i][j];
      }
    }
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(Map[i][j]==2){
	  a=i;b=j;
	  break;
	}
    int ansfl=-1;
    vector<pair<int,int> >AL;
    queue <pair < pair<int,int>,vector<pair<int,int> >  > > que;
    que.push(mp(mp(a,b),AL));
    while(!que.empty()){
      if(que.front().S.size()>=10)
	break;
      vector<pair<int,int> > AA=que.front().S;
      pair<int,int> now=mp(que.front().F.F,que.front().F.S);
      que.pop();
      bool flag2=false,flag3=false,flag4=false,flag5=false;
      for(int j=0;j<AA.size();j++){
	if(now.F==AA[j].F&&now.S-1==AA[j].S){
	  flag2=true;
	}else if(now.F==AA[j].F&&now.S+1==AA[j].S){
	  flag3=true;
	}else if(now.F-1==AA[j].F&&now.S==AA[j].S){
	  flag4=true;
	}else if(now.F+1==AA[j].F&&now.S==AA[j].S){
	  flag5=true;
	}
      }
      if(now.S-1>=0&&Map[now.F][now.S-1]!=1)flag2=true;
      if(now.S+1<w&&Map[now.F][now.S+1]!=1)flag3=true;
      if(now.F-1>=0&&Map[now.F-1][now.S]!=1)flag4=true;
      if(now.F+1<h&&Map[now.F+1][now.S]!=1)flag5=true;
      if((now.S-1>=0&&flag2)){
	for(int i=now.S-1;i>=0;i--){
	  if(Map[now.F][i]==3){
	    ansfl=AA.size()+1;
	    break;
	  }else if(Map[now.F][i]==1){
	    bool flag=true;
	    for(int j=0;j<AA.size();j++){
	      if(now.F==AA[j].F&&i==AA[j].S){
		flag=false;
		break;
	      }
	    }
	    if(flag){
	      AA.pb(mp(now.F,i));
	      que.push(mp(mp(now.F,i+1),AA));
	      AA.pop_back();
	      break;
	    }
	  }
	}
      }
      if(ansfl>0)break;
      if(now.S+1<w&&flag3){
	for(int i=now.S+1;i<w;i++){
	  if(Map[now.F][i]==3){
	    ansfl=AA.size()+1;
	    break;
	  }else if(Map[now.F][i]==1){
	    bool flag=true;
	    for(int j=0;j<AA.size();j++){
	      if(now.F==AA[j].F&&i==AA[j].S){
		flag=false;
		break;
	      }
	    }
	    if(flag){
	      AA.pb(mp(now.F,i));
	      que.push(mp(mp(now.F,i-1),AA));
	      AA.pop_back();
	      break;
	    }
	  }
	}
      }
      if(ansfl>0)break;
      if(now.F-1>=0&&flag4){
	for(int i=now.F-1;i>=0;i--){
	  if(Map[i][now.S]==3){
	    ansfl=AA.size()+1;
	    break;
	  }else if(Map[i][now.S]==1){
	    bool flag=true;
	    for(int j=0;j<AA.size();j++){
	      if(i==AA[j].F&&now.S==AA[j].S){
		flag=false;
		break;
	      }
	    }
	    if(flag){
	      AA.pb(mp(i,now.S));
	      que.push(mp(mp(i+1,now.S),AA));
	      AA.pop_back();
	      break;
	    }
	  }
	}
      }
      if(ansfl>0)break;
      if(now.F+1<h&&flag5){
	for(int i=now.F+1;i<h;i++){
	  if(Map[i][now.S]==3){
	    ansfl=AA.size()+1;
	    break;
	  }else if(Map[i][now.S]==1){
	    bool flag=true;
	    for(int j=0;j<AA.size();j++){
	      if(i==AA[j].F&&now.S==AA[j].S){
		flag=false;
		break;
	      }
	    }
	    if(flag){
	      AA.pb(mp(i,now.S));
	      que.push(mp(mp(i-1,now.S),AA));
	      AA.pop_back();
	      break;
	    }
	  }
	}
      }
      if(ansfl>0)break;
      AA.clear();
    }
    if(ansfl>10)ansfl=-1;
    cout<<ansfl<<endl;
  }
  return 0;
}

Measured memory_bytes: 12312