import java.util.*;
import java.io.*;
import java.math.*;

public class A {

    public static void main(String[] args) throws Exception {
        // FileInputStream inputStream = new FileInputStream("input.txt");
        // FileOutputStream outputStream = new FileOutputStream("output.txt");

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Solver solver = new Solver();
        int t = in.nextInt();
        // int t = 1;
        for (int i = 0 ; i < t ; i++) {
            solver.solve(1, in, out);
        }

        out.close();
    }

    static class Solver {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            if (n == 1) {
                int l = in.nextInt();
                int r = in.nextInt();
                out.println(0);
                return;
            }
            int resultL = Integer.MAX_VALUE;
            int resultR = Integer.MIN_VALUE;
            for (int i = 0 ; i < n ; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                resultL = Math.min(resultL, r);
                resultR = Math.max(resultR, l);
            }
            out.println(resultR - resultL);
        }

    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class A {

    public static void main(String[] args) throws Exception {
        // FileInputStream inputStream = new FileInputStream("input.txt");
        // FileOutputStream outputStream = new FileOutputStream("output.txt");

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Solver solver = new Solver();
        int t = in.nextInt();
        // int t = 1;
        for (int i = 0 ; i < t ; i++) {
            solver.solve(1, in, out);
        }

        out.close();
    }

    static class Solver {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            if (n == 1) {
                out.println(0);
                return;
            }
            int resultL = Integer.MAX_VALUE;
            int resultR = Integer.MIN_VALUE;
            for (int i = 0 ; i < n ; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                resultL = Math.min(resultL, r);
                resultR = Math.max(resultR, l);
            }
            out.println(resultR - resultL);
        }

    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class A {

    public static void main(String[] args) throws Exception {
        // FileInputStream inputStream = new FileInputStream("input.txt");
        // FileOutputStream outputStream = new FileOutputStream("output.txt");

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Solver solver = new Solver();
        int t = in.nextInt();
        // int t = 1;
        for (int i = 0 ; i < t ; i++) {
            solver.solve(1, in, out);
        }

        out.close();
    }

    static class Solver {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int resultL = Integer.MAX_VALUE;
            int resultR = Integer.MIN_VALUE;
            for (int i = 0 ; i < n ; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                resultL = Math.min(resultL, r);
                resultR = Math.max(resultR, l);
            }
            out.println(resultR - resultL);
        }

    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class A {

    public static void main(String[] args) throws Exception {
        // FileInputStream inputStream = new FileInputStream("input.txt");
        // FileOutputStream outputStream = new FileOutputStream("output.txt");

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;

        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Solver solver = new Solver();
        int t = in.nextInt();
        // int t = 1;
        for (int i = 0 ; i < t ; i++) {
            solver.solve(1, in, out);
        }

        out.close();
    }

    static class Solver {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int resultL = Integer.MAX_VALUE;
            int resultR = Integer.MIN_VALUE;
            for (int i = 0 ; i < n ; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                resultL = Math.min(resultL, r);
                resultR = Math.max(resultR, l);
            }
            out.println(Math.max(0, resultR - resultL));
        }

    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package Practice_1100;

import java.io.*;

/**
 *
 * @author Akhilesh
 */
public class A1227 {
    public static void main(String[] args) throws IOException {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));

        int test = Integer.parseInt(scan.readLine().trim());

        test:
        while (test-- > 0) {
            int n = Integer.parseInt(scan.readLine().trim()), min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
            for (int i = 0; i < n; i++) {
                String str[] = scan.readLine().split("\\s+");
                int a = Integer.parseInt(str[0]);
                int b = Integer.parseInt(str[1]);
                min = Math.min(min, b);
                max = Math.max(max, a);
            }

            System.out.println(max - min > 0 ? max - min : 0);
        }
    }
}


import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.lang.String;

import static java.lang.Math.*;

public class Main {
    long mod = (long) 1e9 + 7;

    void run() throws IOException {
        int q = nextInt();
        while (q-- > 0) {
            int n = nextInt();
            if (n == 1) {
                pw.println(0);
                continue;
            }
            long min_r = Long.MAX_VALUE;
            long max_l = 0;
            for (int i = 0; i < n; i++) {
                int l = nextInt();
                int r = nextInt();
                min_r = min(r, min_r);
                max_l = max(l, max_l);
            }
            pw.println(abs(min_r - max_l));
        }
        pw.close();
    }

    class Point {
        int x, y;

        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }

    class PointComp implements Comparator<Point> {

        @Override
        public int compare(Point o1, Point o2) {
            if (o1.x == o2.x) return Integer.compare(o1.y, o2.y);
            return Long.compare(o1.x, o2.x);
        }
    }

    class Touple {
        int x;
        int from, to;

        public Touple(int a, int b, int c) {
            x = a;
            from = b;
            to = c;
        }
    }

    class ToupleComp implements Comparator<Touple> {

        @Override
        public int compare(Touple o1, Touple o2) {
            if (o1.x == o2.x && o1.from == o2.from) return Integer.compare(o1.to, o2.to);
            if (o1.x == o2.x) return Integer.compare(o1.from, o2.from);
            return Long.compare(o1.x, o2.x);
        }
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    PrintWriter pw = new PrintWriter(System.out);

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    String next() throws IOException {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public Main() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

}


import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.lang.String;

import static java.lang.Math.*;

public class Main {
    long mod = (long) 1e9 + 7;

    void run() throws IOException {
        int q = nextInt();
        while (q-- > 0) {
            int n = nextInt();
            long min_r = Long.MAX_VALUE;
            long max_l = 0;
            for (int i = 0; i < n; i++) {
                int l = nextInt();
                int r = nextInt();
                min_r = min(r, min_r);
                max_l = max(l, max_l);
            }
            if (n == 1) {
                pw.println(0);
                continue;
            }
            if (max_l >= min_r) pw.println(max_l - min_r);
            else pw.println(0);
        }
        pw.close();
    }

    class Point {
        int x, y;

        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }

    class PointComp implements Comparator<Point> {

        @Override
        public int compare(Point o1, Point o2) {
            if (o1.x == o2.x) return Integer.compare(o1.y, o2.y);
            return Long.compare(o1.x, o2.x);
        }
    }

    class Touple {
        int x;
        int from, to;

        public Touple(int a, int b, int c) {
            x = a;
            from = b;
            to = c;
        }
    }

    class ToupleComp implements Comparator<Touple> {

        @Override
        public int compare(Touple o1, Touple o2) {
            if (o1.x == o2.x && o1.from == o2.from) return Integer.compare(o1.to, o2.to);
            if (o1.x == o2.x) return Integer.compare(o1.from, o2.from);
            return Long.compare(o1.x, o2.x);
        }
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    PrintWriter pw = new PrintWriter(System.out);

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    String next() throws IOException {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public Main() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

}


import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.lang.String;

import static java.lang.Math.*;

public class Main {
    long mod = (long) 1e9 + 7;

    void run() throws IOException {
        int q = nextInt();
        while (q-- > 0) {
            int n = nextInt();
            long min_r = Long.MAX_VALUE;
            long max_l = 0;
            for (int i = 0; i < n; i++) {
                int l = nextInt();
                int r = nextInt();
                min_r = min(r, min_r);
                max_l = max(l, max_l);
            }
            if (n == 1) {
                pw.println(0);
                continue;
            }
            pw.println(abs(min_r - max_l));
        }
        pw.close();
    }

    class Point {
        int x, y;

        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }

    class PointComp implements Comparator<Point> {

        @Override
        public int compare(Point o1, Point o2) {
            if (o1.x == o2.x) return Integer.compare(o1.y, o2.y);
            return Long.compare(o1.x, o2.x);
        }
    }

    class Touple {
        int x;
        int from, to;

        public Touple(int a, int b, int c) {
            x = a;
            from = b;
            to = c;
        }
    }

    class ToupleComp implements Comparator<Touple> {

        @Override
        public int compare(Touple o1, Touple o2) {
            if (o1.x == o2.x && o1.from == o2.from) return Integer.compare(o1.to, o2.to);
            if (o1.x == o2.x) return Integer.compare(o1.from, o2.from);
            return Long.compare(o1.x, o2.x);
        }
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    PrintWriter pw = new PrintWriter(System.out);

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    String next() throws IOException {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public Main() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

}


#include<bits/stdc++.h>
using namespace std;

char a[105][105];

int main()
{
    int n, m, i, j, d=0, xl=200, xh=-1, cnt=0, yl=200, yh=-1;
    char ch;
    scanf("%d %d\n", &n, &m);
    //printf("%d %d\n\n", n, m);
    for(i=0; i<n; i++)
    {
        gets(a[i]);
        //puts(a[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(a[i][j]=='B')
            {
                xl=min(xl, j);
                xh=max(xh, j);
                yl=min(yl, i);
                yh=max(yh, i);
            }
            //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);

        }
    }
    //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);
    d=max(yh-yl, xh-xl);
    //printf("d=%d\n\n", d);
    if(d==-201){printf("1"); return 0;}
    if(d==0){printf("0"); return 0;}
    if(xh-xl==0||yh-yl==0){printf("-1"); return 0;}
    if((xh-d<0&&xl+d>m)||(yh-d<0&&yl+d>n)){printf("-1"); return 0;}
    if(yl+d>n-1){yl=yh-d;}
    if(xl+d>m-1){xl=xh-d;}
    //printf("xl=%d, yl=%d\n\n", xl, yl);
    for(i=yl; i<=yl+d; i++)
    {
        for(j=xl; j<=xl+d; j++)
        {
            if(a[i][j]=='W'){cnt++;}
        }
    }
    printf("%d", cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

char a[105][105];

int main()
{
    int n, m, i, j, d=0, xl=200, xh=-1, cnt=0, yl=200, yh=-1;
    char ch;
    scanf("%d %d\n", &n, &m);
    for(i=0; i<n; i++)
    {
        gets(a[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(a[i][j]=='B')
            {
                xl=min(xl, j);
                xh=max(xh, j);
                yl=min(yl, i);
                yh=max(yh, i);
                cnt++;
            }
            //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);

        }
    }
    //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);
    d=max(yh-yl, xh-xl);
    //printf("d=%d\n\n", d);
    if(d==-201){printf("1"); return 0;}
    if(d==0){printf("0"); return 0;}
    if(d+1>m||d+1>n){printf("-1"); return 0;}
    printf("%d", (d+1)*(d+1)-cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

char a[105][105];

int main()
{
    int n, m, i, j, d=0, xl=200, xh=-1, cnt=0, yl=200, yh=-1;
    char ch;
    scanf("%d %d\n", &n, &m);
    //printf("%d %d\n\n", n, m);
    for(i=0; i<n; i++)
    {
        gets(a[i]);
        //puts(a[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(a[i][j]=='B')
            {
                xl=min(xl, j);
                xh=max(xh, j);
                yl=min(yl, i);
                yh=max(yh, i);
            }
            //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);

        }
    }
    //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);
    d=max(yh-yl, xh-xl);
    //printf("d=%d\n\n", d);
    if(d==-201){printf("1"); return 0;}
    if(xh-xl==0||yh-yl==0){printf("-1"); return 0;}
    if((xh-d<0&&xl+d>m)||(yh-d<0&&yl+d>n)){printf("-1"); return 0;}
    if(yl+d>n-1){yl=yh-d;}
    if(xl+d>m-1){xl=xh-d;}
    //printf("xl=%d, yl=%d\n\n", xl, yl);
    for(i=yl; i<=yl+d; i++)
    {
        for(j=xl; j<=xl+d; j++)
        {
            if(a[i][j]=='W'){cnt++;}
        }
    }
    printf("%d", cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

char a[105][105];

int main()
{
    int n, m, i, j, d=0, xl=200, xh=-1, cnt=0, yl=200, yh=-1;
    char ch;
    scanf("%d %d\n", &n, &m);
    //printf("%d %d\n\n", n, m);
    for(i=0; i<n; i++)
    {
        gets(a[i]);
        //puts(a[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(a[i][j]=='B')
            {
                xl=min(xl, j);
                xh=max(xh, j);
                yl=min(yl, i);
                yh=max(yh, i);
            }
            //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);

        }
    }
    //printf("xl=%d, xh=%d, yl=%d, yh=%d\n\n", xl, xh, yl, yh);
    d=max(yh-yl, xh-xl);
    //printf("d=%d\n\n", d);
    if(d==-201){printf("1"); return 0;}
    if(d==0){printf("1"); return 0;}
    if(xh-xl==0||yh-yl==0){printf("-1"); return 0;}
    if((xh-d<0&&xl+d>m)||(yh-d<0&&yl+d>n)){printf("-1"); return 0;}
    if(yl+d>n-1){yl=yh-d;}
    if(xl+d>m-1){xl=xh-d;}
    //printf("xl=%d, yl=%d\n\n", xl, yl);
    for(i=yl; i<=yl+d; i++)
    {
        for(j=xl; j<=xl+d; j++)
        {
            if(a[i][j]=='W'){cnt++;}
        }
    }
    printf("%d", cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=1,r2=1,c1=1,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' && co==0){
                r1=i+1;
                c1=j+1;
                co++;
            }
            else if(s[i][j]=='B' ){
                r2=i+1;
                c2=j+1;
                co++;
            }
        }
    }
    c=abs(c1-c2)+1;
    r=abs(r1-r2)+1;
int mx=max(c,r);
mx=mx;
int mn=min(n,m);

int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(r>m || c>n){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=1,r2=1,c1=1,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' && co==0){
                r1=i+1;
                c1=j+1;
                co++;
            }
            else if(s[i][j]=='B' ){
                r2=i+1;
                c2=j+1;
                co++;
            }
        }
    }
    c=abs(c1-c2);
    r=abs(r1-r2);
int mx=max(c,r);
mx=mx+1;
int mn=min(n,m);

int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(mn<mx){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=101,r2=1,c1=101,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' ){
                r1=min(r1,i);
                r2=max(r2,i);
                c1=min(c1,j);
                c2=max(c2,j);
                co++;

            }
        }
    }
   // cout<<c1<<" "<<c2<<endl;
   // cout<<r1<<" "<<r2<<endl;
    c=(c2-c1)+1;
    r=(r2-r1)+1;
int mx=max(c,r);
mx=mx;
//cout<<mx<<endl;
int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(r>m || c>n){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=101,r2=1,c1=101,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' ){
                r1=min(r1,i);
                r2=max(r2,i);
                c1=min(c1,j);
                c2=max(c2,j);
                co++;

            }
        }
    }
   // cout<<c1<<" "<<c2<<endl;
   // cout<<r1<<" "<<r2<<endl;
    c=(c2-c1)+1;
    r=(r2-r1)+1;
int mx=max(c,r);
mx=mx;
int mn=min(n,m);
//cout<<mx<<endl;
int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(mx>mn){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=101,r2=1,c1=101,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' ){
                r1=min(r1,i);
                r2=max(r2,i);
                c1=min(c1,j);
                c2=max(c2,j);
                co++;

            }
        }
    }
   // cout<<c1<<" "<<c2<<endl;
   // cout<<r1<<" "<<r2<<endl;
    c=c2-c1;
    r=r2-r1;
int mx=max(c,r);
mx=mx+1;
//cout<<mx<<endl;
int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(r>m || c>n){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=1,r2=1,c1=1,c2=1,i,j,co=0;
    char s[100][100];
    cin>>n>>m;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' && co==0){
                r1=i+1;
                c1=j+1;
                co++;
            }
            else if(s[i][j]=='B' ){
                r2=i+1;
                c2=j+1;
                co++;
            }
        }
    }
int mx=max(abs(c2-c1),abs(r2-r1))+1;
int mn=min(n,m);

int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(mn<mx){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=101,r2=1,c1=101,c2=1,i,j,co=0,c,r;
    char s[101][101];
    cin>>n>>m;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' && co==0){
                r1=min(r1,i);
                r2=max(r2,i);
                c1=min(c1,j);
                c2=max(c2,j);
                co++;

            }
        }
    }
    c=c2-c1;
    r=r2-r1;
