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. GNU-libiconv编码转换库的使用举例

    继GDAL库.PROJ库.HDF5库.TINYXML库之后,手上进行的项目又让我碰到了ICONV库.之前花了2天时间没有搞定,在甲方一直催促下,今天又捡起来搞搞,搞了一天最终搞定了.相关心得记录例如以 ...

  2. pig 调试(explain&amp;illerstrate)

    grunt> cat t.txt kw1 2 kw3 1 kw2 4 kw1 5 kw2 2 cat test.pig A = LOAD '/user/input/t.txt' as (k:ch ...

  3. nodejs02

    Node.js没有根目录的概念,因为它根本没有任何的web容器! 让node.js提供一个静态服务,都非常难! 也就是说,node.js中,如果看见一个网址是 1127.0.0.1:3000/fang ...

  4. Word histogram

    Here is a program that reads a file and builds a histogram of the words in the file: process_file lo ...

  5. POJ 3173 模拟

    按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...

  6. 使用Chrome浏览器,让我们远离(所有)广告

    你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...

  7. AngularJs轻松入门(七)多视图切换

    在AngularJs应用中,我们可以將html碎片写在一个单独的文件中,然后在其他页面中將该段碎片加载进来.如果有多个碎片文件,我们还可以在控制器中根据用户的操作动态的加载不同的碎片,从而达到切换视图 ...

  8. Android 代码中使用Color工具类 parseColor

    方式一: arg1.setBackgroundColor(Color.parseColor("#87CEFA")); 方式二: arg1.setBackgroundColor(ge ...

  9. 日前加拿大平板厂商 Datawind和印度运营商Reliance Communications日前宣布合作

    全球最便宜智能手机只要15美元 随着手机进入智能时代,这些年智能手机的发展可谓迅猛,苹果三星这样的手机厂商成为最大的受益者同时,低门槛也让越来越多的人开始意识到,全民智能时代确实要来了. 为了能让第三 ...

  10. vuejs scope

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...