Consider a grid with H rows and W columns of squares. Each square can be painted either black or white. The location of a square is denoted as (r, c), where r is the row number (from the top) and c is the column number (from the left).
A grid is considered good if the following conditions are met:
From the starting position (1, 1), you can reach the position (H, W) by moving either right or down repeatedly while always staying on white squares.
Both the starting position (1, 1) and the target position (H, W) must also be white squares.
Your task is to make the grid good by performing the following operation:
Operation
Choose four integers r_0, c_0, r_1, c_1 (where 1 ≤ r_0 ≤ r_1 ≤ H and 1 ≤ c_0 ≤ c_1 ≤ W). For each pair of indices (r, c) within the specified range, invert the color of the square at that position. Inversion means changing a white square to black and vice versa.
Objective
Find the minimum number of operations needed to transform the grid into a good configuration.
Input Format
The first line contains two integers, H and W - the number of rows and columns in the grid, respectively.
The next H lines each contain a string of W characters ('B' for black and 'W' for white), representing the colors of the squares in the grid.
Output Format
Print a single integer: the minimum number of operations required to make the grid good.
Examples
Example 1
Input:
text
3 3
WWW
WBB
WWW
Output:
text
0
Explanation: The grid is already good because you can move from (1, 1) to (3, 3) without hitting any black squares.
Example 2
Input:
text
3 3
WWW
BBW
WWW
Output:
text
1
Explanation: One operation can be performed to make the grid good by inverting the color in the range (2, 1) to (2, 2).
Example 3
Input:
text
2 2
BW
WB
Output:
text
2
Explanation: You need to perform two operations to make both (1, 1) and (2, 2) white and ensure a path is available.
Solution
You should implement the solution in the /app/ directory.