#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

int w, h;
int st[2], ed[2];
bool bo[22][22];

int DFS(int num, int x, int y) {
    //cout << num << endl;
    int ans = 100;
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx >= 0 && xx < h && yy >= 0 && yy < w) {
            if (bo[xx][yy]) {
                if (xx == ed[0] && yy == ed[1]) {
                    ans = 1;
                    break;
                }
                while (1) {
                    int xxx = xx + dx[i];
                    int yyy = yy + dy[i];
                    if (xxx >= 0 && xxx < h && yyy >= 0 && yyy < w) {
                        if (bo[xxx][yyy]) {
                            if (xxx == ed[0] && yyy == ed[1]) {
                                ans = 1;
                                break;
                            } else {
                                xx = xxx;
                                yy = yyy;
                            }
                        } else {
                            if (num <= 8) {
                                bo[xxx][yyy] = true;
                                //cout << num << " " << x << " " << y << endl;
                                int d = DFS(num + 1, xx, yy);
                                if (d != -1 && d + 1 < ans && d + 1 <= 10) {
                                    ans = d + 1;
                                }
                                bo[xxx][yyy] = false;
                                break;
                            } else {
                                break;
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
        }
    }
    if (ans == 100) {
        return -1;
    } else {
        return ans;
    }
}

int main() {
    while (1) {
        cin >> w >> h;
        if (w == 0) {
            break;
        }
        for (int i = 0; i < 22; i++) {
            fill(bo[i], bo[i] + 22, true);
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                int num;
                cin >> num;
                if (num == 1) {
                    bo[i][j] = false;
                } else if (num == 2) {
                    st[0] = i;
                    st[1] = j;
                } else if (num == 3) {
                    ed[0] = i;
                    ed[1] = j;
                }
            }
        }
        ll ans = DFS(0, st[0], st[1]);
        cout << ans << endl;
    }
}



Measured memory_bytes: 3052