// Aizu 1144: Curling 2.0
// 2017.9.17 bal4u@uu

#include <stdio.h>
#include <string.h>

int m[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
char map[22][22]; int w, h;
int ans;

void combi(int r0, int c0, int k)
{
	int i, r, c, dr, dc;

	if (k++ == 10) return;
	for (i = 0; i < 4; i++) {
		dr = m[i][0], dc = m[i][1];
		r = r0 + dr, c = c0 + dc;
		if (r < 0 || r >= h || c < 0 || c >= w || map[r][c] == 1) continue;
		r = r0, c = c0;
		while (1) {
			r += dr, c += dc;
			if (r < 0 || r >= h || c < 0 || c >= w) break;
			if (map[r][c] == 3) { if (k < ans) ans = k; break; }
			if (map[r][c] == 1) {
				map[r][c] = 0, combi(r-dr, c-dc, k), map[r][c] = 1;
				break;
			}
		}
	}
}

int main()
{
	int a, r, c, sr, sc;

	while(scanf("%d%d", &w, &h) && w) {
		for (ans = sr = -1, r = 0; r < h; r++) for (c = 0; c < w; c++) {
			scanf("%d", &a), map[r][c] = a;
			if (a == 2) sr = r, sc = c, map[r][c] = 0;
		}
		if (sr >= 0) { ans = 11, combi(sr, sc, 0); if (ans > 10) ans = -1; }
		printf("%d\n", ans);
	}
	return 0;
}