let visit = [];
const dfs = (graph, start) => {
let stack = [start];
visit.push(start);
while (stack.length > 0) {
const node = stack.pop();
for (const [i, n] of graph[node].entries()) {
if (n === 1 && !visit.includes(i)) {
visit.push(i);
stack.push(i);
}
}
}
};
function solution(n, computers) {
let answer = 0;
for (let i=0; i<n; i++) {
for (let j=0; j<n; j++) {
if (computers[i][j] === 1 && !visit.includes(j)) {
dfs(computers, j);
answer++;
}
}
}
return answer;
}