JavaScript 3 Solutions: Union Find, DFS & BFS
Union Find:\n\nvar findCircleNum = function(M) {\n    let res = 0;\n    const dsu = new DSU(M.length);\n    for(let row = 0; row < M.length; row++) {\n        f
Union Find:\n```\nvar findCircleNum = function(M) {\n    let res = 0;\n    const dsu = new DSU(M.length);\n    for(let row = 0; row < M.length; row++) {\n        for(let col = 0; col < M[0].length; col++) {\n            if(M[row][col] === 1) {\n                dsu.union(row, col);\n            }\n        }\n    }\n    return new Set(M.map((m, i) => dsu.find(i))).size;\n};\n\nclass DSU {\n    constructor(N) {\n        this.parent = [...new Array(N).keys()];\n    }\n    find(x) {\n        if(this.parent[x] !== x) this.parent[x] = this.find(this.parent[x]);\n        return this.parent[x];\n    }\n    union(x, y) {\n        this.parent[this.find(x)] = this.find(y);\n    }\n}\n```\n\nDFS: \n```\nvar findCircleNum = function(M) {\n    const seen = new Set();\n    let res = 0;\n    const dfs = (i) => {\n        for(let j = 0; j < M[0].length; j++) {\n            if(M[i][j] === 1 && !seen.has(j)) {\n                seen.add(j);\n                dfs(j);\n            }\n        }\n    }\n    for(let i = 0; i < M.length; i++) {\n        if(!seen.has(i)) {\n            dfs(i);\n            res++;\n        }\n    }\n\n    return res;\n};\n```\n\nBFS:\n```\nvar findCircleNum = function(M) {\n    const seen = new Set();\n    let res = 0;\n    let stack = [];\n    for(let i = 0; i < M.length; i++) {\n        if(!seen.has(i)) {\n            stack.push(i);\n            while(stack.length) {\n                const curr = stack.pop();\n                seen.add(curr);\n                for(let j = 0; j < M[0].length; j++) {\n                    if(M[curr][j] === 1 && !seen.has(j)) {\n                        stack.push(j);\n                    }\n                }\n            }\n            res++;\n        }\n    }\n    return res;\n};\n```
['Depth-First Search', 'Breadth-First Search', 'Union Find', 'JavaScript']