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.

Difficulty: interview

Starter code:

Solutions:
h, w = map(int, input().split())
x0, y0, x1, y1, c = 1000, 1000, -1, -1, 0
for i in range(h):
  row = str(input())
  for j in range(w):
    if row[j] == 'B':
      x0, y0, x1, y1, c = min(x0, i), min(y0, j), max(x1, i), max(y1, j), c + 1
ln = max(x1 - x0 + 1, y1 - y0 + 1)
if ln > min(h, w):
  print(-1)
elif x1 == -1:
  print(1)
else:
  print(ln * ln - c)

n,m=list(map(int,input().split()))
blacksX,blacksY=[],[]
count=0
for x in range(n):
    inp=input()
    for y in range(m):
        if inp[y]=='B':
            blacksX.append(x)
            blacksY.append(y)
            count+=1
if len(blacksX)==0:
    print(1)
else:
    side=max(max(blacksX)-min(blacksX)+1,max(blacksY)-min(blacksY)+1)
    if side <=n and side <= m:
        print(side*side-count)
    else:
        print(-1)


def list_input():
    return list(map(int,input().split()))
def map_input():
    return list(map(int,input().split()))
def map_string():
    return input().split()
    
n,m = map_input()
cnt = 0
upp = n+1
low = -1
left = -1
right = m+1
for i in range(n):
    a = list(input())
    cnt += a.count('B')
    for j in range(m):
        if a[j] == 'B':
            upp = min(upp,i)
            low = max(low,i)
            left = max(left,j)
            right = min(right,j)
x = abs(upp-low)+1 
y = abs(right-left)+1
s = max(x,y)
# print(upp,low,left,right)
if cnt == 0:
    print(1)
elif s > n or s > m:
    print(-1)
else:
    print((s*s)-cnt)
    


n, m = map(int, input().split())
our = [input() for i in range(n)]
first = -1
for i in range(n):
    if 'B' in our[i]:
        first = i
        break
last = -1
for i in range(n- 1, -1, -1):
    if 'B' in our[i]:
        last = i
        break  
if first == -1:
    print(1)
else:
    f2 = -1
    l2 = -1
    for i in range(m):
        a = [our[j][i] for j in range(n)]
        if 'B' in a:
            f2 = i
            break
    for i in range(m - 1, -1, -1):
        a = [our[j][i] for j in range(n)]
        if 'B' in a:
            l2 = i
            break    
    if last - first + 1 > m or l2 - f2 + 1 > n:
        print(-1)
    else:
        res = 0
        for i in range(first, last + 1):
            for j in range(f2, l2 + 1):
                if our[i][j] == 'W':
                    res += 1
        res += max(last - first + 1, l2 - f2 + 1) ** 2 - (last - first + 1) * (l2 - f2 + 1)
        print(res)

inp = input()
n, m = inp.split()
n = int(n)
m = int(m)
grid = []
top = n
btm = 0
lft = m
rht = 0
allBlackCount = 0

for i in range(0,n):
    inp = input()
    grid.append(inp)
    
for i in range(0, n):
    for j in range(0, m):
        if grid[i][j] == 'B':
            top = min(top, i)
            btm = max(btm, i)
            lft = min(lft, j)
            rht = max(rht, j)
            allBlackCount += 1

# print(top,lft)
# print(btm,rht)

h = btm-top+1
w = rht-lft+1
sz = max(h, w)

if allBlackCount == 0:
    print(1)
elif sz > n or sz > m:
    print(-1)
else:
    print(sz*sz-allBlackCount)

from sys import stdin, stdout

n, m = map(int, stdin.readline().split())
maps = []
challengers = []

for i in range(n):
    maps.append(stdin.readline().strip())

mins = [float('inf'), float('inf')]
maxs = [float('-inf'), float('-inf')]

