Cabbages are planted in a field with width M and height N.
Each cabbage occupies a 1 ร 1 cell, and cells without a cabbage are marked 0.
Cells containing a cabbage are marked 1.
You want to place cabbage worms in the field to protect the cabbages from pests.
A single worm can care for all adjacent cabbages in its group.
Write a program to find the minimum number of worms required.
The first line gives the number of test cases T.
For each case, the first line gives the field's width M, height N, and number of planted cabbage positions K, in that order.
The next K lines give the cabbage positions.
Each position is (x, y), where x is the horizontal coordinate and y is the vertical coordinate.
(0 โค x < M, 0 โค y < N)
Input size: MxN<=50x50=2500,K<=2500
Worst case: All 2,500 cabbages are isolated โ BFS is started O(K) times.
Work inside BFS: Each cell checks at most four neighbors โ O(K ร 4) = O(K).
Total time complexity: O(K).
Additional test cases, excluding the examples ๐