Easy Java Union Find Solution
\npublic class Solution {\n    public int findCircleNum(int[][] M) {\n        int count = M.length;\n        int[] root = new int[M.length];\n        for(int i=
```\npublic class Solution {\n    public int findCircleNum(int[][] M) {\n        int count = M.length;\n        int[] root = new int[M.length];\n        for(int i=0;i<M.length;i++){\n            root[i] =i;\n        }\n        for(int i=0;i<M.length;i++){\n            for(int j=0;j<M[0].length;j++){\n                if(M[i][j]==1){\n                    int rooti = findRoot(root,i);\n                    int rootj = findRoot(root,j);\n                    if(rooti!=rootj){\n                        root[rooti] = rootj;\n                        count--;\n                    }\n                }\n            }\n        }\n        return count;\n    }\n    public int findRoot(int[] roots,int id){\n        while(roots[id]!=id){\n            roots[id] = roots[roots[id]];\n            id = roots[id];\n        }\n        return id;\n    }\n}\n```
[]