#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> parent;
int find(int n)
{
if( n == parent[n]) return n;
else return parent[n] = find(parent[n]);
}
void merge(int n, int v)
{
n = find(n);
v = find(v);
if(n < v)
parent[v] = find(n);
else if (n > v)
parent[n] = find(v);
}
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
vector<int> p;
for(int i = 0; i<n; ++i)
{
p.push_back(i);
}
parent = p;
for(int i = 0; i < computers.size(); ++i)
{
for(int j = 0; j < computers[i].size(); ++j)
{
if(i == j) continue;
if(computers[i][j] ==1)
{
int n = find(i);
int v = find(j);
if(n != v)
{
merge(i,j);
}
}
}
}
for(int i = 0 ; i<n; ++i)
find(i);
sort(parent.begin(), parent.end());
auto End = unique(parent.begin(), parent.end());
parent.erase(End, parent.end());
answer = parent.size();
return answer;
}