#include <bits/stdc++.h>
using namespace std;
const int lim = 21;
const int inf = 1e9;
int W, H, sy, sx;

int ans = inf;

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

void dfs(int n, int y, int x, vector<vector<int>> v){
    if(n > 10 || n >= ans) return;
    for(int i=0; i<4; i++){
        int ny = y + dy[i];
        int nx = x + dx[i];
        
        if(0 <= ny && ny < H && 0 <= nx && nx < W && v[ny][nx] != 1){
            while(0 <= ny && ny < H && 0 <= nx && nx < W){

                if(v[ny][nx] == 3){
                    ans = min(ans, n);
                    return;
                }

                if(v[ny][nx] == 1){
                    v[ny][nx] = 0;
                    dfs(n+1, ny - dy[i], nx - dx[i], v);
                    v[ny][nx] = 1;
                    break;
                }
                ny += dy[i];
                nx += dx[i];
            }
        }
    }
    return;
}

int main(){
    
    while(true){
        ans = inf;
        cin >> W >> H;
        vector<vector<int>> v(H, vector<int>(W, 0));
        if(W == 0 && H == 0) return 0;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cin >> v[i][j];
                if(v[i][j] == 2){
                    sy = i, sx = j;
                    v[sy][sx] = 0;
                }
            }
        }
        dfs(1, sy, sx, v);
        if(ans == inf) cout << -1 << endl;
        else cout << ans << endl;
    }
}