for i in range(n):
    for j in range(m):
        if maps[i][j] == 'B':
            if i < mins[0]:
                mins[0] = i
            
            if j < mins[1]:
                mins[1] = j
            
            if i > maxs[0]:
                maxs[0] = i
            
            if j > maxs[1]:
                maxs[1] = j

if mins != [float('inf'), float('inf')] and max(abs(maxs[1] - mins[1]), abs(maxs[0] - mins[0])) >= min(n, m):
    stdout.write('-1')
elif mins == [float('inf'), float('inf')]:
    stdout.write('1')
else:
    ans = 0
    
    for i in range(mins[0], maxs[0] + 1):
        for j in range(mins[1], maxs[1] + 1):
            if maps[i][j] != 'B':
                ans += 1
    
    if abs(maxs[1] - mins[1]) > abs(maxs[0] - mins[0]):
        ans += (abs(maxs[1] - mins[1]) - abs(maxs[0] - mins[0])) * abs(maxs[1] - mins[1] + 1)
    elif abs(maxs[1] - mins[1]) < abs(maxs[0] - mins[0]):
        ans += (abs(maxs[0] - mins[0]) - abs(maxs[1] - mins[1])) * abs(maxs[0] - mins[0] + 1)
    
    stdout.write(str(ans))

n,m=map(int,input().split())
s=[list(input()) for _ in range(n)]
u,d,l,r=11**11,0,11**11,0
for i in range(n):
    for j in range(m):
        if s[i][j]=='B':
            if i<u:
                u=i
            if i>d:
                d=i
            if j<l:
                l=j
            if j>r:
                r=j
if r-l>=n or d-u>=m:
    print(-1)
elif u==11**11 and d==0 and l==11**11 and r==0:
    print(1)
else:
    ans=0
    for i in range(u,d+1):
        for j in range(l,r+1):
            if s[i][j]=='W':
                ans+=1
    print(ans+((max(r-l+1,d-u+1)-min(r-l+1,d-u+1))*(max(r-l+1,d-u+1))))

import sys

def main():
    n,m = list(map(int,sys.stdin.readline().split()))
    x=[]
    for i in range(n):
        x.append(sys.stdin.readline().rstrip())

    y = [-1,-1,-1,-1]

    for i in range(n):
        for j in range(m):
            if x[i][j]=='B':
                if y[0]==-1:
                    y[0]=i
                y[1]=max(y[1],j)
                y[2] = max(y[2],i)
                if y[3]!=-1:
                    y[3] = min(y[3],j)
                else:
                    y[3] = j

    if y[0] == -1 and y[1] == -1 and y[2] == -1 and y[3] == -1:
        print(1)
        return 

    q = max(y[1]-y[3]+1, y[2]-y[0]+1)
    if q > m or q>n:
        print(-1)
        return

    ans = 0
    for i in range(y[0],y[2]+1):
        for j in range(y[3],y[1]+1):
            if x[i][j]=='W':
                ans+=1

    ans+= q*(q - (y[2]-y[0]+1)) + q*(q - (y[1]-y[3]+1)) 
    print(ans)

    
main()


n, m = list(map(int, input().split()))
nn0 = n
nn1 = -1
mm0 = m
mm1 = -1
cnt = 0

for i in range(n):
  s = input().strip()
  if "B" in s:
    nn0 = min(i, nn0)
    nn1 = max(i, nn1)
    mm0 = min(s.index("B"), mm0)
    mm1 = max(s.rindex("B"), mm1)
    cnt += sum([z == "B" for z in s])
if nn1 < 0:
  print(1)
else:
  z = max(nn1-nn0, mm1-mm0) + 1  
  if z <= min(m,n):
    print(z*z - cnt)
  else:  
    print(-1)


n, m = map(int, input().split())
field = [input() for _ in range(n)]
black = 0
minheight = minwidth = 101
maxheight = maxwidth = 0
for i in range(n):
    s = field[i]
    for j in range(m):
        if s[j] == 'B':
            black += 1
            minheight = min(i, minheight)
            maxheight = max(i, maxheight)
            minwidth = min(j, minwidth)
            maxwidth = max(j, maxwidth)
