Examples
input 1
4 2 BWWW WBBW WBBW WWWB
output 1
4
input 2
3 1 BWB WWB BWB
output 2
2
input 3
5 3 BWBBB BWBBB BBBBB BBBBB WBBBW
output 3
2
input 4
2 2 BW WB
output 4
4
input 5
2 1 WW WW
output 5
4
Note
In the first example, Gildong can click the cell(2,2), then the working screen becomes: