Java BFS - Equivalent to Finding Connected Components in a Graph
\npublic int findCircleNum(int[][] M) {\n    int count = 0;\n    for (int i=0; i<M.length; i++)\n        if (M[i][i] == 1) { count++; BFS(i, M); }\n    return c
```\npublic int findCircleNum(int[][] M) {\n    int count = 0;\n    for (int i=0; i<M.length; i++)\n        if (M[i][i] == 1) { count++; BFS(i, M); }\n    return count;\n}\n\npublic void BFS(int student, int[][] M) {\n    Queue<Integer> queue = new LinkedList<>();\n    queue.add(student);\n    while (queue.size() > 0) {\n        int queueSize = queue.size();\n        for (int i=0;i<queueSize;i++) {\n            int j = queue.poll();\n            M[j][j] = 2; // marks as visited\n            for (int k=0;k<M[0].length;k++) \n                if (M[j][k] == 1 && M[k][k] == 1) queue.add(k);\n        }\n    }\n}\n```
[]