Subtask 1
We have n≤10, and so we can afford to try every possible set of walls. For each such set, we can check the (non-)connectivity requirements using a BFS or DFS algorithm: starting from some hiking areas, and making sure not to wa