Number of Provinces
You are given an n x n **adjacency matrix** isConnected describing direct connections between n cities. isConnected[i][j] === 1 means city i and city j are directly connected, and isConnected[i][j] === 0 means they are not.
A **province** is a group of cities that are connected either directly or indirectly (through other cities), where no city outside the group is connected to any city inside it — in graph terms, a connected component.
Return the total number of provinces.
The matrix is symmetric (isConnected[i][j] === isConnected[j][i]) and every city is connected to itself (isConnected[i][i] === 1).
Example cases
- two provincesin isConnected =110110001out 2Cities 0 and 1 are directly connected, forming one province. City 2 is isolated, forming a second.
- all isolatedin isConnected =100010001out 3No city is connected to any other, so each of the 3 cities is its own province.
- indirect connectionin isConnected =110111011out 10–1 and 1–2 are directly connected, so 0 and 2 are connected indirectly through 1 — one province.
Constraints
- 1 <= n <= 200
- n === isConnected.length === isConnected[i].length
- isConnected[i][j] is 0 or 1
- isConnected[i][i] === 1
- isConnected[i][j] === isConnected[j][i]
isConnected =
[[1,1,0],[1,1,0],[0,0,1]]