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 ;
}

cf B. Black Square的更多相关文章

  1. CF Theatre Square

    Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard input ...

  2. 竞赛题解 - [CF 1080D]Olya and magical square

    Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...

  3. [CF 612E]Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  4. CF 50E. Square Equation Roots

    思路:这题的关键就是重复根只可能是整数. 这样先求出所有的根的数目,在减去重复的根. 代码如下: #include <iostream> #include <cstring> ...

  5. CF 711B - Chris and Magic Square

    挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...

  6. codeforce 1A Theatre Square

    A. Theatre Square Theatre Square in the capital city of Berland has a rectangular shape with the siz ...

  7. 竞赛题解 - CF Round #524 Div.2

    CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...

  8. CF练习记录

    2018/5/6 Codeforces Round #478 (Div. 2) C http://codeforces.com/contest/975/problem/C Valhalla Siege ...

  9. CF 234 C Weather(粗暴方法)

    C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. Swift,Objective-C语言性能对照測试

    原文发表于踏得网 Swift包括了非常多现代语言特性尤其是从一些脚本语言如Javascript/Ruby中汲取了营养. 此外苹果公布Swift时,使用特别选用的一些样例来宣称Swift性能对于Ojbe ...

  2. hibernate动态表名映射--仅仅有想不到,没有做不到

    近期的一个项目有一个需求,有N个考核单位,要对每一个考核单位生成一张考核情况表.这样做的目的是横切数据库,这这个需求的实现中,我的组员遇到了一个技术问题,我将我的解决的方法和整个思考过程与大家分享, ...

  3. 前端到后台ThinkPHP开发整站--php开发案例

    前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...

  4. hpuoj--校赛--送给新生的礼物(水题)

    问题 A: 感恩节KK专场--送给新生的礼物 时间限制: 1 Sec  内存限制: 128 MB 提交: 631  解决: 187 [提交][状态][讨论版] 题目描述 学长KK要送给学弟学妹们礼物, ...

  5. CUDA中的归约

    CUDA编程实战书中的乘方和解决办法: 对一个数组执行某种计算,然后产生一个更小的结果数组. 由一个线程在共享内存上进行迭代并计算出总和值.而如果用并行,所花时间就与数组长度的对数成正比. 代码的思想 ...

  6. luogu P3157 [CQOI2011]动态逆序对(CDQ分治)

    题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...

  7. easyui_datagrid使用

    easyui的datagrid显示数据的方式(使用了jQuery) 第一步 创建显示的格式,方法有两种: 第一种:在HTML标签中创建,类似如下的形式,参数可以在标签中设置,也可以在脚本中 这种方式在 ...

  8. 20180929 北京大学 人工智能实践:Tensorflow笔记06

    入戏 需要修改成如下: (完)

  9. 紫书 例题 10-17 UVa 1639(数学期望+对数保存精度)

    设置最后打开的是盒子1, 另外一个盒子剩下i个 那么在这之前打开了n + n - i次盒子 那么这个时候的概率是C(2 * n - i, n) p ^ (n+1) (1-p)^ (n - i) 那么反 ...

  10. [HAOI2006]旅行(并查集)

    寒假填坑五十道省选题——第五道 [HAOI2006]旅行 题目描述 Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,…,N),这些景点被M条道路 ...