#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
/*
* Complete the 'countGroups' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING_ARRAY related as parameter.
*/
vector<int> g[200020];
int vis[200020];
void dfs(int x) {
vis[x] = 1;
for (auto y: g[x]) if (vis[y] == 0) dfs(y);
}
int countGroups(vector<string> related) {
for (int i = 0; i <= related.size(); i++)
g[i].clear(), vis[i] = 0;
for (int i = 0; i < related.size(); i++)
for (int j = 0; j < related.size(); j++)
if (related[i][j] == '1') g[i].push_back(j);
int ans = 0;
for (int i = 0; i < related.size(); i++)
if (vis[i] == 0) {
dfs(i);
ans++;
}
return ans;
}
int main()
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIApzdHJpbmcgbHRyaW0oY29uc3Qgc3RyaW5nICYpOwpzdHJpbmcgcnRyaW0oY29uc3Qgc3RyaW5nICYpOwogCiAKLyoKICogQ29tcGxldGUgdGhlICdjb3VudEdyb3VwcycgZnVuY3Rpb24gYmVsb3cuCiAqCiAqIFRoZSBmdW5jdGlvbiBpcyBleHBlY3RlZCB0byByZXR1cm4gYW4gSU5URUdFUi4KICogVGhlIGZ1bmN0aW9uIGFjY2VwdHMgU1RSSU5HX0FSUkFZIHJlbGF0ZWQgYXMgcGFyYW1ldGVyLgogKi8KIAp2ZWN0b3I8aW50PiBnWzIwMDAyMF07CmludCB2aXNbMjAwMDIwXTsKIAp2b2lkIGRmcyhpbnQgeCkgewogICAgdmlzW3hdID0gMTsKICAgIGZvciAoYXV0byB5OiBnW3hdKSBpZiAodmlzW3ldID09IDApIGRmcyh5KTsKfQogCmludCBjb3VudEdyb3Vwcyh2ZWN0b3I8c3RyaW5nPiByZWxhdGVkKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8PSByZWxhdGVkLnNpemUoKTsgaSsrKSAKICAgICAgICBnW2ldLmNsZWFyKCksIHZpc1tpXSA9IDA7CiAK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