import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	char map[][];
	int h,w;
	LinkedList<char[][]> mapList = new LinkedList<char[][]>();
	//ArrayList??????Queue???LinkedList???????????????????????????
	static final int TOP = 0;
	static final int RIGHT = 1;
	static final int BOTTOM = 2;
	static final int LEFT = 3;
	static final char SPACE = 0;
	static final char BLOCK = 1;
	static final char START = 2;
	static final char GOAL = 3;
	static final int OUT = -1;
	void run(){
		Scanner scan = new Scanner(System.in);
		while(true){
			w = scan.nextInt();
			h = scan.nextInt();
			if(w == 0 && h == 0)break;
			map = new char[h][w];
			int x = 0;
			int y = 0;
			for(int i = 0;i < h;i++)
				for(int j = 0;j < w;j++)
					map[i][j] = (char) scan.nextInt();
			addMapList(map);
			System.out.println(solve());
			cleanMapList();
		}
	}

	void cleanMapList(){//mapList?????????????????????
		mapList.clear();
	}

	void addMapList(char[][] map){//mapList?????°???????????¢?????????
			mapList.addLast(map);
	}

	void getMapList(){//map????¬?????????¢???????????????
		map = mapList.getFirst();
		mapList.remove();
	}

	boolean hitBlock(char[][] cMap,int dir,int x,int y){
		int dX,dY;
		switch(dir){
			case TOP:
				dX = 0;
				dY = -1;
				break;
			case RIGHT:
				dX = 1;
				dY = 0;
				break;
			case BOTTOM:
				dX = 0;
				dY = 1;
				break;
			case LEFT:
				dX = -1;
				dY= 0;
				break;
			default:
				dX = dY = 0;
		}
		if(cMap[y + dY][x + dX] == BLOCK){//?¬??????????????£??????´???
			cMap[y + dY][x + dX] = SPACE;
			cMap[y][x] = START;
			addMapList(cMap);
			return true;
		}else{
			return false;
		}
	}

	int solve(){
		int cnt = 1;
		int add = 0;//??????????????¶??°
		int stock = 1;//????????????????????¶??°
		while(true){
			for(int i = 0;i < stock;i++){
				getMapList();
				int x,y;
				x = y = 0;
				out:for(y = 0;y < h;y++)
					for(x = 0;x < w;x++)
						if(map[y][x] == START)break out;
				map[y][x] = SPACE;
				for(int move = 0;move < 4;move++){//4??????????§????????????§??????
					//currentX,currentY
					int cX = x;
					int cY = y;
					char cMap[][] = new char[h][w];
					for(int a = 0;a < h;a++)
						cMap[a] = map[a].clone();
					switch(move){
					case TOP:
						if(cY > 1 && cMap[cY - 1][cX] == BLOCK){//?????????????????£??\??????????£??????´???
							break;
						}
						while(true){
							if(cY - 1 < 0)//?¬????????????????????????´???
								break;
							if(hitBlock(cMap,TOP,cX,cY)){
								add++;
								break;
							}
							if(cMap[cY - 1][cX] == GOAL){//?¬???????????????´???????????´???
								return cnt;
							}
							cY--;
						}
						break;
					case RIGHT:
						if(cX + 1 < w && cMap[cY][cX + 1] == BLOCK){//?????????????????£??\??????????£??????´???
							break;
						}
						while(true){
							if(cX + 1 >= w)//?¬????????????????????????´???
								break;
							if(hitBlock(cMap,RIGHT,cX,cY)){
								add++;
								break;
							}
							if(cMap[cY][cX + 1] == GOAL){//?¬???????????????´???????????´???
								return cnt;
							}
							cX++;
						}
						break;
					case BOTTOM:
						if(cY + 1 < h && cMap[cY + 1][cX] == BLOCK){//?????????????????£??\??????????£??????´???
							break;
						}
						while(true){
							if(cY + 1 >= h)//?¬????????????????????????´???
								break;
							if(hitBlock(cMap,BOTTOM,cX,cY)){
								add++;
								break;
							}
							if(cMap[cY + 1][cX] == GOAL){//?¬???????????????´???????????´???
								return cnt;
							}
							cY++;
						}
						break;
					case LEFT:
						if(cX > 1 && cMap[cY][cX - 1] == BLOCK){//?????????????????£??\??????????£??????´???
							break;
						}
						while(true){
							if(cX - 1 < 0)//?¬????????????????????????´???
								break;
							if(hitBlock(cMap,LEFT,cX,cY)){
								add++;
								break;
							}
							if(cMap[cY][cX - 1] == GOAL){//?¬???????????????´???????????´???
								return cnt;
							}
							cX--;
						}
						break;
					}
				}
			}
			stock = add;
			add = 0;
			cnt++;
			if(cnt > 10){
				return OUT;
			}
		}
	}

	public static void main(String args[]){
		new Main().run();
	}
}

Measured memory_bytes: 301236