Problem: Number of Provinces (LeetCode ). You are given cities. Some are directly connected. Find how many provinces exist. A province is a group of directly or indirectly connected cities. Input is an adjacency matrix: isConnected[i][j] = 1 means cities and are connected.
This is a connected components problem. Each province is one component. Run the component counting algorithm: loop through cities, DFS from each unvisited city, count how many times you start a DFS. That is your answer.