Language: C++

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> Pr;
typedef tuple<int, int, int> Tp;
#define at(t, i) get<i>(t)

int h, w;
int sx, sy, gx, gy;
char grid[14][14];
int dist[14][14];
int group[14][14];
int size[1024];
bool visit[14][14];

bool search(int x, int y, int d)
{
	if (visit[x][y]) return false;
	if (dist[x][y] > d) return false;
	if (group[x][y] != -1 && size[group[x][y]] <= 1) return false;
	
	if (x == gx && y == gy) return true;
	
	visit[x][y] = true;
	if (group[x][y] != -1) size[group[x][y]]--;
	
	for (int dir = 0; dir < 4; dir++){
		int nx = x + dx[dir];
		int ny = y + dy[dir];
		if (grid[nx][ny] == '#') continue;
		if (visit[nx][ny]) continue;
		if (search(nx, ny, d - 1)) return true;
	}
	
	visit[x][y] = false;
	if (group[x][y] != -1) size[group[x][y]]++;
	
	return false;
}

int solve()
{
	int color = 0;
	for (int i = 1; i <= h; i++){
		for (int j = 1; j <= w; j++){
			if (compl group[i][j]) continue;
			if (grid[i][j] != 'X') continue;
			size[color] = 1;
			group[i][j] = color;
			
			queue<Pr> que;
			for (que.push(Pr(i, j)); que.size(); que.pop()){
				Pr p = que.front();
				int x = p.first, y = p.second;
				for (int dir = 0; dir < 4; dir++){
					int nx = x + dx[dir];
					int ny = y + dy[dir];
					if (group[nx][ny] == -1 && grid[nx][ny] == 'X'){
						group[nx][ny] = color;
						size[color]++;
						que.push(Pr(nx, ny));
					}
				}
			}
			
			size[color] = size[color] / 2 + 1;
			color++;
		}
	}
	
	{
		queue<Tp> que;
		dist[gx][gy] = 0;
		for (que.push(Tp(gx, gy, 0)); que.size(); que.pop()){
			Tp t = que.front();
			int x = at(t, 0), y = at(t, 1), d = at(t, 2);
			for (int dir = 0; dir < 4; dir++){
				int nx = x + dx[dir];
				int ny = y + dy[dir];
				if (grid[nx][ny] == '#') continue;
				if (dist[nx][ny] < INF) continue;
				dist[nx][ny] = d + 1;
				que.push(Tp(nx, ny, d + 1));
			}
		}
	}
	
	for (int d = 1; ; d++){
		if (search(sx, sy, d)) return d;
	}
}

int main()
{
	while (scanf("%d %d", &w, &h), h){
		fill_n(*grid, 14 * 14, '#');
		fill_n(*dist, 14 * 14, INF);
		fill_n(*group, 14 * 14, -1);
		fill_n(size, 1024, 0);
		fill_n(*visit, 14 * 14, false);
		
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf(" %c", &grid[i][j]);
				if (grid[i][j] == 'S'){
					sx = i;
					sy = j;
					grid[i][j] = '.';
				}
				else if (grid[i][j] == 'G'){
					gx = i;
					gy = j;
					grid[i][j] = '.';
				}
			}
		}
		
		printf("%d\n", solve());
	}
}

Compiler output:
a.cc: In function 'bool search(int, int, int)':
a.cc:20:13: error: reference to 'visit' is ambiguous
   20 |         if (visit[x][y]) return false;
      |             ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:6: note:                 'bool visit [14][14]'
   16 | bool visit[14][14];
      |      ^~~~~
a.cc:22:34: error: reference to 'size' is ambiguous
   22 |         if (group[x][y] != -1 && size[group[x][y]] <= 1) return false;
      |                                  ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:26:9: error: reference to 'visit' is ambiguous
   26 |         visit[x][y] = true;
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:6: note:                 'bool visit [14][14]'
   16 | bool visit[14][14];
      |      ^~~~~
a.cc:27:32: error: reference to 'size' is ambiguous
   27 |         if (group[x][y] != -1) size[group[x][y]]--;
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:33:21: error: reference to 'visit' is ambiguous
   33 |                 if (visit[nx][ny]) continue;
      |                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:6: note:                 'bool visit [14][14]'
   16 | bool visit[14][14];
      |      ^~~~~
a.cc:37:9: error: reference to 'visit' is ambiguous
   37 |         visit[x][y] = false;
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:6: note:                 'bool visit [14][14]'
   16 | bool visit[14][14];
      |      ^~~~~
a.cc:38:32: error: reference to 'size' is ambiguous
   38 |         if (group[x][y] != -1) size[group[x][y]]++;
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc: In function 'int solve()':
a.cc:50:25: error: reference to 'size' is ambiguous
   50 |                         size[color] = 1;
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:62:49: error: reference to 'size' is ambiguous
   62 |                                                 size[color]++;
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:68:25: error: reference to 'size' is ambiguous
   68 |                         size[color] = size[color] / 2 + 1;
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:68:39: error: reference to 'size' is ambiguous
   68 |                         size[color] = size[color] / 2 + 1;
      |                                       ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:101:24: error: reference to 'size' is ambiguous
  101 |                 fill_n(size, 1024, 0);
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:15:5: note:                 'int size [1024]'
   15 | int size[1024];
      |     ^~~~
a.cc:102:25: error: reference to 'visit' is ambiguous
  102 |                 fill_n(*visit, 14 * 14, false);
      |                         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:16:6: note:                 'bool visit [14][14]'
   16 | bool visit[14][14];
      |      ^~~~~