python dfs iter version
recursion version is easier to understand, but the iterate version shows up in my mind firstly.\n\nclass Solution(object):\n    def findCircleNum(self, M):\n   
recursion version is easier to understand, but the iterate version shows up in my mind firstly.\n```\nclass Solution(object):\n    def findCircleNum(self, M):\n        """\n        :type M: List[List[int]]\n        :rtype: int\n        """\n        circle = 0\n        n = len(M)\n        for i in xrange(n):\n            if M[i][i] != 1:\n                continue\n            friends = [i]\n            while friends:\n                f = friends.pop()\n                if M[f][f] == 0:\n                    continue\n                M[f][f] = 0\n                for j in xrange(n):\n                    if M[f][j] == 1 and M[j][j] == 1:\n                        friends.append(j)\n            circle += 1\n        return circle\n```\nother people have already posted recursion solutions, but here I still paste mine:\n```\nclass Solution(object):\n    def findCircleNum(self, M):\n        """\n        :type M: List[List[int]]\n        :rtype: int\n        """\n        def dfs(node):\n            visited.add(node)\n            for friend in xrange(len(M)):\n                if M[node][friend] and friend not in visited:\n                    dfs(friend)\n\n        circle = 0\n        visited = set()\n        for node in xrange(len(M)):\n            if node not in visited:\n                dfs(node)\n                circle += 1\n        return circle\n```
[]