https://pintia.cn/problem-sets/994805046380707840/exam/problems/1649748772841508875?type=7&page=1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef vector<long long> VI;
#define int long long
#define INF 0x3f3f3f3f
#define endl '\n'
#define N 200010
const int mod = 1e9 + 7;
ll ksm(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1LL) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x) {
return -x & x;
}
int p[N], si[N];
int find(int x) {
if (x == p[x])return p[x];
p[x] = find(p[x]);
return p[x];
}
//size[find(b)] += size[find(a)];
//p[find(a)] = find(b);
int n, m, ok;
vector<int> vis[N];
vector<char> g[N];
int res, ans;
int dx[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int u, int v) {
vis[u][v] = 1;
if (g[u][v] > '1') {
ok = 1;
}
for (int i = 0; i < 4; i++) {
int x = u + dx[i][0], y = v + dx[i][1];
if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || g[x][y] == '0')continue;
dfs(x, y);
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j ++) {
char ch;
cin >> ch;
g[i].push_back(ch);
vis[i].push_back(0);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!vis[i][j] && g[i][j] > '0') {
ok = 0;
res ++;
dfs(i, j);
// cout << i << " " << j << endl;
if (ok)ans ++;
}
}
}
cout << res << " " << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
solve();
return 0;
}