2 Solutions (BFS & UnionFind)
\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int cities = isConnected.length;\n        int provinces = 0;\n        \n       
```\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int cities = isConnected.length;\n        int provinces = 0;\n        \n        boolean[] visited = new boolean[cities];\n        Queue<Integer> q = new LinkedList();\n        for(int i = 0; i < cities; i++)\n        {\n            if(!visited[i])\n            {\n                q.offer(i);\n                provinces++;\n            }\n            while(!q.isEmpty())\n            {\n                int current = q.poll();\n                visited[current] = true;\n                for(int j = 0; j < cities; j++)\n                {\n                    if(j != current && isConnected[current][j] == 1 && !visited[j])\n                        q.offer(j);\n                }\n            } \n        }\n        \n        return provinces;\n    }\n\n}\n```\nUnionFind Solution to perform Union between related Cities.\n\n```\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int cities = isConnected.length;\n        UnionFind uf = new UnionFind(cities);\n        boolean[] visited = new boolean[cities];\n        for(int i = 0; i < cities; i++)\n        {\n            visited[i] = true;\n            for(int j = 0; j < cities; j++)\n            {\n                if(i != j && !visited[j] && isConnected[i][j] == 1)\n                    uf.Union(i, j);\n            }\n        }\n        \n        return uf.provinces();\n    }\n    class UnionFind{\n        int components;\n        int[] parent;\n        \n        public UnionFind(int len)\n        {\n            components = len;\n            parent = new int[len];\n            \n            for(int i = 0; i < len; i++)\n                parent[i] = i;\n        }\n        \n        private int Find(int node)\n        {\n            if(node != parent[node]) \n                parent[node] = Find(parent[node]);\n            \n            return parent[node];\n        }\n        \n        private void Union(int a, int b)\n        {\n            int parentA = Find(a);\n            int parentB = Find(b);\n            \n            if(parentA == parentB) return;\n            \n            components--;\n            parent[parentA] = parentB;\n        }\n        \n        private int provinces()\n        {\n            return components;\n        }\n    }\n}\n```
['Breadth-First Search', 'Union Find', 'Java']