www.acmicpc.net/problem/1012
#include <stdio.h>
int arr[52][52] = { 0 };
void dfs(int x, int y)
{
arr[y][x] = 0;
if (arr[y][x+1] == 1)
dfs(x + 1, y);
if (arr[y][x - 1] == 1)
dfs(x - 1, y);
if (arr[y+1][x] == 1)
dfs(x, y + 1);
if (arr[y-1][x] == 1)
dfs(x, y - 1);
}
int main(void)
{
int l, k, i, j, x, y, a, b, testcase, num, cnt=0;
scanf("%d", &testcase);
for (k = 0; k < testcase; k++)
{
scanf("%d %d %d", &x, &y, &num);
for (l = 0; l < num; l++)
{
scanf("%d %d", &a, &b);
arr[b][a] = 1;
}
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
if (arr[j][i] == 1)
{
dfs(i, j);
cnt++;
}
}
}
printf("%d\n", cnt);
cnt = 0;
}
}