#include<stdio.h>
int map[20][20];
int w, h;
int mx(int a, int b) {
	if (a == -1 && b == -1) return -1;
	if (a == -1) return b;
	if (b == -1) return a;
	if (a < b) {
		if (a == 10) return -1;
		return a;
	}
	if (b == 10) return -1;
	return b;
}
int solve(int x, int y) {
	int righ, lef, up, dow;
	int i, n;
	if (x == h - 1) dow = -1;
	else if (map[x + 1][y] == 1) dow = -1;
	else if (map[x + 1][y] == 3) return 1;
	else {
		for (i = 1; 1; i ++) {
			if (x + i >= h) {
				dow = -1; break;
			}
			if (map[x + i][y] == 1) {
				map[x + i][y] = 0;
				dow = solve(x + i - 1, y);
				map[x + i][y] = 1;
				break;
			} else if (map[x + i][y] == 3) return 1;
		}
	}
	if (x == 0) up = -1;
	else if (map[x - 1][y] == 1) up = -1;
	else if (map[x - 1][y] == 3) return 1;
	else {
		for (i = 1; 1; i ++) {
			if (x - i <= -1) {
				up = -1; break;
			}
			if (map[x - i][y] == 1) {
				map[x - i][y] = '0';
				up = solve(x - i + 1, y);
				map[x - i][y] = 1;
				break;
			} else if (map[x - i][y] == 3) return 1;
		}
	}
	if (y == w - 1) righ = -1;
	else if (map[x][y + 1] == 1) righ = -1;
	else if (map[x][y + 1] == 3) return 1;
	else {
		for (i = 1; 1; i ++) {
			if (y + i >= w) {
				righ = -1; break;
			}
			if (map[x][y + i] == 1) {
				map[x][y + i] = 0;
				righ = solve(x, y + i - 1);
				map[x][y + i] = 1;
				break;
			} else if (map[x][y + i] == 3) return 1;
		}
	}
	if (y == 0) lef = -1;
	else if (map[x][y - 1] == 1) lef = -1;
	else if (map[x][y - 1] == 3) return 1;
	else {
		for (i = 1; 1; i ++) {
			if (y - i <= -1) {
				lef = -1; break;
			}
			if (map[x][y - i] == 1) {
				map[x][y - i] = 0;
				righ = solve(x, y - i + 1);
				map[x][y - i] = 1;
				break;
			} else if (map[x][y - i] == 3) return 1;
		}
	}
	n = mx(dow, mx(up, mx(righ, lef)));
	if (n == -1) return -1;
	return n + 1;
}
int main() {
	int i, j, x, y, n;
	while (1) {
		scanf("%d %d", &w, &h);
		if (!(w||h)) break;
		for (i = 0; i < h; i ++) {
			for (j = 0; j < w; j ++) {
				scanf("%d", &map[i][j]);
				if (map[i][j] == 2) {
					x = i; y = j;
				}
			}
		}
		n = solve(x, y);
		printf("%d\n", n);
	}
	return 0;
}