#C10285. Minimum Barriers for Research Stations
Minimum Barriers for Research Stations
Minimum Barriers for Research Stations
In a rectangular grid, each cell is either empty (represented by .
) or contains a research station (represented by R
). Two research stations can communicate directly if they are located in the same row or the same column and there is no barrier between them. A barrier placed on a row or a column blocks communication along that line.
Your task is to determine the minimum number of barriers needed such that no two research stations can communicate directly. It can be proved that the optimal strategy is to block either all rows that contain at least one research station or all columns that contain at least one research station. Mathematically, if we let \(r\) be the number of rows containing at least one station and \(c\) be the number of columns containing at least one station, then the answer is given by:
[ \min(r, c) \quad ]
You are given multiple test cases. For each test case, calculate the minimum number of barriers needed.
inputFormat
The input is read from stdin and has the following format:
T m1 n1 row1 row2 ... (m1 rows total) ... mT nT row1 row2 ... (mT rows total)
Here, T
is the number of test cases. For each test case, the first line contains two space-separated integers m
and n
representing the number of rows and columns of the grid, respectively. The following m
lines each contain a string of length n
comprised of characters R
and .
.
outputFormat
For each test case, output a single integer on a new line — the minimum number of barriers required so that no two research stations can communicate directly. The output should be written to stdout.
## sample5
3 3
R.R
R.R
...
3 4
R..R
....
R..R
3 3
...
.R.
...
3 3
RRR
R..
RRR
3 3
R..
..R
R..
2
2
1
3
2
</p>