if black == 0:
    print(1)
else:
    a = maxwidth - minwidth + 1
    b = maxheight - minheight + 1
    c = max(a, b)
    des = c * c
    if des > n * m or c > n or c > m:
        print(-1)
    else:
        print(des - black)

n, m = list(map(int, input().split()))
sq = [list(input()) for _ in range(n)]

rmin = n
rmax = 0
cmin = m
cmax = 0
cnt = 0
for r in range(n):
    for c in range(m):
        if sq[r][c] == 'B':
            cnt += 1
            rmin = min(rmin, r)
            rmax = max(rmax, r)
            cmin = min(cmin, c)
            cmax = max(cmax, c)

l = max(max(rmax - rmin + 1, cmax - cmin + 1), 1)
if min(n, m) < l:
    ans = -1
else:
    ans = l * l - cnt
print(ans)




n,m = list(map(int,input().split()))
l = []
up = -1
down = -1
left = -1
right = -1
cout = 0
for i in range(n):
    s = input()
    for j in range(m):
        if s[j] == 'B':
            cout += 1
            if up == -1:
                up = i
                down = i
                left = j
                right = j
            else:
                down = i
                left = min(j,left)
                right = max(j,right)
            

side =max(down-up,right-left)+1
if side == 0:
    side = 1
if side >  n or side > m:
    print(-1)
else:
    print(side**2-cout)


n,m = list(map(int,input().split()))
arr = [0]*(n+1)
for i in range(n):
    arr[i] = input()
cntb = 0
mn = 200
mx = 0
for i in range(n):
    first = True
    for j in range(m):
        if arr[i][j]=='B':
            cntb += 1
            if first:
                mn = min(mn,j)
                mx = max(mx,j)
                first = False
            else:
                mx = max(mx,j)
mn2 = 200
mx2 = 0
if cntb==0:
    print(1)
    return
elif cntb==1:
    print(0)
    return
for i in range(m):
    first = True
    for j in range(n):
        if arr[j][i]=='B':
            if first:
                mn2 = min(mn2,j)
                mx2 = max(mx2,j)
                first = False
            else:
                mx2 = max(mx2,j)
val = max(mx-mn+1,mx2-mn2+1)
if val>m or val>n:
    print(-1)
    return
print(val*val-cntb)


q,w=list(map(int,input().split()))
z,x,c,v=-1,-1,-1,-1
a=[]
s=0
for i in range(0,q):
    e=input()
    if e.count('B')!=0:
        s+=e.count('B')
        n=e.find('B')
        m=e.rfind('B')
        if c==-1:
            c=i
        v=i
        if z==-1:
            z=n
        else:
            z=min(z,n)
        x=max(x,m)
    a.append(e)
z,x=x-z+1,v-c+1
if max(z,x)>min(q,w):
    print(-1)
elif x*z==0:
    print(1)
else:
    print(max(z,x)**2-s)


n, m = [int(x) for x in input().split()]

arr = []

x1, x2, y1, y2 = 101, -1, 101, -1

for i in range(n):
    arr.append(input().rstrip())
c1 = 0
for i in range(n):
    for j in range(m):
        if arr[i][j] == "B":
            x1 = min(x1, j)
            x2 = max(x2, j)
            y1 = min(y1, i)
            y2 = max(y2, i)
            c1 = 1

#print(x1, x2, y1, y2)

count = 0

for j in range(x1, x2 + 1):
    for i in range(y1, y2 + 1):
        if arr[i][j] == "W":
            count += 1

sizeX = x2 - x1 + 1
sizeY = y2 - y1 + 1
if not c1:
    print(1)
elif sizeX > sizeY:
    if sizeX > n:
        print(-1)
    else:
        print(count + sizeX * (sizeX - sizeY))
