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();
	}

	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(cMap[cY - 1][cX] == BLOCK){//?¬??????????????£??????´???
								//?????????????????????????????????????????????
								cMap[cY - 1][cX] = SPACE;
								cMap[cY][cX] = START;
								addMapList(cMap);
								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(cMap[cY][cX + 1] == BLOCK){//?¬??????????????£??????´???
								cMap[cY][cX + 1] = SPACE;
								cMap[cY][cX] = START;
								addMapList(cMap);
								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(cMap[cY + 1][cX] == BLOCK){//?¬??????????????£??????´???
								cMap[cY + 1][cX] = SPACE;
								cMap[cY][cX] = START;
								addMapList(cMap);
								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(cMap[cY][cX - 1] == BLOCK){//?¬??????????????£??????´???
								cMap[cY][cX - 1] = SPACE;
								cMap[cY][cX] = START;
								addMapList(cMap);
								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: 299624