Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Since every group of farmland is rectangular, the top left corner of each group will have the smallest x-coordinate and y-coordinate of any farmland in the group.
Similarly, the bottom right corner of each group will have the largest x-coordinate and y-coordinate of any farmland in the group.
Use DFS to traverse through different groups of farmlands and keep track of the smallest and largest x-coordinate and y-coordinates you have seen in each group.