Oneliners :-P
Solution 1, using a SciPy function:\n\nimport scipy.sparse\n\nclass Solution(object):\n    def findCircleNum(self, M):\n        return scipy.sparse.csgraph.conn
Solution 1, using a SciPy function:\n```\nimport scipy.sparse\n\nclass Solution(object):\n    def findCircleNum(self, M):\n        return scipy.sparse.csgraph.connected_components(M)[0]\n```\n\nSolution 2, compute the transitive closure of the (boolean) matrix and count the number of different rows:\n```\nimport numpy as np\n\nclass Solution(object):\n    def findCircleNum(self, M):\n        return len(set(map(tuple, (np.matrix(M, dtype='bool')**len(M)).A)))\n```
[]