-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPP0224.cpp
More file actions
49 lines (46 loc) · 1.24 KB
/
CPP0224.cpp
File metadata and controls
49 lines (46 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#define MAX 10000001
#define ll long long
#define fastsync() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const int mod = 1e9 + 7;
using namespace std;
int posx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int posy[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
void canh(int i, int j, int a[][105], int n, int m) {
a[i][j] = 0;
for (int t = 0; t < 9; t++) {
int canhx = i + posx[t], canhy = j + posy[t];
if (canhx >= 0 && canhx < n && canhy >= 0 && canhy < m && a[canhx][canhy]) {
canh(canhx, canhy, a, n, m);
}
}
}
int main() {
fastsync();
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int a[105][105];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j]) {
++cnt;
canh(i, j, a, n, m);
}
}
}
cout << cnt << endl;
}
return 0;
}