Language: C

tdio.h>          // printf(), fprintf(), scanf()
#include <stdlib.h>         // exit()
#include <stdbool.h>

#define SZ_FIELD 100
#define QUE_SIZE 10000

typedef struct que_tbl
{
	int x;
	int y;
} que_t;

int N, M;
char field[SZ_FIELD][SZ_FIELD + 1];
que_t queue[QUE_SIZE];
int que_in;
int que_out;

void
cleanup(int ecode)
{
	exit(ecode);
}

int
que_empty()
{
	return que_in == que_out;
}

void
que_push(que_t *que)
{
	queue[que_in].x = que->x;
	queue[que_in].y = que->y;
	que_in = (que_in + 1) % QUE_SIZE;

	if (que_in == que_out)
	{
		fprintf(stderr, "Queue overflow. size:%d\n", QUE_SIZE);
		cleanup(1);
	}
}

void
que_pop()
{
	if (que_out == que_in)
	{
		fprintf(stderr, "Queue underflow\n");
		cleanup(1);
	}

	que_out = (que_out + 1) % QUE_SIZE;
}

void
que_front(que_t *que)
{
	que->x = queue[que_out].x;
	que->y = queue[que_out].y;
}

void
dfs(int x, int y)
{
	field[y][x] = '.';
	for (int dy = -1; dy <= 1; ++dy)
	{
		for (int dx = -1; dx <= 1; ++dx)
		{
			int nx = x + dx, ny = y + dy;
			if (0 <= nx && nx < N && 0 <= ny && ny < M && field[ny][nx] == '#')
				dfs(nx, ny);
		}
	}
}

bool
bfs(int *cx, int *cy)
{
	const int dx[4] = {-1, 0, 1, 0};
	const int dy[4] = { 0,-1, 0, 1};
	int mx = 0, my = 0;
	que_t que;

	que_in = 0;
	que_out = 0;

	que.x = *cx, que.y = *cy;
	que_push(&que);

	while (!que_empty())
	{
		que_front(&que); que_pop();
		int x = que.x, y = que.y;
		if (x == 0 || x == N - 1 || y == 0 || y == M - 1)
			return true;

		for (int i = 0; i < 4; ++i)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if (0 <= nx && nx < N && 0 <= ny && ny < M)
			{
				if (field[ny][nx] == '.')
				{
					field[ny][nx] = ' ';
					que.x = nx, que.y = ny;
					que_push(&que);
				}
				else if (field[ny][nx] == '#')
				{
					if (nx > mx || ny > my)
						mx = nx, my = ny;
				}
			}
		}
	}

	*cx = mx, *cy = my;
	return false;
}

int
solve()
{
	int cx, cy;

	for (cy = 0; cy < M; ++cy)
	{
		for (cx = 0; cx < N; ++cx)
		{
			if (field[cy][cx] == '&')
				break;
		}

		if (cx < N)
			break;
	}

	field[cy][cx] = ' ';
	int count = 0;
	while (!bfs(&cx, &cy))
	{
		dfs(cx, cy);
		count++;
	}

	return count;
}

int
main(int argc, char **argv)
{
	while (true)
	{
		scanf("%d%d", &N, &M);
		if (N == 0 && M == 0)
			break;

		for (int i = 0; i < M; ++i)
			scanf("%s", &field[i][0]);

		printf("%d\n", solve());
	}

	return 0;
}


Compiler output:
main.c:1:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
    1 | tdio.h>          // printf(), fprintf(), scanf()
      |     ^
In file included from main.c:2:
/usr/include/stdlib.h:98:8: error: unknown type name 'size_t'
   98 | extern size_t __ctype_get_mb_cur_max (void) __THROW __wur;
      |        ^~~~~~
/usr/include/stdlib.h:57:1: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
   56 | #include <bits/floatn.h>
  +++ |+#include <stddef.h>
   57 | 
/usr/include/stdlib.h:531:25: error: unknown type name 'size_t'
  531 |                         size_t __statelen) __THROW __nonnull ((2));
      |                         ^~~~~~
/usr/include/stdlib.h:531:25: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:561:25: error: unknown type name 'size_t'
  561 |                         size_t __statelen,
      |                         ^~~~~~
/usr/include/stdlib.h:561:25: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:661:42: error: unknown type name 'size_t'
  661 | extern void arc4random_buf (void *__buf, size_t __size)
      |                                          ^~~~~~
/usr/include/stdlib.h:661:42: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:672:22: error: unknown type name 'size_t'
  672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |                      ^~~~~~
/usr/include/stdlib.h:672:22: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:675:22: error: unknown type name 'size_t'
  675 | extern void *calloc (size_t __nmemb, size_t __size)
      |                      ^~~~~~
