```
public int FindCircleNum(int[,] M) {
int result = 0;
bool[] visited = new bool[M.GetLength(0)];
for (int i = 0; i <= M.GetLength(0) - 1; i++)
if(!visited[i])
{
DFS(i, M, visited);
result++;
}
return result;
}
private void DFS(int startNode, int[,] graph, bool[] visited)
{
visited[startNode] = true;
for (int i = 0; i <= graph.GetLength(1) - 1; i++)
{
if (startNode == i)
continue;
if (graph[startNode, i] == 1 && !visited[i])
DFS(i, graph, visited);
}
}
```