Python DFS solution (Explained in detail with analogy)
This is based on one of the approaches listed at: https://leetcode.com/problems/friend-circles/discuss/303150/python-union-find-dfs-bfs\n\nclass Solution:\n    
This is based on one of the approaches listed at: https://leetcode.com/problems/friend-circles/discuss/303150/python-union-find-dfs-bfs\n```\nclass Solution:\n    def findCircleNum(self, M: List[List[int]]) -> int:\n        #We\'re gonna imitate a DFS appraoch to this.\n        #If we visit a row (person), look for every col index that\'s 1 and explore those partcular rows(people). The number of independent rows you need to search is the result. \n        rows=len(M)\n        seen=set()\n        #dfs() enables the exploring\n        def dfs(r):\n            for c,val in enumerate(M[r]):\n                if val==1 and c not in seen:\n                    seen.add(c)\n                    dfs(c)\n        #Now start exploring\n        count=0\n        for i in range(rows):\n            if i not in seen:\n                dfs(i)\n                count+=1\n        return count\n        \n```\n\nThe key here is to realize that the number of friend circles is the number of rows in the matrix(or groups of people in the school) that are distinct from other groups of people. \nTo do this, imagine a pen on a piece of paper. You\'ve heard of the challenge to not take off the pen from the sheet and yet trace a figure. \n\nIn our problem, this is like jumping from one row(person) to other rows(other people) based on which col indices are "1". Thus we dfs() from one row to another to another until no "1" references any other row that we have not yet "seen". Hence why we need the "seen" set- to make sure we dont infintely explore the same rows. \nEvery now and then we\'ll reach the end of a dfs traversal ansd still have rows we have not yet explored. \n\nThis why we need the for loop that goes through every row (with a check to see if we\'ve "seen" the row yet). This way, every row will be explored and the number of times we need to take the pen off the paper( or the num of times we need to let the for loop do a dfs() call) is the number of distinct friend groups. As usual, be sure to point out any errors in my analysis. \nVoila ;)
['Depth-First Search', 'Python']