#include <bits/stdc++.h>

#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
#define X first
#define Y second
#define in(a,b,c) (b<=a&&a<c)

using namespace std;

using P=pair<int, int>;

const P dir[4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int m[20][20];
int res;
int W, H;

void dfs(int depth, int ys, int xs) {
	if(depth>=res) return;
	for(P D:dir) {
		int x = xs+D.X;
		int y = ys+D.Y;
		if(m[y][x] == 1) continue;
		while(in(y, 0, H)&&in(x, 0, W)&&m[y][x] != 1) {
			if(m[y][x] == 3) {
				res = depth;
				return;
			}
			else {
				x += D.X; y += D.Y;
			}
		}
		if(in(y, 0, H)&&in(x, 0, W)&&m[y][x] == 1) {
			m[y][x] = 0;
			dfs(depth+1, y-D.Y, x-D.X);
			m[y][x] = 1;
		}
	}
}

int main() {
	while(cin >> W >> H && W && H) {
		P S;
		res = 11;

		rep(i,H) rep(j,W) {
			cin >> m[i][j];
			if(m[i][j] == 2) S = pair<int, int>(j, i);
		}

		dfs(1, S.Y, S.X);

		if(res==11)
			cout << -1 << endl;
		else
			cout << res << endl;

	}
	return 0;
}