Similar to the island problem, we sink all the numbers that we found so far.

```
class Solution(object):
def findCircleNum(self, M):
"""
:type M: List[List[int]]
:rtype: int
"""
m=len(M)
count=0
def sink(i):
M[i][i]=0
for j in range(m):
if M[i][j]==1 and M[j][j]==1:
sink(j)
for i in range(m):
if M[i][i]==1:
sink(i)
count+=1
return count
```