def bfs(y, x): q = [] q.append([y, x]) check[y][x] = True
while q: cntY, cntX = q.pop()
for i in range(4): ny = cntY + dy[i] nx = cntX + dx[i]
if 0 > ny or ny >= col or 0 > nx or nx >= row: continue
if check[ny][nx] == False and field[ny][nx] == 1: q.append([ny, nx]) check[ny][nx] = True
test_case = int(input()) row = 0 col = 0 k = 0 field = [] check = [] dy = [1, -1, 0, 0] dx = [0, 0, 1, -1]
for t in range(test_case): row, col, k = map(int, input().split()) field = [[0] * row for _ in range(col)] check = [[False] * row for _ in range(col)] count = 0
for i in range(k): x, y = map(int, input().split()) field[y][x] = 1
for i in range(col): for j in range(row): if check[i][j] == False and field[i][j] == 1: count += 1 bfs(i, j)
print(count)
|