/*
AizuOnline A1144
Title carling
@kankichi573
*/
#include <stdio.h>
#include <limits.h>
#define min(x,y) (((x)<(y))?(x):(y))

int w,h;
int ban[21][21];
//
void dump_ban(int ban[21][21],int y,int x)
{
       int i,j;


       printf("------\n");
       for(i=0;i<h;i++)
       {
               for(j=0;j<w;j++)
               {
                       //printf("%d",ban[j][i]);
                       if(y==i && x==j)
                               printf("\x1b[34m*\x1b[37m");
                       else if (ban[i][j]==1)
                                printf("#");
                       else if (ban[i][j]==3)
                               printf("\x1b[32mG\x1b[37m");
                       else
                               printf(" ");
               }
               printf("\n");
       }
}

void copyban(int dst[][21],int src[][21])
{
         int i,j;

        for(i=0;i<21;i++)
                for(j=0;j<21;j++)
                        dst[i][j]=src[i][j];

}
int solve(int ban[][21],int y,int x,int depth)
{
        int ban_r[21][21];
        int ban_l[21][21];
        int ban_u[21][21];
        int ban_d[21][21];
        int ret,ret_l,ret_r,ret_u,ret_d;
        int xx,yy;


        //dump_ban(ban,y,x);

        if(depth >= 10)
                return(-1);
        ret_l=ret_r=ret_u=ret_d=-1;

        copyban(ban_r,ban); //left
        xx=x;
        while(1)
        {
                if(ban_r[y][xx]==3)
                        return(1);
                if(xx >= w)
                {
                        ret_r=(-1);break;
                }
                if(ban_r[y][xx+1]==1)
                {
                        if(xx==x) //direct shougai:can't move
                                ret_r=-1;
                        else      //vanish shougai
                        {
                                ban_r[y][xx+1]=0;
                                ret_r=solve(ban_r,y,xx,depth+1);
                         
                        }
                        break;
                }
                xx++;
        }
        copyban(ban_l,ban); // right
        xx=x;
        while(1)
        {
                if(ban_l[y][xx]==3)
                        return(1);
                if(xx <= 0)
                {
                        ret=-1;
                        break;
                }
                if(ban_l[y][xx-1]==1)
                {
                        if(xx==x)
                                ret=-1;
                        else
                        {
                                ban_l[y][xx-1]=0;
                                ret_l=solve(ban_l,y,xx,depth+1);
                        }
                        break;
                }
                xx--;
        }

        copyban(ban_u,ban);
        yy=y;
        while(1)
        {
                if(ban_u[yy][x]==3)
                        return(1);
                if(yy <= 0)
                {
                        ret_u=-1;
                        break;
                }
                if(ban_u[yy-1][x]==1)
                {
                        if(yy==y)
                                ret_u=-1;
                        else
                        {
                                ban_u[yy-1][x]=0;
                                ret_u=solve(ban_u,yy,x,depth+1);
                        }
                        break;
                }
                yy--;
        }

        copyban(ban_d,ban);
        yy=y;
        while(1)
        {
                //printf("YY=%d\n",yy);
                if(ban_d[yy][x]==3)
                        return(1);
                if(yy >= h-1)
                {
                        ret_d = -1;
                        break;
                }
                if(ban_d[yy+1][x]==1)
                {
                        if(yy==y)
                                ret_d=-1;
                        else
                        {
                                ban_d[yy+1][x]=0;
                                ret_d=solve(ban_d,yy,x,depth+1);
                        }
                        break;
                }
                yy++;
        }
        if(ret_l==-1 && ret_r==-1 && ret_u==-1 && ret_d==-1)
                return(-1);
        if(ret_r<0)
                ret_r=INT_MAX;
        if(ret_l<0)
                ret_l=INT_MAX;
        if(ret_u<0)
                ret_u=INT_MAX;
        if(ret_d<0)
                ret_d=INT_MAX;
        return(1+min(min(ret_r,ret_l),min(ret_u,ret_d)));
}
main()
{
        int i,j,ret,x,y;

        while(EOF!=scanf("%d %d",&w,&h) && (w||h))
        {
                //printf("WH=%d %d\n",w,h);

                for(i=0;i<h;i++)
                        for(j=0;j<w;j++)
                        {
                                scanf("%d",&(ban[i][j]));
                                if(ban[i][j]==2)
                                {
                                        x=j;y=i;
                                        ban[i][j]==0;
                                }
                        }
                //dump_ban(ban,y,x);
                ret=solve(ban,y,x,0);
                printf("%d\n",ret);
        }
return(0);
}