int mx=max(c,r);
mx=mx+1;

int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(r>m || c>n){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,r1=1,r2=1,c1=1,c2=1,i,j,co=0;
    char s[100][100];
    cin>>n>>m;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>s[i][j];
            if(s[i][j]=='B' && co==0){
                r1=i+1;
                c1=j+1;
                co++;
            }
            else if(s[i][j]=='B' ){
                r2=i+1;
                c2=j+1;
                co++;
            }
        }
    }
int mx=max(abs(c2-c1),abs(r2-r1))+1;
int mn=min(n,m);

int ans=0;
if(co==1)
    cout<<0<<endl;
else if(co==0)
    cout<<1<<endl;
else if(mn<mx){
    cout<<-1<<endl;
}
else{
        ans=(mx*mx)-co;
        cout<<ans<<endl;
    }

}
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ww = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        // cout << mni << ' ' << mnj << ' ' << mxi << ' ' << mxj << endl;
        if (bl == 0) {
                puts("1"); return 0;
        }
        bool nn = 0;
        int i; i = mxi;
        int j; j = mxj;
        while (i > mni || j > mnj) {
                i--; j--;
                if (i < 1 || j < 1) {
                        nn = 1;
                }
        }
        if (nn) {
                i = mxi;
                j = mnj;
                bool nnn = 0;
                while (i > mni || j < mxj) {
                        i--; j++;
                        if (i < 1 || j > m) {
                                nnn = 1;
                                break;
                        }
                }
                int x = i;
                int y = j;
                if (nnn) {
                        puts("-1"); return 0;
                }
                for (int i = x; i <= mxi; i++) {
                        for (int j = y; j >= mnj; j--) {
                                if (c[i][j] == 'W') ww++;
                        }
                }
                cout << ww;
                return 0;
        }
        x = i;
        y = j;
        // cout << i << ' ' << j << endl;
        for (i = x; i <= mxi; i++) {
                for (j = y; j <= mxj; j++) {
                        if (c[i][j] == 'W') {
                                ww++;
                        }
                }
        }
        cout << ww;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                cout << 1; return 0;
        }
        int raz = max(mxi - mni + 1, mxj - mnj + 1);
        if (mni == n) {
                raz = 1;
        }
        if (raz > n || raz > m) {
                puts("-1");
        } else {
                cout << raz * raz - bl;
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        int raz = max(mxi - mni + 1, mxj - mnj + 1);
        if (mni == n) {
                raz = 1;
        }
        if (raz > n || raz > m) {
                puts("-1");
        } else {
                cout << raz * raz - bl;
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                puts("1"); return 0;
        }
        bool no = 0;
        if (mxi - mni + 1 < mxj - mnj + 1) {
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mxi + raz > n) {
                        no = 1;
                }
                if (mxi + raz - mni == mxj - mnj && !no) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                        return 0;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                return 0;
        } else {
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mxj + raz > m) {
                        no = 1;
                }
                if (mxj + raz - mnj == mxi - mni && !no) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                        return 0;
                }
        }
        if (mxi - mni + 1 < mxj - mnj + 1) {
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mni - raz < 1) {
                        no = 1;
                }
                if (mxi - mni + raz == mxj - mnj && !no) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
        } else {
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mnj - raz < 1) {
                        no = 1;
                }
                if (mxj - mnj + raz == mxi - mni && !no) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                }
        }
        if (no) {
                puts("-1");
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        // cout << mni << ' ' << mnj << ' ' << mxi << ' ' << mxj << endl;
        if (bl == 0) {
                puts("1"); return 0;
        }
        int i = mxi;
        int j = mxj;
        while (i >= mni || j >= mnj) {
                if (i < 1 || j < 1) {
                        puts("-1");
                        return 0;
                }
                i--; j--;
        }
        i++; j++;
        int x = i;
        int y = j;
        // cout << i << ' ' << j << endl;
        int ww = 0;
        for (i = x; i <= mxi; i++) {
                for (j = y; j <= mxj; j++) {
                        if (c[i][j] == 'W') {
                                ww++;
                        }
                }
        }
        cout << ww;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                cout << 1; return 0;
        }
        int raz = max(mxi - mni + 1, mxj - mnj + 1);
        if (mni == inf) {
                raz = 1;
        }
        if (raz > n || raz > m) {
                puts("-1");
        } else {
                cout << raz * raz - bl;
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                cout << 1; return 0;
        }
        int raz = max(mxi - mni + 1, mxj - mnj + 1);
        if (mni == n + 1) {
                raz = 1;
        }
        if (raz > n || raz > m) {
                puts("-1");
        } else {
                cout << raz * raz - bl;
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        int raz = max(mxi - mni + 1, mxj - mnj + 1);
        if (mni == inf) {
                raz = 1;
        }
        if (raz > n || raz > m) {
                puts("-1");
        } else {
                cout << raz * raz - bl;
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                puts("1"); return 0;
        }
        bool no = 0, was = 0;
        if (mxi - mni + 1 < mxj - mnj + 1) {
                no = 0;
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mxi + raz > n) {
                        no = 1;
                }
                if (mxi + raz - mni == mxj - mnj && !no) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                        return 0;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                return 0;
        } else {
                no = 0;
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mxj + raz > m) {
                        no = 1;
                }
                if (mxj + raz - mnj == mxi - mni && !no) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                        return 0;
                }
        }
        if (mxi - mni + 1 < mxj - mnj + 1) {
                no = 0;
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mni - raz < 1) {
                        no = 1;
                }
                if (mxi - mni + raz == mxj - mnj && !no) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                        was = 1;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
        } else {
                no = 0;
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mnj - raz < 1) {
                        no = 1;
                }
                if (mxj - mnj + raz == mxi - mni && !no) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                        was = 1;
                }
        }
        if (no && !was) {
                puts("-1");
        }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e7 + 7;

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

