Language: C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define MAX 20

#define FLOOR 0
#define BLOCK 1
#define START 2
#define GOAL 3
#define OUT 4

#define UP 0
#define DOWN 1
#define RIGHT 2
#define LEFT 3
#define VARIATION  4

#define INFTY 100//>MAX

using namespace std;

int H,W;
//grid[0][i],grid[MAX+1][i],grid[i][0],grid[i][MAX+1]は番兵
int grid[MAX+3][MAX+3];

int dfs(int row,int column,int direction,int phase){
	if(grid[row][column] == FLOOR){
		if(direction == UP)return dfs(row,column+1,UP,phase);
		if(direction == DOWN)return dfs(row,column-1,DOWN,phase);
		if(direction == RIGHT)return dfs(row+1,column,RIGHT,phase);
		if(direction == LEFT)return dfs(row-1,column,LEFT,phase);
	}
	else if(grid[row][column] == BLOCK){
		phase++;
		if(phase > 10)return INFTY;
		grid[row][column] = FLOOR;
		if(direction == UP)column--;
		else if(direction == DOWN)column++;
		else if(direction == RIGHT)row--;
		else if(direction == LEFT)row++;
		else exit(1);

		if(grid[row][column] == GOAL)return phase;

		if(grid[row][column+1]!=BLOCK)cnt = min(cnt,dfs(i,j,UP,0));
		if(grid[row][column-1]!=BLOCK)cnt = min(cnt,dfs(i,j,DOWN,0));
		if(grid[row+1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,RIGHT,0));
		if(grid[row-1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,LEFT,0));

	}
	else if(grid[row][column] == GOAL)return phase+1;
	else if(grid[row][column] == OUT)return INFTY;
}


//スタート地点を見つける
void solve(){
	int cnt = INFTY;
	for(int i = 1;i <= H;i++){
		for(int j = 1;j <= W;j++){
			if(grid[i][j] == START){
				grid[i][j] = FLOOR;
				if(grid[i][j+1]!=BLOCK)cnt = min(cnt,dfs(i,j,UP,0));
				if(grid[i][j-1]!=BLOCK)cnt = min(cnt,dfs(i,j,DOWN,0));
				if(grid[i+1][j]!=BLOCK)cnt = min(cnt,dfs(i,j,RIGHT,0));
				if(grid[i-1][j]!=BLOCK)cnt = min(cnt,dfs(i,j,LEFT,0));
			}
		}
	}
	if(cnt == INFTY)cout <<-1 <<endl;
	else cout <<cnt <<endl;
}

int main() {
	char mark;
	scanf("%d %d",&W,&H);//幅，高さの順に入力される

	while(!(H == 0 && W == 0)){
	
		for(int i = 0; i <= H+1;i++){
			for(int j = 0;j <= W+1;j++) grid[i][j] = OUT;
		}

		for(int h = 1;h <= H;h++){
			for(int w = 1;w <= W;w++) cin >>grid[h][w];//1文字ずつ読み込み
		}

		solve();
		scanf("%d %d",&H,&W);
	}
   
    return 0;
}

Compiler output:
a.cc: In function 'int dfs(int, int, int, int)':
a.cc:47:47: error: 'cnt' was not declared in this scope; did you mean 'int'?
   47 |                 if(grid[row][column+1]!=BLOCK)cnt = min(cnt,dfs(i,j,UP,0));
      |                                               ^~~
      |                                               int
a.cc:47:65: error: 'i' was not declared in this scope
   47 |                 if(grid[row][column+1]!=BLOCK)cnt = min(cnt,dfs(i,j,UP,0));
      |                                                                 ^
a.cc:47:67: error: 'j' was not declared in this scope; did you mean 'jn'?
   47 |                 if(grid[row][column+1]!=BLOCK)cnt = min(cnt,dfs(i,j,UP,0));
      |                                                                   ^
      |                                                                   jn
a.cc:48:47: error: 'cnt' was not declared in this scope; did you mean 'int'?
   48 |                 if(grid[row][column-1]!=BLOCK)cnt = min(cnt,dfs(i,j,DOWN,0));
      |                                               ^~~
      |                                               int
a.cc:48:65: error: 'i' was not declared in this scope
   48 |                 if(grid[row][column-1]!=BLOCK)cnt = min(cnt,dfs(i,j,DOWN,0));
      |                                                                 ^
a.cc:48:67: error: 'j' was not declared in this scope; did you mean 'jn'?
   48 |                 if(grid[row][column-1]!=BLOCK)cnt = min(cnt,dfs(i,j,DOWN,0));
      |                                                                   ^
      |                                                                   jn
a.cc:49:47: error: 'cnt' was not declared in this scope; did you mean 'int'?
   49 |                 if(grid[row+1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,RIGHT,0));
      |                                               ^~~
      |                                               int
a.cc:49:65: error: 'i' was not declared in this scope
   49 |                 if(grid[row+1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,RIGHT,0));
      |                                                                 ^
a.cc:49:67: error: 'j' was not declared in this scope; did you mean 'jn'?
   49 |                 if(grid[row+1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,RIGHT,0));
      |                                                                   ^
      |                                                                   jn
a.cc:50:47: error: 'cnt' was not declared in this scope; did you mean 'int'?
   50 |                 if(grid[row-1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,LEFT,0));
      |                                               ^~~
      |                                               int
a.cc:50:65: error: 'i' was not declared in this scope
   50 |                 if(grid[row-1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,LEFT,0));
      |                                                                 ^
a.cc:50:67: error: 'j' was not declared in this scope; did you mean 'jn'?
   50 |                 if(grid[row-1][column]!=BLOCK)cnt = min(cnt,dfs(i,j,LEFT,0));
      |                                                                   ^
      |                                                                   jn
a.cc:55:1: warning: control reaches end of non-void function [-Wreturn-type]
   55 | }
      | ^