using namespace std; #define MAXN 105 int dx[4] = {0, -1, 0, 1}; int dy[4] = {-1, 0, 1, 0}; int mapp[MAXN][MAXN]; int m, n;
void dfs(int x, int y) { mapp[x][y] = -1; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < m && ny >= 0 && ny <= n && mapp[nx][ny] == 1) { mapp[nx][ny] == -1; dfs(nx, ny); } } }
int main() { int T; scanf("%d\n", &T); while (T--) { int ans = 0; scanf("%d%d", &m, &n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &mapp[i][j]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (mapp[i][j] == 1) { dfs(i, j); ans++; } } } printf("%d\n", ans); } return 0; }