I pretty much have the same code, but when i try to run it, for the last couple of test cases it says time limit exceeded. Can you help me out by explaining where i am going wrong?

public int solution(int[][] M) { int count = 0; List<Integer> visited = new ArrayList<>(); List<Integer> overall; for(int i=0;i<M.length;i++) { if(!visited.contains(i)) { visited.add(i); overall = recurse(i,visited,M); overall.remove(visited); visited.addAll(overall); count++; } } return count; } public List<Integer> recurse(int visit, List<Integer> visited, int[][] m) { for (int j = 0; j < m.length; j++) { if (m[visit][j] == 1 && !visited.contains(j)) { visited.add(j); recurse(j,visited,m); } } return visited; }Friend Circles