elif sizeY > sizeX:
    if sizeY > m:
        print(-1)
    else:
        print(count + sizeY * (sizeY - sizeX))
else:
    print(count)


n, m = map(int, input().split())
pole = list()
min_i, min_j, max_i, max_j = n, m, -1, -1
count_B = 0

for i in range(n):
    pole += [input()]
    for j in range(m):
        t = pole[i][j]
        if t == 'B':
            count_B += 1
            min_i, min_j, max_i, max_j = min(i, min_i), min(j, min_j), max(i, max_i), max(j, max_j)

d_i, d_j = max_i - min_i, max_j - min_j
side = max(d_i + 1, d_j + 1)

print(1) if count_B == 0 else print(side ** 2 - count_B) if side <= min(n, m) else print(-1)


n,m=list(map(int,input().split()))
ls=[]
for i in range(n):
    l=list(str(input()))
    ls.append(l)
minr=1000
maxr=-1
minc=1000
maxc=-1
c=0
for i in range(n):
    for j in range(m):
        if ls[i][j]=='B':
            c+=1
            if minr>i:
                minr=i
            if maxr<i:
                maxr=i
            if minc>j:
                minc=j
            if maxc<j:
                maxc=j
if c==0:
    print(1)
    return
elif c==1:
    print(0)
    return
else:
    a=max(maxr-minr+1,maxc-minc+1)
    if a>m or a>n:
        print(-1)
        return
    else:
        print(a*a-c)
        return


n, m = list(map(int, input().split()))
L = [input() for _ in range(n)]
C = [[0 for _ in range(m)] for _ in range(n)]
x1, y1 = 500, 500
x2, y2 = -1, -1
for i in range(n):
    sa = 0
    for j in range(m):
        if L[i][j] == 'B':
            x1 = min(i, x1)
            x2 = max(i, x2)
            y1 = min(j, y1)
            y2 = max(j, y2)
            sa += 1
        if i > 0:
            C[i][j] += C[i-1][j]
        C[i][j] += sa
if x2 == -1:
    print(1)
else:
    a = max(x2-x1, y2-y1)
    ans = 1000000
    for i in range(n):
        for j in range(m):
            if i + a < n and j + a < m and i <= x1 and x2 <= i + a and j <= y1 and y2 <= j + a:
                s = C[i+a][j+a]
                if i > 0:
                    s -= C[i-1][j]
                if j > 0:
                    s -= C[i][j-1]
                if i > 0 and j > 0:
                    s -= C[i-1][j-1]
                ans = min((a+1)*(a+1) - s, ans)
    if ans == 1000000:
        ans = -1
    print(ans)



s = input().split(' ')
n = int(s[0])
m = int(s[1])
ans = -1
up = n
down = 0
left = m
right = 0
have = 0
for i in range(n):
    s = input()
    for j in range(m):
        if s[j] == 'B':
            have += 1
            up = min(up,i)
            down = max(down,i)
            left  = min(left,j)
            right = max(right,j)

if have == 0:
    print(1)
else:
    if up <= down and left <= right:
        l = max(down - up + 1,right - left + 1)
        if l<= min(n,m):
            print(l*l - have)
        else:
            print(-1)


def min_squares(canvas, length, width):
    def find_top():
        for i in range(length):
            for j in range(width):
                if canvas[i][j] == "B":
                    return i
        return -1

    def find_left():
        for j in range(width):
            for i in range(length):
                if canvas[i][j] == "B":
                    return j

    def find_bottom():
        for i in reversed(list(range(length))):
            for j in range(width):
                if canvas[i][j] == "B":
                    return i

    def find_right():
        for j in reversed(list(range(width))):
            for i in range(length):
                if canvas[i][j] == "B":
                    return j

    t = find_top()
    if t == -1:
        return 1
    l = find_left()
    b = find_bottom()
    r = find_right()

    # print(t, l, b, r)
    painted = 0
    for i in range(t, b + 1):
        for j in range(l, r + 1):
            if canvas[i][j] == "W":
                painted += 1

    while(b - t < r - l):
        if t > 0:
            t -= 1
            painted += r - l + 1
        elif b < length - 1:
            b += 1
            painted += r - l + 1
        else:
            return -1

    while(r - l < b - t):
        if l > 0:
            l -= 1
            painted += b - t + 1
        elif r < width - 1:
            r += 1
            painted += b - t + 1
        else:
            return -1

    return painted


