Easy & Clear Solution Python3
Approach:\n\n1. Initialize a variable res to 0, which will keep track of the total number of provinces.\n2. Get the size of the input matrix isConnected and sto
# Approach:\n\n1. Initialize a variable res to 0, which will keep track of the total number of provinces.\n2. Get the size of the input matrix isConnected and store it in the variable n.\n3. Define a helper function dfs(i) to perform DFS starting from city i.\n4. In the dfs function, mark city i as visited by setting cnx[i][i] to 2.\n5. Iterate through all cities j from 0 to n-1:\n- If city i and city j are directly connected (cnx[i][j] == 1) and city j is not visited (cnx[j][j] != 2), mark city j as visited by setting cnx[j][j] to 2 and recursively call dfs(j).\n6. Iterate through all cities i from 0 to n-1:\n- If city i is not visited (cnx[i][i] == 1), increment res by 1 and call dfs(i) to explore all the cities in the province.\n7. Return the final result res, which represents the total number of provinces.\n# Complexity:\n# Time complexity:\n The algorithm visits each city at most once, so the time complexity is O(n^2), where n is the number of cities.\n# Space complexity: \nThe space complexity is O(n) for the recursive calls in the DFS function.\n\n# Code\n```\nclass Solution:\n    def findCircleNum(self, cnx: List[List[int]]) -> int:\n        res=0\n        n=len(cnx)\n        def dfs(i):\n            cnx[i][i]=2\n            for j in range(0,n):\n                if cnx[i][j]==1:\n                    cnx[i][j]=2\n                    dfs(j)\n        for i in range(0,n):\n            if cnx[i][i]==1:\n                res+=1\n                dfs(i)\n        return res\n\n```
['Depth-First Search', 'Python3']