char c[105][105];

int main () {
        int n, m, x, y; scanf ("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        cin >> c[i][j];
                }
        }
        int mni, mnj, mxi, mxj, ans = 0;
        mni = mnj = inf;
        mxi = mxj = -inf;
        int bl = 0;
        for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                        if (c[i][j] == 'B') {
                                mni = min(mni, i);
                                mnj = min(mnj, j);
                                mxi = max(mxi, i);
                                mxj = max(mxj, j);
                                bl++;
                        }
                }
        }
        if (bl == 0) {
                puts("1"); return 0;
        }
        bool no = 0;
        if (mxi - mni + 1 < mxj - mnj + 1) {
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mxi + raz > n) {
                        no = 1;
                }
                if (mxi + raz - mni == mxj - mnj) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                        return 0;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                return 0;
        } else {
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mxj + raz > m) {
                        no = 1;
                }
                if (mxj + raz - mnj == mxi - mni) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                        return 0;
                }
        }
        if (mxi - mni + 1 < mxj - mnj + 1) {
                int raz = (mxj - mnj + 1) - (mxi - mni + 1);
                if (mni - raz < 1) {
                        no = 1;
                }
                if (mxi - mni + raz == mxj - mnj) {
                        cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
                }
        } else if (mxi - mni + 1 == mxj - mnj + 1) {
                cout << (mxj - mnj + 1) * (mxj - mnj + 1) - bl;
        } else {
                int raz = (mxi - mni + 1) - (mxj - mnj + 1);
                if (mnj - raz < 1) {
                        no = 1;
                }
                if (mxj - mnj + raz == mxi - mni) {
                        cout << (mxi - mni + 1) * (mxi - mni + 1) - bl;
                }
        }
        if (no) {
                puts("-1");
        }
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    long left = m-1,right = 0,up = n-1,down = 0;
    long blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1<<endl;
        return 0;
    }
    else{
        int s1 = down-up+1;
        int s2 = right-left+1;

        if(s2 == s1)
            cout<<(s1*s1 - blacks)<<endl;

        else if(s2<s1){
            if((left>0) || (right<m-1)){
                cout<<(s1*s1 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
        else{
            if((up>0) || (down<n-1)){
                cout<<(s2*s2 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    long left = m-1,right = 0,up = n-1,down = 0;
    long blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if((blacks==0) || (left>right) || (up>down)){
        cout<<1<<endl;
        return 0;
    }
    else{
        int s1 = down-up+1;
        int s2 = right-left+1;

        if(s2 == s1)
            cout<<(s1*s1 - blacks)<<endl;

        else if(s2<s1){
            if((left>0) || (right<m-1)){
                cout<<(s1*s1 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
        else{
            if((up>0) || (down<n-1)){
                cout<<(s2*s2 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    int left = m,right = -1,up = n,down = -1;
    int blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1;
        return 0;
    }

    int s1 = down-up+1;
    int s2 = right-left+1;

    if(s2==s1)
        cout<<(s1*s1 - blacks);

    else if(s2<s1){
        if((left>0) || (right<m-1)){
            cout<<(s1*s1 - blacks);
        }
        else
            cout<<-1;
    }
    else{
        if((up>0) || (down<n-1)){
            cout<<(s2*s2 - blacks);
        }
        else
            cout<<-1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    long left = m,right = -1,up = n,down = -1;
    long blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1<<endl;
        return 0;
    }

    int s1 = down-up+1;
    int s2 = right-left+1;

    if(s2 == s1)
        cout<<(s1*s1 - blacks)<<endl;

    else if(s2<s1){
        if((left>0) || (right<m-1)){
            cout<<(s1*s1 - blacks)endl;
        }
        else
            cout<<-1<<endl;
    }
    else{
        if((up>0) || (down<n-1)){
            cout<<(s2*s2 - blacks)<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    int left = m,right = -1,up = n,down = -1;
    int blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1;
        return 0;
    }

    int s1 = down-up+1;
    int s2 = right-left+1;

    if(s2==s1)
        cout<<s1*s1 - blacks;

    else if(s2<s1){
        if(left>0 || right<m-1){
            cout<<s1*s1 - blacks;
        }
        else
            cout<<-1;
    }
    else{
        if(up>0 || down<n-1){
            cout<<s2*s2 - blacks;
        }
        else
            cout<<-1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    long left = m,right = -1,up = n,down = -1;
    long blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1<<endl;
        return 0;
    }

    int s1 = down-up+1;
    int s2 = right-left+1;

    if(s2 == s1)
        cout<<(s1*s1 - blacks)<<endl;

    else if(s2<s1){
        if((left>0) || (right<m-1)){
            cout<<(s1*s1 - blacks)<<endl;
        }
        else
            cout<<-1<<endl;
    }
    else{
        if((up>0) || (down<n-1)){
            cout<<(s2*s2 - blacks)<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i];

    long left = m-1,right = 0,up = n-1,down = 0;
    long blacks = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(arr[i][j] == 'B'){
                blacks++;
                if(i<up)
                    up = i;
                if(i>down)
                    down = i;
                if(j<left)
                    left = j;
                if(j>right)
                    right = j;
            }
        }
    }

    if(blacks==0){
        cout<<1<<endl;
        return 0;
    }
    else{
        int s1 = down-up+1;
        int s2 = right-left+1;

        if(s2 == s1)
            cout<<(s1*s1 - blacks)<<endl;

        else if(s2<s1){
            if(m>=s1){
                cout<<(s1*s1 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
        else{
            if(n>=s2){
                cout<<(s2*s2 - blacks)<<endl;
            }
            else
                cout<<-1<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll i,j,n,m,r=0,c=0,res=0,d=0,total=0,mx=0,mn=1e15;
    cin>>n>>m;
    bool f=0;
    string s[n+1];
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<=n; i++)
    {
        f=c=d=0;
        for(j=0; j<m; j++)
        {
            if(s[i][j]=='B'&& f==0)
                f=1,c=j+1;
            if(s[i][j]=='B')
                total++,d=j+1;
        }
        if(f)
        {
            r++;
            mn = min(mn,c);
            mx = max(mx,d);
        }
    }
    res = max((mx-mn+1),r);
    res*=res;
    if(total==m*m && total==n*n)
        cout<<0<<endl;
    else if(mx>m || mx>n)
        cout<<-1<<endl;
    else
    {
        ll ans=0;
        if(res-total==0)
            ans=1;
        else
            ans=res-total;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll i,j,n,m,r=0,c=0,res=0,d=0,cl=0,col=0,total=0,mx=0,mn=1e15;
    cin>>n>>m;
    bool f=0,flag=0;
    string s[n+1];
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<=n; i++)
    {
        f=c=d=0;
        for(j=0; j<m; j++)
        {
            if(s[i][j]=='B'&&flag==0)
                cl=i,flag=1;
            if(s[i][j]=='B'&& f==0)
                f=1,c=j+1;
            if(s[i][j]=='B')
                total++,d=j+1,col = i;
        }
        if(f)
        {
            mn = min(mn,c);
            mx = max(mx,d);
        }
    }
    res = max((mx-mn+1),col-cl+1);
    res*=res;//cout<<res<<" "<<col<<" "<<cl<<endl;

    if(n==1 && m>1&&total>1)
       cout<<-1<<endl;
    else if(n>1&&m==1&&total>1)
        cout<<-1<<endl;
    else if((mx-mn+1)>m ||(col-cl+1)>n)
         cout<<-1<<endl;
    else if(total==m*n)
        cout<<0<<endl;
    else
    {
        ll ans=0;
        if(total==0)
            ans=1;
        else
            ans=res-total;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll i,j,n,m,r=0,c=0,res=0,d=0,cl=0,col=0,total=0,mx=0,mn=1e15;
    cin>>n>>m;
    bool f=0,flag=0;
    string s[n+1];
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<=n; i++)
    {
        f=c=d=0;
        for(j=0; j<m; j++)
        {
            if(s[i][j]=='B'&&flag==0)
                cl=i,flag=1;
            if(s[i][j]=='B'&& f==0)
                f=1,c=j+1;
            if(s[i][j]=='B')
                total++,d=j+1,col = i;
        }
        if(f)
        {
            mn = min(mn,c);
            mx = max(mx,d);
        }
    }
    res = max((mx-mn+1),col-cl+1);
    res*=res;//cout<<res<<" "<<col<<" "<<cl<<endl;

    if(n==1 && m>1&&total>1)
       cout<<-1<<endl;
    else if(n>1&&m==1&&total>1)
        cout<<-1<<endl;
    else if((mx-mn+1)>m ||(mx-mn+1)>n||(col-cl+1)>m||(col-cl+1)>n)
         cout<<-1<<endl;
    else if(total==m*n)
        cout<<0<<endl;
    else
    {
        ll ans=0;
        if(total==0)
            ans=1;
        else
            ans=res-total;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll i,j,n,m,r=0,c=0,total=0,mx=0;
    cin>>n>>m;
    bool f=0;
    string s[n+1];
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<=n; i++)
    {
        f=c=0;
        for(j=0; j<m; j++)
        {
            if(s[i][j]=='B')
                c++,total++,f=1;
        }
        if(f)
            r++;
        mx=max(mx,c);
    }
    mx=max(mx,r);
    if(total==m*m && total==n*n)
        cout<<0<<endl;
    else if(mx>m || mx>n)
        cout<<-1<<endl;
    else
    {
        ll ans=0;
        if((mx*mx)-total==0)
            ans=1;
        else
         ans=(mx*mx)-total;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ll i,j,n,m,r=0,c=0,res=0,d=0,cl=0,col=0,total=0,mx=0,mn=1e15;
    cin>>n>>m;
    bool f=0,flag=0;
    string s[n+1];
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<=n; i++)
    {
        f=c=d=0;
        for(j=0; j<m; j++)
        {
            if(s[i][j]=='B'&&flag==0)
                cl=i,flag=1;
            if(s[i][j]=='B'&& f==0)
                f=1,c=j+1;
            if(s[i][j]=='B')
                total++,d=j+1,col = i;
        }
        if(f)
        {
            mn = min(mn,c);
            mx = max(mx,d);
        }
    }
    res = max((mx-mn+1),col-cl+1);
    res*=res;//cout<<res<<" "<<col<<" "<<cl<<endl;

    if(n==1 && m>1&&total>1)
       cout<<-1<<endl;
    else if(n>1&&m==1&&total>1)
        cout<<-1<<endl;
    else if(total==m*n)
        cout<<0<<endl;
    else
    {
        ll ans=0;
        if(total==0)
            ans=1;
        else
            ans=res-total;
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = 200;
    int Min_y = 200;
    int Max_x = -1;
    int Max_y = -1;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < n; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < n; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;

            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = m - 1;
    int Min_y = n - 1;
    int Max_x = 0;
    int Max_y = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < m; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < n; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;
            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = n - 1;
    int Min_y = m - 1;
    int Max_x = 0;
    int Max_y = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < n; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < m; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;
            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = n - 1;
    int Min_y = n - 1;
    int Max_x = 0;
    int Max_y = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < m; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < n; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;
            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = n - 1;
    int Min_y = n - 1;
    int Max_x = 0;
    int Max_y = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < m; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < n; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;
            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char> > a(n, vector<char>(m));
    int kol = 0;
    for (int i = 0; i < n; ++i)
    {

        for (int j = 0; j < m; ++j)
        {
            cin >> a[i][j];
            kol += (a[i][j] == 'B');
        }
    }
    if (kol == 0)
    {
        cout << 1 << endl;
        return 0;
    }
    int Min_x = n - 1;
    int Min_y = n - 1;
    int Max_x = 0;
    int Max_y = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (a[i][j] == 'B')
            {
                Min_x = min(Min_x, i);
                Min_y = min(Min_y, j);
                Max_x = max(Max_x, i);
                Max_y = max(Max_y, j);
            }
        }
    }
    int e = 1e4 + 179;
    for (int x1 = 0; x1 <= Min_x; ++x1)
    for (int x2 = Max_x; x2 < n; ++x2)
    for (int y1 = 0; y1 <= Min_y; ++y1)
    for (int y2 = Max_y; y2 < n; ++y2)
    {
        if (x2 - x1 == y2 - y1)
        {
            if (e > x2 - x1 + 1)
            {
                e = x2 - x1 + 1;
            }
        }
    }
    if (e == 1e4 + 179)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << e * e - kol << endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                sum++;
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    //printf("%d %d %d %d\n",l,r,up,down);
    if(sum==0)return puts("1");
	int xd=down-up+1,yd=r-l+1;
	int mx = max(xd,yd);
	printf("%d\n",mx<=n&&mx<=m?mx*mx-sum:-1);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                sum++;
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    //printf("%d %d %d %d\n",l,r,up,down);
    if(sum==0)
    {
        puts("1");
        return 0;
    }
    else
    {
        if(r-l == down-up);
        else if(down-up > r-l)
        {
            if(m < (down-up+1))
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(down-up+1)*(down-up+1)-sum);
        }
        else
        {
            if(n < (r-l)+1)
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(r-l+1)*(r-l+1)-sum);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    if(l==101)
    {
        puts("1");
        return 0;
    }
    else
    {
        if(r-l == down-up)
        {
            puts("0");
            return 0;
        }
        else if(down-up > r-l)
        {
            if(r-(down-up) >= 0)
                l = r-(down-up);
            else if(l+(down-up) < m)
                r = l+(down-up);
            else
            {
                puts("-1");
                return 0;
            }
        }
        else
        {
            if(down-(r-l) >= 0)
                up = down-(r-l);
            else if(up+(r-l) < n)
                down = up+(r-l);
            else
            {
                puts("-1");
                return 0;
            }
        }
        int ans = 0;
        for(int i = up; i <=down; i++)
            for(int j = l; j <= r; j++)
                if(s[i][j] == 'W')
                    ans ++;
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                sum++;
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    //printf("%d %d %d %d\n",l,r,up,down);
    if(sum==0)
    {
        puts("1");
        return 0;
    }
    else
    {
        if(r-l == down-up);
        else if(down-up > r-l)
        {
            if(n < (down-up+1))
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(down-up+1)*(down-up+1)-sum);
        }
        else
        {
            if(m < (r-l)+1)
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(r-l+1)*(r-l+1)-sum);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                sum++;
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    //printf("%d %d %d %d\n",l,r,up,down);
    if(l==101)
    {
        puts("1");
        return 0;
    }
    else
    {
        if(r-l == down-up);
        else if(down-up > r-l)
        {
            if(m < (down-up+1))
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(down-up+1)*(down-up+1)-sum);
        }
        else
        {
            if(n < (r-l)+1)
            {
                puts("-1");
                return 0;
            }
            printf("%d\n",(r-l+1)*(r-l+1)-sum);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
//#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=110;
const LL mod = 1e9+7;

char s[N][N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
        scanf("%s",s[i]);
    int l=101,r=-1,up=101,down=-1;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(s[i][j] == 'B')
            {
                l = min(l,j);
                r = max(r,j);
                up = min(up,i);
                down = max(down,i);
            }
        }
    }
    if(l==101)
    {
        puts("1");
        return 0;
    }
    else
    {
        if(r-l == down-up);
        else if(down-up > r-l)
        {
            if(r-(down-up) >= 0)
                l = r-(down-up);
            else if(l+(down-up) < m)
                r = l+(down-up);
            else
            {
                puts("-1");
                return 0;
            }
        }
        else
        {
            if(down-(r-l) >= 0)
                up = down-(r-l);
            else if(up+(r-l) < n)
                down = up+(r-l);
            else
            {
                puts("-1");
                return 0;
            }
        }
        int ans = 0;
        for(int i = up; i <=down; i++)
            for(int j = l; j <= r; j++)
                if(s[i][j] == 'W')
                    ans ++;
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
main()
{
    int n,m,i,j,r1=105,c1=105,r2=0,c2=0,ans,b=0;
    cin>>n>>m;
    char arr[n][m];
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
        cin>>arr[i][j];
        if(arr[i][j]=='B')
        {
            b++;
            r1=min(r1,i);
            r2=max(r2,i);
            c1=min(c1,j);
            c2=max(c2,j);
        }
    }
    }

    if(r1==105)
        {
            cout<<1<<endl;
            return 0;
        }

else{

        ans=max(r2-r1,c2-c1)+1;
       // cout<<ans<<" "<<b<<endl;

        if(ans>n||ans>m)
            cout<<-1<<endl;
        else
            cout<<ans*ans-b<<endl;
}
        return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i,j,r1=1000,c1=1000,r2=0,c2=0,ans,b=0;
    cin>>n>>m;
    char arr[205][205];
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
        cin>>arr[i][j];
        if(arr[i][j]=='B')
        {
            b++;
            r1=min(r1,i);
            r2=max(r2,i);
            c1=min(c1,j);
            c2=max(c2,j);
        }
    }
    }

    if(c2==0)
            cout<<1<<endl;

else{
        ans=max(r2-r1,c2-c1)+1;

        if(ans>n||ans>m)
            cout<<-1<<endl;
        else
            cout<<ans*ans-b<<endl;
}
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i,j,r1=105,c1=105,r2=0,c2=0,ans,b=0;
    cin>>n>>m;
    char arr[n][m];
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
        cin>>arr[i][j];
        if(arr[i][j]=='B')
        {
            b++;
            r1=min(r1,i);
            r2=max(r2,i);
            c1=min(c1,j);
            c2=max(c2,j);
        }
    }
    }

    if(c2==0)
        {
            cout<<1<<endl;
            return 0;
        }

else{

        ans=max(r2-r1,c2-c1)+1;

        if(ans>n||ans>m)
            cout<<-1<<endl;
        else
            cout<<ans*ans-b<<endl;
}
        return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r, sum = 0;
    char c;
    bool hasb = 0;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                ++sum;
                t = min(i, t);
                l = min(j, l);
                b = max(i, b);
                r = max(j, r);
            }
        }
    int len = max(b - t + 1, r - l + 1);
    if (!sum)
        cout << 1;
    else if (len > n || len > m)
        cout << -1;
    else
        cout << len * len - sum;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    bool hasb = 0;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                hasb = 1;
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    int len = max(b - t + 1, r - l + 1);
    if (!hasb)
    {
        cout << 1;
        return 0;
    }
    if (len > n || len > m)
    {
        cout << -1;
        return 0;
    }
    int anf = 0, ans = 0;
    // cout << t << " " << b << " " << l << " " << r << " " << len << endl;
    if (b + 1 - len >= 0 && r + 1 - len >= 0)
        for (int i = b; i > b - len; --i)
            for (int j = r; j > r - len; --j)
                if (ar[i][j] == 'W')
                    anf++;
    if (t + len <= n && l + len <= m)
        for (int i = t; i < t + len; ++i)
            for (int j = l; j < l + len; ++j)
                if (ar[i][j] == 'W')
                    ans++;
    // cout << anf << " " << ans;
    if (!anf)
        cout << ans;
    else if (!ans)
        cout << anf;
    else
        cout << min(ans, anf);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r, sum = 0;
    char c;
    bool hasb = 0;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                ++sum;
                t = min(i, t);
                l = min(j, l);
                b = max(i, b);
                r = max(j, r);
            }
        }
    int len = max(b - t + 1, r - l + 1);
    if (!sum)
        cout << 1;
    else if (len > n || len > m)
        cout << -1;
    cout << len * len - sum;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    int len =max(b - t + 1, r - l + 1);
    if (len > n || len > m)
    {
        cout << -1;
        return 0;
    }
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = min(min(n, m), max(b - t + 1, r - l + 1));
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == 199 && t == l && r == -1 && r == b || t ==l && t ==r && t==b)
        cout << 1;
    else if (ans < 1 || b - len > t || r - len > l)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
        int len = min(min(n, m), max(b - t + 1, r - l + 1));
        // cout << t << " " << b << " " << l << " " << r << " " << len << endl;
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == l && t == r && t == b)
        cout << 0;
    else if (t == 199 && t == l && r == -1 && r == b)
        cout << 1;
    else if (b - len > t || r - len > l)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = min(min(n, m), max(b - t + 1, r - l + 1));
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == 199 && t == l && r == -1 && r == b)
        cout << 1;
    else if (ans < 1 || b - len > t || r - len > l)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = min(min(n, m), max(b - t + 1, r - l + 1));
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == 199 && t == l && r == -1 && r == b || t ==l && t ==r || t==b)
        cout << 1;
    else if (ans < 1 || b - len > t || r - len > l)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    bool hasb = 0;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                hasb = 1;
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    int len = max(b - t + 1, r - l + 1);
    if (!hasb)
    {
        cout << 1;
        return 0;
    }
    if (len > n || len > m)
    {
        cout << -1;
        return 0;
    }
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = max(b - t + 1, r - l + 1);
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == 199 && t == l && r == -1 && r == b)
        cout << 1;
    else if (ans < 1)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = min(min(n, m), max(b - t + 1, r - l + 1));
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == 199 && t == l && r == -1 && r == b)
        cout << 1;
    else if (ans < 1)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    int t, b, l, r;
    char c;
    array<array<char, 103>, 103> ar;
    t = l = 199;
    b = r = -1;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> c;
            ar[i][j] = c;
            if (c == 'B')
            {
                if (i < t)
                    t = i;
                if (j < l)
                    l = j;
                if (i > b)
                    b = i;
                if (j > r)
                    r = j;
            }
        }
    // cout << t << " " << b << " " << l << " " << r << endl;
    int len = min(min(n, m), max(b - t + 1, r - l + 1));
    int ans = 0;
    for (int i = b; i > b - len; --i)
        for (int j = r; j > r - len; --j)
            if (ar[i][j] == 'W')
                ans++;
    if (t == l && t == r && t == b)
        cout << 0;
    else if (t == 199 && t == l && r == -1 && r == b)
        cout << 1;
    else if (ans < 1 || b - len > t || r - len > l)
        cout << -1;
    else
        cout << ans;
    return 0;
}


#include<cstdio>
char a;
int l, h, ans, count, temp;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		l = 0;
		h = 0;
		ans = 0;
		count = 0;
		temp = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			int flag = 0;
			l = 0;
			for (int j = 1;j <= m;j++)
			{

				scanf("%c", &a);
				if (a == 'B')
				{
					count++;
					l++;
					flag = 1;
				}
				if (l > temp)temp = l;
			}
			if (flag) h++;
		}
		if (h == 1 && count == 0) ans = 1;
		else if (h == 1 && count == 1) ans = 0;
		else if (h == 1 && count > 1) ans = -1;
		else if (h == 0 && count == 0) ans = 1;
		else
		{
			if (temp > h)h = l;
			ans = h*h;
			ans -= count;
		}
		printf("%d\n", ans);
	}return 0;
}