def __starting_point():
    l, w = list(map(int, input().split()))
    canvas = []
    for _ in range(l):
        canvas.append(input())
    print(min_squares(canvas, l, w))

__starting_point()

n, m = [int(s) for s in input().split()]
g = [[0 for i in range(m)] for j in range(n)]
sum = 0
top = n-1
bottom = 0
left = m-1
right = 0
for i in range(n):
    f = str(input())
    for j in range(m):
        if f[j]  == 'B':
            sum += 1
            top = min(top, i)
            bottom = max(bottom, i)
            left = min(left, j)
            right = max(right, j)
if sum == 0:
    print(1)
else:
    a = max(right - left + 1, bottom - top + 1)
    if a>n or a>m:
        print(-1)
    else:
        print(a**2 - sum)


def polycarp(n, m, l):
    left = min(l).find("B")
    if left == -1:
        return 1
    right = m - min([x[::-1] for x in l]).find("B") - 1
    top = bottom = -1
    for i in range(n):
        if l[i].find("B") != -1:
            top = i
            break
    for i in range(n - 1, -1, -1):
        if l[i].find("B") != -1:
            bottom = i
            break
    w = right - left + 1
    h = bottom - top + 1
    if w > n or h > m:
        return -1
    r = 0
    for i in range(top, bottom + 1):
        for j in range(left, right + 1):
            if l[i][j] == "W":
                r += 1
    if w > h:
        r += (w - h) * w
    else:
        r += (h - w) * h
    return r


n, m = list(map(int, input().strip().split()))
l = list()
for i in range(n):
    l.append(input().strip())
print(polycarp(n, m, l))

n, m = list(map(int,input().split()))
q = [str(input()) for i in range(n)]
k=0
t=True
u=0
li=vi=n*m
pi=ni=0
for i in range(n):
    t=False
    u=0
    for j in q[i]:
        if j=='B':
            if u<li:
                li=u
            if u>pi:
                pi=u
            if i<vi:
                vi=i
            if i>ni:
                ni=i
            k+=1
        u+=1
if (max(ni-vi, pi-li)+1)>n or (max(ni-vi, pi-li)+1)>m:
    print(-1)
elif k==0:
    print(1)
else:
    print((max(ni-vi, pi-li)+1)**2-k)


def __starting_point():
    n, m = list(map(int, input().split()))
    matrix = list()
    minR = n; minC = m; maxR=maxC=0
    count = 0
    for i in range(n):
        s = input()
        for j in range(m):
            if s[j] == 'B':
                minR = min(minR, i)
                maxR = max(maxR, i)
                minC = min(minC, j)
                maxC = max(maxC, j)
                count += 1
    edge = max(maxR-minR+1, maxC-minC+1)
    if count == 0:
        print(1)
    elif edge > n or edge > m:
        print(-1)
    else:
        print(edge*edge-count)
    

__starting_point()

n,k=list(map(int,input().split(' ')))
b=[]
r=[]
c=[]

for i in range (n):
  b.append(input())
for i in range(n):
  for j in range(k):
    if b[i][j]=='B':
      
      r.append(i)
      c.append(j)

if len(c)==0:
  print(1)
else:
  maxr=max(r)-min(r)
  maxc=max(c)-min(c)
  size=max(maxc,maxr)+1
  if (size>n or size>k):
    print(-1)
  else:
    print(size**2-len(c))