### constant

MAX_STEP = 10
DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

### subroutines

def count_rec(k, x, y)
  return k if k > MAX_STEP

  min_count = MAX_STEP + 1

  DXYS.each do |dx, dy|
    x0 = x + dx
    y0 = y + dy
    next if x0 >= 0 && x0 < $w && y0 >= 0 && y0 < $h && $flds[y0][x0] == 1
    while x0 >= 0 && x0 < $w && y0 >= 0 && y0 < $h
      return k if x0 == $gx && y0 == $gy
      if $flds[y0][x0] == 1
        $flds[y0][x0] = 0
        c = count_rec(k + 1, x0 - dx, y0 - dy)
        min_count = c if min_count > c
        $flds[y0][x0] = 1
        break
      end
      x0 += dx
      y0 += dy
    end
  end
  min_count
end

### main

loop do
  $w, $h = gets.split.map(&:to_i)
  break if ($w | $h) == 0

  $flds = $h.times.map{$w.times.map{0}}
  sx = sy = $gx = $gy = 0

  for y in (0...$h)
    line = gets.split.map(&:to_i)
    for x in (0...$w)
      case line[x]
      when 1
        $flds[y][x] = 1
      when 2
        sx = x
        sy = y
      when 3
        $gx = x
        $gy = y
      end
    end
  end
  #p $flds
  #p [sx, sy, $gx, $gy]

  c = count_rec(1, sx, sy)
  puts (c > MAX_STEP ? -1 : c)
end