#include<cstdio>
char a;
int l, h, ans, count, temp;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		l = 0;
		h = 0;
		ans = 0;
		count = 0;
		temp = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			int flag = 0;
			l = 0;
			for (int j = 1;j <= m;j++)
			{

				scanf("%c", &a);
				if (a == 'B')
				{
					count++;
					l++;
					flag = 1;
				}
				if (l > temp)temp = l;
			}
			if (flag) h++;
		}
		if (count == 0) ans = 1;
		else if (h == 1 && count == 1) ans = 0;
		else if (h == 1 && count > 1) ans = -1;
		else if (h == 0 && count == 0) ans = -1;
		else if (h > m)ans = -1;
		else if (temp > n)ans = -1;
		else
		{
			if (temp > h)h = l;
			ans = h*h;
			ans -= count;
		}
		printf("%d\n", ans);
	}return 0;
}


#include<cstdio>
char a;
int l, h, ans, count, temp;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		l = 0;
		h = 0;
		ans = 0;
		count = 0;
		temp = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			int flag = 0;
			l = 0;
			for (int j = 1;j <= m;j++)
			{

				scanf("%c", &a);
				if (a == 'B')
				{
					count++;
					l++;
					flag = 1;
				}
				if (l > temp)temp = l;
			}
			if (flag) h++;
		}
		if (h == 1 && count == 0) ans = 1;
		else if (h == 1 && count == 1) ans = 0;
		else if (h == 1 && count > 1) ans = -1;
		else if (h == 0 && count == 0) ans = 1;
		else if (h > m)ans = -1;
		else if (temp > n)ans = -1;
		else
		{
			if (temp > h)h = l;
			ans = h*h;
			ans -= count;
		}
		printf("%d\n", ans);
	}return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
