Round #423 B. Black Square(Div.2)

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 4WWWWWWWBWWWBWWBBWWWW

output

5

input

1 2BB

output

-1

input

3 3WWWWWWWWW

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.

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 int main(){
 5     int n,m,ans=0;
 6     scanf("%d%d",&n,&m);
 7     char a;
 8     int imi=105,jmi=105,ima=0,jma=0;
 9     for(int i=1;i<=n;i++)
10     for(int j=1;j<=m;j++){
11             cin>>a;   //scanf("%c",&a);
12             if(a==‘B‘){
13                 ans++;
14                 imi=min(imi,i);
15                 ima=max(ima,i);
16                 jmi=min(jmi,j);
17                 jma=max(jma,j);
18             }
19     }
20     int k=max(ima-imi,jma-jmi)+1;
21     if(ans==0)  printf("1\n");
22     else if(n>=k&&m>=k) printf("%d\n",k*k-ans);
23     else printf("-1\n");
24     return 0;
25 }
时间: 2024-08-24 04:34:21

Round #423 B. Black Square(Div.2)的相关文章

Round #423 A.Restaurant Tables(Div.2)

In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. I

Round #423 C. String Reconstruction(Div.2)

Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one. Ivan knows some information about the string s. Namely, he re

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,

Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load 题意: 给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小. 题解: 显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好. 然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了. 最大距离的最小值就是离根最远的和最近的加一加就行了. 1 #include<cstdio> 2 #define F(i,a,b) for

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) B. Black Square

题意:问是否可以形成一个全黑正方形 思路:可以找出正方形的边,然后判断下这个矩阵是否容得下,n,m都比边短,比赛的时候写麻烦了,还去找了这个正方形究竟在哪个位置,这样的话得考虑很多情况,不如就边*边-黑子的总数 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=2e5+10; 5 char a[102][102]; 6 int main(){ 7 int n,m; 8 s

Codeforces Round #423 (Div. 2) A-C

A. Restaurant Tables 这里看错题意还wa了两发.... 按题意模拟就行了 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #includ

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seate

Codeforces Round #423 (Div. 2)A B C D

A. Restaurant Tables 题意是一个点单人座有a个双人座有b个,有n组人,每组人有一个或两个人.当一个人时优先坐单人座的没有就坐没有人坐的双人座,然后才是在已经坐了一人的双人座,还没有就拒绝为他服务.当两个人时,只坐双人座的没有就拒绝服务.问这个店要拒绝服务多少人. 直接模拟下就行了. 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 using namespace st

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

题意:出n个点,其中k个叶子节点,问构造出的树最远的两个点最近是多少 思路:以一个点为中心,然后m个伸出,一层层扩散,(n-1)%m==k,如果k==0,即可以平分,长度就是2*(n-1)/m,如果取模为k==1,说明多出一个,+1,其他的话,就是最后一层补k个,但是最长的还是+2 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int n,m; 6 cin>>n>>m; 7 int