304337: CF828B. Black Square
Memory Limit:256 MB
Time Limit:1 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Black Square
题意翻译
有一张大小为 $n × m$ 的方格纸, 一些方格涂成了黑色,其余的保持白色。想用黑色绘制最少数量的方格,以便所有黑色方格形成一个正方形。 请你确定需要涂成黑色的方格的最小数量,以使所有黑色方格形成一个黑色正方形,其边与绘画的边平行。不属于正方形的所有单元格应为白色。题目描述
Polycarp has a checkered sheet of paper of size $ n×m $ . Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.输入输出格式
输入格式
The first line contains two integers $ n $ and $ m $ ( $ 1<=n,m<=100 $ ) — the sizes of the sheet. The next $ n $ lines contain $ m $ letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.
输出格式
Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.
输入输出样例
输入样例 #1
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
输出样例 #1
5
输入样例 #2
1 2
BB
输出样例 #2
-1
输入样例 #3
3 3
WWW
WWW
WWW
输出样例 #3
1