One sheep... two sheep...
InterviewTime limit1sMemory limit256 MB
Count the groups of # cells connected up, down, left, or right in each test grid.
Problem
A while ago I could not sleep. I would lie awake staring at the ceiling until morning. One day I told my friend Gwangmin about it, and he said, "Just count sheep!"
I thought that was useless advice. But the next time I lay down and tried to fall asleep, I caught myself counting sheep anyway. Then it occurred to me that I could turn this into a program, so I got out of bed and sat down at the computer.
A sheep is written as # and grass is written as .. A meadow full of grazing sheep then becomes a grid. If two sheep cells touch each other up, down, left, or right, they belong to the same flock. Two cells that touch only at a corner belong to different flocks. One connected blob of # cells is one flock, and a single # with no sheep next to it still counts as a flock of its own.
By the time the grid was drawn I was getting sleepy, but I had to know how many flocks were on it. Given the grid, count the flocks.
Input
The first line contains the number of test cases .
The first line of each test case contains the grid height and the grid width , separated by a space. The next lines each contain one string of length . Every character is either # for a sheep or . for grass.
Output
For each test case, print the number of sheep flocks in the grid on its own line, in the order the test cases are given.