#C11246. Minimum Watering Operations

    ID: 40541 Type: Default 1000ms 256MiB

Minimum Watering Operations

Minimum Watering Operations

Given a rectangular farm with n rows and m columns, each cell is either empty (represented by '.') or contains a crop (represented by '*'). A single watering operation waters a contiguous group of crops. Two cells are considered adjacent if they share a common edge. Formally, two cells \( (i, j) \) and \( (k, l) \) are adjacent if \( |i-k| + |j-l| = 1 \).

Your task is to determine the minimum number of watering operations required to water all crops in the farm.

inputFormat

The input is read from stdin and follows this format:

  • The first line contains an integer \( T \), the number of test cases.
  • For each test case, the first line contains two integers \( n \) and \( m \) – the number of rows and columns.
  • This is followed by \( n \) lines, each containing a string of length \( m \) representing the farm grid.

outputFormat

For each test case, output a single integer on a new line representing the minimum number of watering operations required to water all crops. The output should be written to stdout.

## sample
2
3 4
.*..
..*.
....
2 3
*..
..*
2

2

</p>