#P1767. Family Count on Isolated Island
Family Count on Isolated Island
Family Count on Isolated Island
On a secluded island, a curious phenomenon occurs: households belonging to the same family are always adjacent (neighbors in the north, south, east, and west directions). However, households of different families are always separated by rivers or hills. Note that households of the same family do not necessarily have the same surname. You are given a map of the island with \( n \) rows, where each row consists of characters. Each cell is either a space (representing the sea), an asterisk (\(\ast\)) representing obstacles like rivers or hills, or a lowercase letter representing a household's surname.
Your task is to count the number of different families on the island. A family is defined as a connected component (via up, down, left, and right moves) of cells that contain letters.
inputFormat
The first line of input contains an integer \( n \), the number of rows in the grid. Each of the following \( n \) lines contains a string representing a row of the map. Each character is either a space (representing the sea), an asterisk (\(\ast\)) representing an obstacle, or a lowercase letter.
outputFormat
Output a single integer: the number of families on the island.
sample
3
ab
a
c
2