```
I would like to use the DFS to connect the graphs and count how many graphs this matrix has, but it indicates stack overflow
public class Solution {
public int findCircleNum(int[][] M) {
if(M == null || M.length == 0){
return 0;
}
boolean[][] visit = new boolean[M.length][M[0].length];
int count = 0;
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[0].length; j++){
if(M[i][j] == 1 && visit[i][j] == false){
help(i, j, visit, M);
count++;
}
}
}
return count;
}
void help(int i, int j, boolean[][] visit, int[][] M){
if(i < 0 || j < 0 || i >= visit.length || j >= visit.length || M[i][j] == 0){
return;
}
visit[i][j] = true;
help(i - 1, j, visit, M);
help(i + 1, j, visit, M);
help(i, j - 1, visit, M);
help(i, j + 1, visit, M);
return;
}
```

}