Amazon Interview Question

bfs/dfs (connected components to solve if wanted)