Simple DFS problem.

It can also be solved without using dfs. TODO.