Solution 1, using a SciPy function:

```
import scipy.sparse
class Solution(object):
def findCircleNum(self, M):
return scipy.sparse.csgraph.connected_components(M)[0]
```

Solution 2, compute the transitive closure of the (boolean) matrix and count the number of different rows:

```
import numpy as np
class Solution(object):
def findCircleNum(self, M):
return len(set(map(tuple, (np.matrix(M, dtype='bool')**len(M)).A)))
```