char a;
int l[100], h[100], ans, cou, temp, left, right;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		ans = 0;
		cou = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			for (int j = 1;j <= m;j++)
			{
				scanf("%c", &a);
				if (a == 'B')
				{
					l[j] = 1;
					h[i] = 1;
					cou++;
				}
			}
		}
		for (int i = 1;i <= m;i++)
			if (l[i] == 1)temp++;
		for (int i = 1;i <= n;i++)
		{
			if (h[i] == 1)
			{
				left = i;
				break;
			}
		}
		for (int i = 1;i <= n;i++)
		{
			if (h[i] == 1) right = i;
		}
		temp = max(temp, right - left + 1);
		if (cou == 0) ans = 1;
		else if (temp == 1 && cou == 1)ans = 0;
		else if (temp == 1 && cou > 1)ans = -1;
		else if (temp > m || temp > n)ans = -1;
		else ans = temp*temp - cou;
		printf("%d\n", ans);
	}return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
char a;
int lx=1000, rx=0, ly=1000, ry=0, ans, cou, temp, left, right;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		ans = 0;
		cou = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			for (int j = 1;j <= m;j++)
			{
				scanf("%c", &a);
				if (a == 'B')
				{
					lx = min(lx, j);
					rx = max(rx, j);
					ly = min(ly, i);
					ry = max(ry, i);
					cou++;
				}
			}
		}
		temp = max(rx - lx, ry - ly) + 1;
		if (cou == 0) ans = 1;
		else if (temp == 1 && cou == 1)ans = 0;
		else if (temp > m || temp > n)ans = -1;
		else ans = temp*temp - cou;
		printf("%d\n", ans);
	}return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
