LeetCode gives you isConnected, which is an adjacency matrix. isConnected[i][j] = 1 means there is an edge between cities and . You need to convert this to DFS logic.
Instead of building an adjacency list, you can DFS directly on the matrix. When exploring neighbors of city , loop through all cities from to . If isConnected[u][v] = 1 and is not visited, recurse on . This works fine for small (up to a few hundred). For large graphs, adjacency lists are faster.