Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy as dcp
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, lb, ub, field, count):
	result = 11
	for dx, dy in delta:
		next = (here[0]+dx,here[1]+dy)
		if ( not (lb[0] <= next[0] <= ub[0] and lb[1] <= next[1] <= ub[1]) or
		   field[next[1]][next[0]] == 1) :
		    continue
		tmp = slide(next, (dx,dy), lb, ub, dcp(field), count + 1) 
		if tmp < result :
			result = tmp
	return result

def slide(here, di, lb, ub, field, count):
	while lb[0] <= here[0] <= ub[0] and lb[1] <= here[1] <= ub[1]:
		if here == goal:
			return count
		if field[here[1]][here[0]] == 1:
			field[here[1]][here[0]] = 0
			return ds((here[0]-di[0],here[1]-di[1]), lb, ub, field, count)
		here = (here[0]+di[0],here[1]+di[1])
	else:
		return 11
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [[0 for _ in range(w)] for _ in range(h)]
	for j in range(h):
		inp = map(int,raw_input().split(" "))
		for i in range(w):
			tmp = inp[i]
			if tmp == 0: continue
			if tmp == 1: board[j][i] = 1
			elif tmp == 2: start = (i,j)
			else : goal = (i,j)
	count = ds(start, (0,0), (w-1,h-1), board, 0) 
	print count if count < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy as dcp
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, lb, ub, field, count):
	result = 11
	if count == 11:
		return count
	for dx, dy in delta:
		next = (here[0]+dx,here[1]+dy)
		if ( not (lb[0] <= next[0] <= ub[0] and lb[1] <= next[1] <= ub[1]) or
		   field[next[1]][next[0]] == 1) :
		    continue
		tmp = slide(next, (dx,dy), lb, ub, dcp(field), count + 1) 
		if tmp < result:
			result = tmp
	return result

def slide(here, di, lb, ub, field, count):
	while lb[0] <= here[0] <= ub[0] and lb[1] <= here[1] <= ub[1]:
		if here == goal:
			return count
		if field[here[1]][here[0]] == 1:
			field[here[1]][here[0]] = 0
			return ds((here[0]-di[0],here[1]-di[1]), lb, ub, field, count)
		here = (here[0]+di[0],here[1]+di[1])
	else:
		return 11
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [[0 for _ in range(w)] for _ in range(h)]
	for j in range(h):
		inp = map(int,raw_input().split(" "))
		for i in range(w):
			tmp = inp[i]
			if tmp == 0: continue
			if tmp == 1: board[j][i] = 1
			elif tmp == 2: start = (i,j)
			else : goal = (i,j)
	count = ds(start, (0,0), (w-1,h-1), board, 0) 
	print count if count < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Buggy python submission (verdict: Time Limit Exceeded):
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, lb, ub, field, count):
	result = 11
	if count == 11:
		return count
	for dx, dy in delta:
		next = (here[0]+dx,here[1]+dy)
		if ( not (lb[0] <= next[0] <= ub[0] and lb[1] <= next[1] <= ub[1]) or
		   field[next[1]][next[0]] == 1) :
		    continue
		tmp = slide(next, (dx,dy), lb, ub, field, count + 1) 
		if tmp < result:
			result = tmp
	return result

def slide(here, di, lb, ub, field, count):
	while lb[0] <= here[0] <= ub[0] and lb[1] <= here[1] <= ub[1]:
		if here == goal:
			result = count
			break
		if field[here[1]][here[0]] == 1:
			field[here[1]][here[0]] = 0
			result = ds((here[0]-di[0],here[1]-di[1]), lb, ub, field, count)
			field[here[1]][here[0]] = 1
			break
		here = (here[0]+di[0],here[1]+di[1])
	else:
		result = 11
	return result
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [[0 for _ in range(w)] for _ in range(h)]
	for j in range(h):
		inp = map(int,raw_input().split(" "))
		for i in range(w):
			tmp = inp[i]
			if tmp == 0: continue
			if tmp == 1: board[j][i] = 1
			elif tmp == 2: start = (i,j)
			else : goal = (i,j)
	count = ds(start, (0,0), (w-1,h-1), board, 0) 
	print count if count < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Buggy python submission (verdict: Time Limit Exceeded):
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub):
	if count == 11:
		return count
	ans = 11
	for dx, dy in delta:
		neighbor = (here[0]+dx,here[1]+dy)
		nx, ny = neighbor
		tmp = 11
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if not(nx == neighbor[0] and ny == neighbor[1]):
					field[ny][nx] = 0
					tmp = ds((nx-dx,ny-dy),field, count+1, lb, ub)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				tmp = count + 1
				break
			nx += dx
			ny += dy
		if tmp < ans:
			ans = tmp 
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	ans = ds(start, board, 0, (0,0), (w-1,h-1)) 
	print ans if ans < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Buggy python submission (verdict: Time Limit Exceeded):
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count > min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		neighbor = (here[0]+dx,here[1]+dy)
		nx, ny = neighbor
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if not(nx == neighbor[0] and ny == neighbor[1]):
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3: 
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Buggy python submission (verdict: Time Limit Exceeded):
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count > min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1

Fixed submission:
delta = ((1, 0), (0, 1), (-1, 0), (0, -1))
def ds(here, field, count, lb, ub, min_count):
	if count == min_count:
		return min_count
	ans = min_count
	for dx, dy in delta:
		nx, ny = (here[0]+dx,here[1]+dy)
		neighbor = (nx, ny)
		while lb[0] <= nx <= ub[0] and lb[1] <= ny <= ub[1]:
			if field[ny][nx] == 1:
				if (nx, ny) != neighbor:
					field[ny][nx] = 0
					ans = ds((nx-dx,ny-dy),field, count+1, lb, ub, ans)
		    		field[ny][nx] = 1
				break
			if field[ny][nx] == 3:
				if count + 1 < ans:
					ans = count + 1
				break
			nx += dx
			ny += dy
	return ans
while 1:
	w, h = map(int,raw_input().split(" "))
	if w == h == 0: break
	board = [ map(int,raw_input().split(" ")) for _ in range(h)]
	for j in range(h):
		for i in range(w):
			if board[j][i] == 2:
				start = (i,j)
	result = ds(start, board, 0, (0,0), (w-1,h-1), 11)
	print result if result < 11 else -1