B. Black Square
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Examples
Input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
Output
5
Input
1 2
BB
Output
-1
Input
3 3
WWW
WWW
WWW
Output
1
Note

In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).

In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.

In the third example all cells are colored white, so it's sufficient to color any cell black.

注意边界

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define lowbit(x) x&(-x)
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define mem(a) (memset(a,0,sizeof(a)))
const int inf=0x3f3f3f3f;
int main()
{
int n,m,ans=;
cin>>n>>m;
int imin=,jmin=;
int imax=,jmax=;
char a[n+][m+];
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='B')
{
ans++;
imin=min(imin,i);
imax=max(imax,i);
jmin=min(jmin,j);
jmax=max(jmax,j);
}
}
}
int k=max(imax-imin,jmax-jmin)+;
if(!ans) printf("1\n");
else if(n>=k && m>=k)
printf("%d\n",k*k-ans);
else printf("-1\n");
return ;
}

最新文章

  1. ASP.NET Core 中文文档 第四章 MVC(4.4)依赖注入和控制器
  2. 【JQ基础】数组
  3. 《图解HTTP》读书笔记
  4. E-Business Suite 12.2 startCD 50 Install Fails with Fatal Error: TXK Install Service oracle.apps.fnd.txk.config.ProcessStateException: OUI process failed Cannot install Web Tier Utilities
  5. SpringMVC 视图和视图解析器&amp;表单标签
  6. Git修改提交的用户名和Email
  7. 8.1H5学习笔记
  8. [Nginx][HttpUpstreamModule]翻译负载均衡
  9. Python如何规定对方输入的数字必须是整数?
  10. Android 模块化编程之引用本地的aar
  11. PB+MS SQL+触发器必须指出
  12. ORACLE数据库 DBA常用知识
  13. Python 代码片段整理
  14. c#重命名文件,报错“System.NotSupportedException”类型的未经处理的异常在 mscorlib.dll 中发生”
  15. Python pip源更改
  16. springmvc shiro整合cas单点登入
  17. canvas学习之树叶动画
  18. ELK新手教程(二)
  19. [golang note] 环境搭建
  20. elasticsearch 路由文档到分片

热门文章

  1. JAVA jsp page指令的属性 errorPage 和isErrorPage
  2. 实验二实验结论&amp;实验总结与体会
  3. Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP
  4. Java 二进制,八进制,十进制,十六进制转换
  5. 在VS2013中配置QT5 win7_64
  6. 5.array
  7. leetcode 新题型----SQL,shell,system design
  8. PostgreSQL Replication之第一章 理解复制概念(2)
  9. vue2.0 兄弟组件数据传递方法
  10. windows server 2012安装.NET3.5安装提示需要指定源路径 安装.net3.5提示安装不成功,提示需要指定源路径。