#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)

int H, W, Sr, Sc, Gr, Gc, ans;
int B[25][25];
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};

void dfs(int r, int c, int d) {
    if (d == 10) return;
    REP(i, 4) {
        int nr = r;
        int nc = c;
        bool out = false;
        bool goal = false;
        while (true) {
            nr += dr[i];
            nc += dc[i];
            if (nr < 0 || nr >= H || nc < 0 || nc >= W) {
                out = true;
                break;
            }
            if (nr == Gr && nc == Gc) {
                goal = true;
                ans = min(ans, d+1);
                break;
            }
            if (B[nr][nc] == 1) {
                break;
            }
        }

        if (out) continue;
        if (goal) return;
        if (nr-dr[i] == r && nc-dc[i] == c) continue;

        B[nr][nc] = 0;
        dfs(nr-dr[i], nc-dc[i], d+1);
        B[nr][nc] = 1;
        
    }
}

int solve() {
    cin >> W >> H;
    if (H == 0) return 0;

    REP(i, H) REP(j, W) {
        cin >> B[i][j];
        if (B[i][j] == 2) Sr = i, Sc = j;
        if (B[i][j] == 3) Gr = i, Gc = j;
    }

    ans = 1 << 29;
    dfs(Sr, Sc, 0);
    if (ans == 1 << 29) ans = -1;
    
    return ans;
}

int main() {
    while (true) {
        int ret = solve();
        if (ret == 0) break;
        cout << ret << endl;
    }
}