char a;
int lx=1000, rx=0, ly=1000, ry=0, ans, cou, temp, left, right;

int main()
{
	int m, n;
	while (~scanf("%d%d", &n, &m))
	{
		ans = 0;
		cou = 0;
		for (int i = 1;i <= n;i++)
		{
			getchar();
			for (int j = 1;j <= m;j++)
			{
				scanf("%c", &a);
				if (a == 'B')
				{
					lx = min(lx, j);
					rx = max(rx, j);
					ly = min(ly, i);
					ry = max(ry, i);
					cou++;
				}
			}
		}
		temp = max(lx - rx, ry - ly) + 1;
		if (cou == 0) ans = 1;
		else if (temp == 1 && cou == 1)ans = 0;
		else if (temp > m || temp > n)ans = -1;
		else ans = temp*temp - cou;
		printf("%d\n", ans);
	}return 0;
}


/*
 * socode.online
 *
 * Author: 李策
 * E-mail: 691397536@qq.com
 * Time  : 2020-04-10 18:37:56
 */
#include <bits/stdc++.h>

using namespace std;

char s[105][105];

int main() {
    int n, m;
    scanf("%d %d",&n,&m);
    int u, d, l, r, cnt = 0;
    u = l = 0x3f3f3f3f, d = r = -1;
    for( int i = 0; i < n; i++ ) scanf("%s",s[i]);
    for( int i = 0; i < n; i++ ) {
        for( int j = 0; j < m; j++ ) {
            if( s[i][j] == 'B' ) {
                cnt++;
                u = min( u, i+1 ), d = max( d, i+1 ) , l = min( l, j+1 ), r = max( r, j+1 );
            }
        }
    }
    if( cnt == 0 ) {
        puts("1");
        return 0;
    }
    int side = max(r-l+1,d-u+1);
    if( n < side || m < side ) puts("-1");
    else printf("%d\n",side*side-cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,ax=INF,bx,ay=INF,by,sum;
char s[105][105];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='B'){
				sum++;
				ax=min(ax,i);
				bx=max(bx,i);
				ay=min(ay,j);
				by=max(by,j);
			}
		}
	}
	if(sum==0){
		cout<<1<<endl;
		return 0;
	}
	int t=max(bx-ax,by-ay)+1;
	if(t>m||t>n)cout<<-1<<endl;
	else cout<<t*t-sum<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