/usr/include/stdlib.h:675:22: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:675:38: error: unknown type name 'size_t'
  675 | extern void *calloc (size_t __nmemb, size_t __size)
      |                                      ^~~~~~
/usr/include/stdlib.h:675:38: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:683:36: error: unknown type name 'size_t'
  683 | extern void *realloc (void *__ptr, size_t __size)
      |                                    ^~~~~~
/usr/include/stdlib.h:683:36: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:695:41: error: unknown type name 'size_t'
  695 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                         ^~~~~~
/usr/include/stdlib.h:695:41: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:695:57: error: unknown type name 'size_t'
  695 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                                         ^~~~~~
/usr/include/stdlib.h:695:57: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:701:41: error: unknown type name 'size_t'
  701 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                         ^~~~~~
/usr/include/stdlib.h:701:41: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:701:57: error: unknown type name 'size_t'
  701 | extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
      |                                                         ^~~~~~
/usr/include/stdlib.h:701:57: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
In file included from /usr/include/stdlib.h:706:
/usr/include/alloca.h:32:22: error: unknown type name 'size_t'
   32 | extern void *alloca (size_t __size) __THROW;
      |                      ^~~~~~
/usr/include/alloca.h:25:1: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
   24 | #include <stddef.h>
  +++ |+#include <stddef.h>
   25 | 
/usr/include/stdlib.h:712:22: error: unknown type name 'size_t'
  712 | extern void *valloc (size_t __size) __THROW __attribute_malloc__
      |                      ^~~~~~
/usr/include/stdlib.h:712:22: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:718:45: error: unknown type name 'size_t'
  718 | extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
      |                                             ^~~~~~
/usr/include/stdlib.h:718:45: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:718:65: error: unknown type name 'size_t'
  718 | extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
      |                                                                 ^~~~~~
/usr/include/stdlib.h:718:65: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:724:29: error: unknown type name 'size_t'
  724 | extern void *aligned_alloc (size_t __alignment, size_t __size)
      |                             ^~~~~~
/usr/include/stdlib.h:724:29: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:724:49: error: unknown type name 'size_t'
  724 | extern void *aligned_alloc (size_t __alignment, size_t __size)
      |                                                 ^~~~~~
/usr/include/stdlib.h:724:49: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:961:23: error: unknown type name 'size_t'
  961 |                       size_t __nmemb, size_t __size, __compar_fn_t __compar)
      |                       ^~~~~~
/usr/include/stdlib.h:961:23: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:961:39: error: unknown type name 'size_t'
  961 |                       size_t __nmemb, size_t __size, __compar_fn_t __compar)
      |                                       ^~~~~~
/usr/include/stdlib.h:961:39: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:970:34: error: unknown type name 'size_t'
  970 | extern void qsort (void *__base, size_t __nmemb, size_t __size,
      |                                  ^~~~~~
/usr/include/stdlib.h:970:34: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:970:50: error: unknown type name 'size_t'
  970 | extern void qsort (void *__base, size_t __nmemb, size_t __size,
      |                                                  ^~~~~~
/usr/include/stdlib.h:970:50: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1044:20: error: unknown type name 'size_t'
 1044 |                    size_t __len) __THROW __nonnull ((3, 4, 5));
      |                    ^~~~~~
/usr/include/stdlib.h:1044:20: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1047:20: error: unknown type name 'size_t'
 1047 |                    size_t __len) __THROW __nonnull ((3, 4, 5));
      |                    ^~~~~~
/usr/include/stdlib.h:1047:20: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1051:45: error: unknown type name 'size_t'
 1051 |                     char *__restrict __buf, size_t __len)
      |                                             ^~~~~~
/usr/include/stdlib.h:1051:45: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1055:45: error: unknown type name 'size_t'
 1055 |                     char *__restrict __buf, size_t __len)
      |                                             ^~~~~~
/usr/include/stdlib.h:1055:45: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1062:36: error: unknown type name 'size_t'
 1062 | extern int mblen (const char *__s, size_t __n) __THROW;
      |                                    ^~~~~~
/usr/include/stdlib.h:1062:36: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1066:48: error: unknown type name 'size_t'
 1066 |                    const char *__restrict __s, size_t __n) __THROW;
      |                                                ^~~~~~
/usr/include/stdlib.h:1066:48: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1073:8: error: unknown type name 'size_t'
 1073 | extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
      |        ^~~~~~
/usr/include/stdlib.h:1073:8: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1074:53: error: unknown type name 'size_t'
 1074 |                         const char *__restrict __s, size_t __n) __THROW
      |                                                     ^~~~~~
/usr/include/stdlib.h:1074:53: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
/usr/include/stdlib.h:1077:8: error: unknown type name 'size_t'
 1077 | extern size_t wcstombs (char *__restrict __s,
      |        ^~~~~~
/usr/include/stdlib.h:1077:8: note: 'size_t' is defined in header '