```
void dfs(int i, vector<vector<int>>& M)
{
M[i][i] = 2;
for(int j=0; j<M.size(); ++j)
if(i != j && M[j][j] == M[i][j])
dfs(j, M);
}
int findCircleNum(vector<vector<int>>& M) {
int circles = 0;
for(int i=0; i<M.size(); ++i)
{
if(M[i][i] == 1)
{
circles++;
dfs(i, M);
}
}
return circles;
}
```