#include<bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i=0; i<n; i++)

const int MAX_W = 22;
const int MAX_H = 22;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int H, W;
int maze[MAX_H+1][MAX_W+1];

inline bool check(int hx, int hy){ return 0 <= hx && hx < H && 0 <= hy && hy < W; }

void dfs(int hx, int hy, int cnt, int& res)
{
  rep(k, 4){
    int nx = hx + dx[k];
    int ny = hy + dy[k];
    if(check(nx, ny) && maze[nx][ny] == 1) continue;
    while(check(nx, ny) && maze[nx][ny] == 0) nx += dx[k],  ny += dy[k];
    if(!check(nx, ny)) continue;
    if(maze[nx][ny] == 3){
      if(res > cnt) res = cnt;
      return;
    }
    if(cnt >= 10) continue;
    maze[nx][ny] = 0;
    dfs(nx - dx[k], ny - dy[k], cnt + 1, res);
    maze[nx][ny] = 1;
  }
}

int main()
{
  while(cin >> W >> H, W){
    rep(i, H) rep(j, W) cin >> maze[i][j];

    int res = 1E9;
    rep(i, H) rep(j, W) if(maze[i][j] == 2){
      maze[i][j] = 0;
      dfs(i, j, 1, res);
    }

    cout << (res >= 1E9 ? -1 : res) << endl;
  }

  return 0;
}