/**
 *    author:  otera    
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int w, h;
int sx, sy;
int step;

void rec(int s[21][21], int x, int y, int d) {
	if(step <= d + 1) return;
	int sb[21][21];
	rep(i, h) {
		rep(j, w) {
			sb[i][j] = s[i][j];
		}
	}
	//right
	int nowx = x, nowy = y;
	while (nowy + 1 < w && s[nowx][nowy + 1] == 0) {
		++ nowy;
	}
	if(nowy == w - 1) {
		//
	} else if(s[nowx][nowy + 1] == 3) {
		chmin(step, d + 1);
	} else if(s[nowx][nowy + 1] == 1) {
		sb[nowx][nowy + 1] = 0;
		if(nowy != y) rec(sb, nowx, nowy, d + 1);
		sb[nowx][nowy + 1] = 1;
	}
	//left
	nowx = x, nowy = y;
	while (nowy - 1 >= 0 && s[nowx][nowy - 1] == 0) {
		-- nowy;
	}
	if(nowy == 0) {
		//
	} else if(s[nowx][nowy - 1] == 3) {
		chmin(step, d + 1);
	} else if(s[nowx][nowy - 1] == 1) {
		sb[nowx][nowy - 1] = 0;
		if(nowy != y) rec(sb, nowx, nowy, d + 1);
		sb[nowx][nowy - 1] = 1;
	}
	//up
	nowx = x, nowy = y;
	while (nowx > 0 && s[nowx - 1][nowy] == 0) {
		-- nowx;
	}
	if(nowx == 0) {
		//
	} else if(s[nowx - 1][nowy] == 3) {
		chmin(step, d + 1);
	} else if(s[nowx - 1][nowy] == 1) {
		sb[nowx - 1][nowy] = 0;
		if(nowx != x) rec(sb, nowx, nowy, d + 1);
		sb[nowx - 1][nowy] = 1;
	}
	//down
	nowx = x, nowy = y;
	while (nowx + 1 < h && s[nowx + 1][nowy] == 0) {
		++ nowx;
	}
	if(nowx == h - 1) {
		//
	} else if(s[nowx + 1][nowy] == 3) {
		chmin(step, d + 1);
	} else if(s[nowx + 1][nowy] == 1) {
	    sb[nowx + 1][nowy] = 0;
		if(nowx != x) rec(sb, nowx, nowy, d + 1);
		sb[nowx + 1][nowy] = 1;
	}
}

void solve() {
    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;
        int s[21][21];
		rep(i, 21) {
			rep(j, 21) {
				s[i][j] = 0;
			}
		}
		rep(i, h) {
			rep(j, w) {
				cin >> s[i][j];
				if(s[i][j] == 2) {
					s[i][j] = 0;
					sx = i, sy = j;
				}
			}
		}
		step = 11;
		rec(s, sx, sy, 0);
		if(step >= 11) {
			cout << -1 << endl;
		} else {
			cout << step << endl;
		}
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}


Measured memory_bytes: 3160