#include <iostream>
#include <vector>
using namespace std;
int m, n;
vector<vector<int>> lettuce;
vector<vector<bool>> visited;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
void dfs(int x, int y){
visited[x][y] = true;
for(int d = 0; d < 4; d++){
int nx = x + dx[d];
int ny = y + dy[d];
if(nx >= 0 && nx < n && ny >= 0 && ny < m){
if(lettuce[nx][ny] == 1 && !visited[nx][ny]){
dfs(nx, ny);
}
}
}
}
int main(){
int T;
cin >> T;
for(int t = 0; t < T; t++){
int k;
cin >> m >> n >> k;
lettuce = vector<vector<int>>(n, vector<int>(m,0));
visited = vector<vector<bool>>(n, vector<bool>(m,false));
for(int i = 0; i < k; i++){
int x, y;
cin >> x >> y;
lettuce[y][x] = 1;
}
int count = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(lettuce[i][j] == 1 && !visited[i][j]){
dfs(i, j);
count++;
}
}
}
cout << count << "\n";
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: