#include <iostream>
#define inf 1000000000

using namespace std;

int w, h;
int map[25][25];
int sx, sy, gx, gy;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dfs(int map[25][25], int px, int py, int rem)
{
  if(px == gx && py == gy) return 0;
  if(rem == 0) return inf;
  int ret = inf;
  for(int d = 0; d < 4; d++){
    int map2[25][25];
    for(int x = 1; x <= w; x++){
      for(int y = 1; y <= h; y++){
        map2[x][y] = map[x][y];
      }
    }
    int x = px, y = py, nx, ny;
    while(1){
      nx = x + dx[d], ny = y + dy[d];
      if(nx <= 0 || nx > w || ny <= 0 || ny > h) break;
      if(nx == gx && ny == gy) return 1;
      if(map2[nx][ny] == 1){
        if(x == px && y == py) break;
        map2[nx][ny] = 0;
        ret = min(ret, dfs(map2, x, y, rem-1));
        break;
      }
      x = nx, y = ny;
    }
  }
  return ret + 1;
}

int main(void)
{
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    for(int y = 1; y <= h; y++){
      for(int x = 1; x <= w; x++){
        cin >> map[x][y];
        if(map[x][y] == 2){
          sx = x, sy = y;
          map[x][y] = 0;
        }
        if(map[x][y] == 3){
          gx = x, gy = y;
          map[x][y] = 0;
        }
      }
    }

    int ans = dfs(map, sx, sy, 10);
    if(ans >= inf) ans = -1;
    cout << ans << endl;
  }
  return 0;
}

