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. 紫书 例题8-14 UVa 1607 (二分)

    题意非常难理解-- #include<cstdio> #define REP(i, a, b) for(int i = (a); i < (b); i++) using namesp ...

  2. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  3. cin详解(get()、getline()、clear()、sync())

    简述 在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" io ...

  4. 优秀Swift开源项目推荐

    工具类 SwiftyJSON:GitHub上最为开发者认可的JSON解析类 Safe.ijaimi:源码漏洞分析检测工具,一键完成 Dollar.swift:Swift版Lo-Dash(或unders ...

  5. 4、python序列对比

    1.列表:[ ],杂(什么都可以放进去),有序,可变 2.元组:(),有序,不可变 3.字典:{ },键值对,无序,可变 4.集合:{ },不可重复,无序,可变

  6. MyEclipse常见错误汇总,中英注释版(长期更新)

    No.1 当一条语句漏写分号时错误描述如下 Syntax error, insert ";" to complete Statement(语法错误:插入分号完成语句描述) No.2 ...

  7. CSDN博客2014年4月24日清理缓存

    亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...

  8. Sqlite3的安装Windows

  9. WAF——针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入、XSS跨站、Webshell上传、命令注入、非法HTTP协议请求、非授权文件访问等

    核心概念 WAF Web应用防火墙(Web Application Firewall),简称WAF. Web攻击 针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入.XSS跨站.Websh ...

  10. Mysql-in查询问题

    Mysql-in查询问题 标签(空格分隔): mysql 问题:mysql用in语法查询出来的数据少了好多! 我的实际情况: 数据表: content字段记录着一些选项的id,多个选项用逗号隔开,比如 ...