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 <algorithm>
using namespace std;
typedef long long ll;
char a[][];
int main()
{
int n,m,ans1=;
int minx=,miny=,maxx=,maxy=,ans=;
cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='B')
{
ans++;
minx=min(i,minx);
miny=min(j,miny);
maxx=max(i,maxx);
maxy=max(j,maxy);
}
}
if(ans==) {puts("");return ;}
if(ans==) {puts("");return ;}
int xx=maxx-minx+,yy=maxy-miny+;
if(xx>m||yy>n) {puts("-1");return ;} ans=max(xx,yy)*max(xx,yy)-ans; cout<<ans<<endl;
return ;
}

B. Black Square(字符串)的更多相关文章

  1. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  2. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  3. SQL-数学、字符串、时间日期函数和类型转换

    --数学函数 --ABS绝对值,select ABS(-99)--ceiling取上限,select CEILING(4.5)--floor去下限select FLOOR(4.5)--power 几次 ...

  4. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

  5. sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)

    Pixel density Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pi ...

  6. 字符串核对之Boyer-Moore算法

    算法说明: 在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键 ...

  7. Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题

    B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...

  8. C++ 字符串相关函数

    <转>自:http://zhidao.baidu.com/question/173202165.html 首先就是memcpy表头文件: #include <string.h> ...

  9. php中的字符串和正则表达式

    一.字符串类型的特点 1.PHP是弱类型语言,其他数据类型一般都可以直接应用于字符串函数操作. 1: <?php //输出345 //输出345 //先查找hello常量,若没找到,将hello ...

随机推荐

  1. php下载大文件

    <?php $file = @ fopen($file_dir . $file_name,"r"); $filesize=filesize($file_dir.$file_n ...

  2. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. HDU 5950Recursive sequence ICPC沈阳站

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. gitlab通过api创建组、项目、成员

    前戏 获取gitlab中admin用户的private_token Groups API 获取某个组的详细 curl --header "PRIVATE-TOKEN: *********&q ...

  5. JAVA中3种将byte转换为String的方法

    HttpClient 类库中GetMethod类的getResponseBody方法返回的是byte[]类型,要操作起来不方便,我想把它转化成String类型. 查了网上的资料,有说法认为用这种方法比 ...

  6. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    [算法]模拟 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; int a[maxn],A[m ...

  7. 继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建.

      继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建.   https://www.evernote.com/shard/s227 ...

  8. Tornado 安装及简单程序示例

    1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...

  9. 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week1 Introduction to deep learning课堂笔记

    Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week1 Introduction to deep learn ...

  10. Low-overhead enhancement of reliability of journaled file system using solid state storage and de-duplication

    A mechanism is provided in a data processing system for reliable asynchronous solid-state device bas ...