#include <iostream>

using namespace std;

int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };

int field[20][20];

int dfs(int W, int H, int x, int y, int step, int limit){
	if(++step > limit){ return limit; }
	for(int i = 0; i < 4; ++i){
		int fx = x + dx[i], fy = y + dy[i];
		if(fx < 0 || fx >= W || fy < 0 || fy >= H){ continue; }
		while(!(fx < 0 || fx >= W || fy < 0 || fy >= H)){
			if(field[fy][fx] < 0){ return step; }
			if(field[fy][fx] > 0){
				fx -= dx[i]; fy -= dy[i];
				break;
			}
			fx += dx[i]; fy += dy[i];
		}
		if(fx < 0 || fx >= W || fy < 0 || fy >= H){ continue; }
		if(fx == x && fy == y){ continue; }
		field[fy + dy[i]][fx + dx[i]] = 0;
		limit = min(limit, dfs(W, H, fx, fy, step, limit));
		field[fy + dy[i]][fx + dx[i]] = 1;
	}
	return limit;
}

int main(){
	while(true){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0){ break; }
		int x = 0, y = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cin >> field[i][j];
				if(field[i][j] == 2){
					x = j; y = i;
					field[i][j] = 0;
				}else if(field[i][j] == 3){
					field[i][j] = -1;
				}
			}
		}
		int answer = dfs(w, h, x, y, 0, 11);
		if(answer >= 11){ answer = -1; }
		cout << answer << endl;
	}
	return 0;
}
/* 駄実装の典型 皆さんはstackではなくてn進法を使いましょう */
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <deque>
#include <algorithm>

using namespace std;

int main() {
    while (1) {
        int n;
        int k;
        vector<string> element_strings;
        set<string> generated_strings;
        deque<int> stack;
    
        scanf("%d", &n);
        scanf("%d", &k);
    
        if (n == 0 && k == 0) return 0;
 
        for (int i=0; i<n; i++) {
            char number[3];
            scanf("%s", number);
            element_strings.push_back(string(number));
        }
    
        sort(element_strings.begin(), element_strings.end());
    
        while (1) {
            char has_appeared[11];
            char not_count = 0;
            string new_string = "";
            int next_ind;
    
            while (stack.size() != k) {
                stack.push_back(0);
            }

            memset(has_appeared, 0, sizeof(has_appeared));

            for (deque<int>::iterator itr=stack.begin(); itr != stack.end(); itr++) {
                if (has_appeared[*itr]) {
                    not_count = 1;
                    break;
                }
                has_appeared[*itr] = 1;
                new_string += element_strings[*itr];
            }

            if (!not_count && generated_strings.count(new_string) == 0) { 
                generated_strings.insert(new_string);
            }
    
            while (!stack.empty() && stack.back() == element_strings.size()-1) {
                stack.pop_back();
            }
    
            if (stack.empty()) {
                break;
            }
    
            next_ind = stack.back() + 1;
            stack.pop_back();
            stack.push_back(next_ind);
        }
    
        printf("%lu\n", generated_strings.size());
    }
}