```
from collections import deque
class Solution(object):
def findCircleNum(self, M):
"""
:type M: List[List[int]]
:rtype: int
"""
cnt = 0
Q = deque()
N = len(M)
for i in range(N):
for j in range(N):
if M[i][j] == 1:
cnt += 1
M[i][j] = 0
Q.append(j)
while Q:
tmp = Q.popleft()
for t in range(N):
if M[tmp][t] == 1:
Q.append(t)
M[tmp][t] = 0
return cnt
```