using namespace std;
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>

#define INF INT_MAX/3

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define DECFOR(i,a,b) for(int i=(a);i>(b);--i)
#define REP(i,n)  FOR(i,0,n)

#define NDEBUG

#define MAX_W 20
#define MAX_H 20
#define MOVING_LIMIT 10

#define NORTH 0
#define EAST  1
#define SOUTH 2
#define WEST  3

enum CellState {
  EMPTY,
  OBSTACLE,
  START,
  STOP,
  OUTSIDE
};

struct Position {
  int x;
  int y;
};

struct Moving {
  Position begin_cell;
  Position obstacle_cell;
  int dir;
  int is_goal;
};


struct GameState {
  int width;
  int height;
  CellState field[MAX_H][MAX_W];
  Position pos;
  int dir;
  Moving history[MOVING_LIMIT];
  int count;
};

int load(GameState* state) {
  int w, h;
  scanf("%d %d", &w, &h);
  if( w == 0 && h == 0 ) return 0;
  state->width = w;
  state->height = h;
  REP(j, h) REP(i, w) {
    scanf("%d", &(state->field[j][i]));
    if( state->field[j][i] == START ) {
      state->pos.x = i;
      state->pos.y = j;
    }
  }
  return 1;
}

void get_next_cell(Position* buf, Position* begin, int dir) {
  *buf = *begin;
  switch(dir) {
  case NORTH:
    buf->x -= 1;
    break;
  case EAST:
    buf->y += 1;
    break;
  case SOUTH:
    buf->x += 1;
    break;
  case WEST:
    buf->y -= 1;
    break;
  default:
    assert(0);
    break;
  }
}

CellState get_cell_state(GameState* state, Position* pos) {
  int x = pos->x;
  int y = pos->y;
  if( x < state->width && y < state->height && x >= 0 && y >= 0 ) {
    return state->field[pos->y][pos->x];
  } else {
    return OUTSIDE;
  }
}

void set_cell_state(GameState* state, Position* pos, CellState s) {
  int x = pos->x;
  int y = pos->y;
  assert( x < state->width && y < state->height && x >= 0 && y >= 0 );
  state->field[pos->y][pos->x] = s;
}

enum MovingResult {
  SUCCESS,
  FAILURE,
  GOAL
};

MovingResult move(GameState* state) {
  int dir = state->dir;
  Position next;
  get_next_cell(&next, &state->pos, dir);
  if( get_cell_state(state, &next) == OBSTACLE ) {
    return FAILURE;
  }

  Moving* mv = &state->history[state->count];
  mv->begin_cell = state->pos;
  mv->dir = dir;

  while(true) {
    switch( get_cell_state(state, &next) ) {
    case EMPTY:
    case START:
      state->pos = next;
      get_next_cell(&next, &state->pos, dir);
      break;
    case OBSTACLE:
      mv->obstacle_cell = next;
      mv->is_goal = 0;
      set_cell_state(state, &next, EMPTY);
      ++(state->count);
      return SUCCESS;
    case STOP:
      ++(state->count);
      mv->is_goal = 1;
      return GOAL;
    default:
      state->pos = mv->begin_cell;
      return FAILURE;
    }
  }
}

int rollback(GameState* state) {
  if( state->count <= 0 ) {
    return 0;
  }
  --(state->count);
  Moving* mv = &state->history[state->count];
  if( !mv->is_goal ) {
    set_cell_state(state, &mv->obstacle_cell, OBSTACLE);
  }
  state->pos = mv->begin_cell;
  state->dir = mv->dir;
  return 1;
}

int rotate(GameState* state) {
  if( state->dir != WEST ) {
    state->dir = (state->dir+1) % 4;
    return 1;
  } else {
    return 0;
  }
}

int retry(GameState* state) {
  while(1) {
    if( rotate(state) ) {
        return 1;
    }
    if( !rollback(state) ) {
        return 0;
    }
  }
}

int solve(GameState* state) {
  int ret = -1;
  int loop_flag = 1;
  state->dir = NORTH;
  state->count = 0;
  while( loop_flag ) {
    switch( move(state) ) {
    case SUCCESS:
      if( state->count >= MOVING_LIMIT ) {
        loop_flag = rollback(state) && retry(state);
        break;
      }
      state->dir = NORTH;
      break;
    case FAILURE:
      loop_flag = retry(state);
      break;
    case GOAL:
      if( ret > 0 ) {
        ret = min(ret, state->count);
      } else {
        ret = state->count;
      }
      loop_flag = rollback(state) && retry(state);
      break;
    }
  }
  return ret;
}



int main() {
  GameState state;
  while( load(&state) ) {
    printf("%d\n", solve(&state));
  }
  return 0;
}

Measured memory_bytes: 1188