string mapp[105];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>mapp[i];
    int sx=0x3f3f3f3f,sy=0x3f3f3f3f,ex=0,ey=0,cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(mapp[i][j]=='B'){
                sx=min(sx,i);sy=min(sy,j);
                ex=max(ex,i);ey=max(ey,j);
            }
        }
    }
    if(cnt==0){
        cout<<1;
        return 0;
    }
    int len=max(ex-sx+1,ey-sy+1);
    if(len>n||len>m){
        cout<<-1;
        return 0;
    }
    cout<<len*len-cnt;
    return 0;
}


/*
 * socode.online
 *
 * Author: 小螺号
 * E-mail: 598601767@qq.com
 * Time  : 2020-04-10 18:39:56
 */
#include <bits/stdc++.h>
using namespace std;

char s[105][105];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
    }
    int l=105;
    int r=0;
    int u=105;
    int d=0;
    int cnt=0;
    int ans=0;
    int flag=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                flag=1;
                l=min(l,i);
                r=max(r,i);
                u=min(u,j);
                d=max(d,j);
                cnt++;
            }
        }
    }
    if(flag==1){
        int Max=max(r-l,d-u);
        if(Max+1>n||Max+1>m){
            printf("-1\n");
        }
        else{
            ans=(Max+1)*(Max+1)-cnt;
            printf("%d\n",ans);
        }

    }
    else{
        printf("1\n");
    }
    return 0;
}