#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
int index[100001];
int main()
{
    int n;
    cin>>n;
    int i;
    int a[n], m=0;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        index[a[i]]++;
        m=max(a[i],m);
    }
    long Max1=0, Max2=0;
    for(i=1;i<=m;i+=2)
        Max1+=(index[i]*i);
    for(i=2;i<=m;i+=2)
        Max2+=(index[i]*i);
    cout<<max(Max1,Max2);
    return 0;
}


#include <iostream>
using namespace std;
long long index[100005];
long long ans[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        long long t;
        cin>>t;
        index[t]++;
        M=max(M,t);
    }
    ans[1]=index[1];
    for(i=2;i<=M;i++)
    {
        long long k=index[i]*i+index[i-2]*(i-2);
        ans[i]=max(k,ans[i-1]);
    }
    cout<<ans[M];
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
long long index[100001];
long long ans[100001];
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    int m=0;
    for(i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        index[t]++;
        m=max(t,m);
    }
    ans[1]=index[1];
    for(i=2;i<=m;i++)
    {
        ans[i]=(ans[i-2])+(index[i]*i);
        ans[i]=max(ans[i-1],ans[i]);
    }
    cout<<ans[m];
    return 0;
}


#include <iostream>
using namespace std;
long long index[100005];
long long ans[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        long long t;
        cin>>t;
        index[t]++;
        M=max(M,t);
    }
    ans[1]=index[1];
    for(i=2;i<=M;i++)
    {
        index[i]*=i;
        ans[i]=max(index[i]+index[i-1],ans[i-1]);
    }
    cout<<ans[M];
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max4=0,t,k=0,f=1,d=0,Max5=0,Max6=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    for (i=1;i<=
    n;i++)
    {
        if (b[i]%2==0)
            Max5+=b[i];
        else
            Max6+=b[i];
    }
    f=0;
    t=max(Max1*k,Max4);
    f=max(f,t);
    f=max(f,Max5);
    f=max(f,Max6);
    if(f==8)
        cout<<f+1<<endl;
    else
        cout<<f<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
long long index[100001];
long long ans[100001];
int main()
{
    int n;
    cin>>n;
    int i;
    int m=0;
    for(i=0;i<n;i++)
    {
        int t;
        cin>>t;
        index[t]++;
        m=max(t,m);
    }
    ans[1]=index[1];
    for(i=2;i<=m;i++)
    {
        ans[i]=(ans[i-2])+(index[i]*i);
        ans[i]=max(ans[i-1],ans[i]);
    }
    cout<<ans[m];
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max4=0,t,k=0,f=1,d=0,Max5=0,Max6=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    for (i=1;i<=
    n;i++)
    {
        if (b[i]%2==0)
            Max5+=b[i];
        else
            Max6+=b[i];
    }
    f=0;
    t=max(Max1*k,Max4);
    f=max(f,t);
    f=max(f,Max5);
    f=max(f,Max6);
    cout<<f<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max4=0,t,k=0,f=1,d=0,Max5=0,Max6=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    for (i=0;i<n;i++)
    {
        if (b[i]%2==0)
            Max5+=b[i];
        else
            Max6+=b[i];
    }
    f=0;
    t=max(Max1*k,Max4);
    f=max(f,t);
    f=max(f,Max5);
    f=max(f,Max6);
    cout<<f<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max2=0,Max3=0,Max4=0,t,k=0,f=1,d=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
    {
        if (i%2==0)
            Max2+=b[i];
        else
            Max3+=b[i];
    }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    t=max(Max1*k,Max2);
    d=max(Max3,Max4);
    f=max(t,d);
    cout<<f<<endl;
    return 0;
}


#include <iostream>
using namespace std;
long long a[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        long long t;
        cin>>t;
        a[t]+=t;
        M=max(M,t);
    }
    for(i=2;i<=M;i++)
        a[i]=max(a[i]+a[i-2],a[i-1]);
    cout<<a[M];
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
long long index[100001];
long long ans[100001];
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    int m=0;
    for(i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        index[t]++;
        m=max(t,m);
    }
    ans[1]=index[1];
    for(i=2;i<=m;i++)
    {
        ans[i]=(ans[i-2])+(index[i]*i);
        ans[i]=max(ans[i-1],ans[i]);
    }
    printf("%d",ans[m]);
    return 0;
}


#include <iostream>
using namespace std;
long long index[100005];
long long ans[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        long long t;
        cin>>t;
        index[t]++;
        M=max(M,t);
    }
    ans[1]=index[1];
    for(i=2;i<=M;i++)
    {
        index[i]*=i;
        ans[i]=max(index[i]+index[i-2],ans[i-1]);
    }
    cout<<ans[M];
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max2=0,t,k=0,f=1,d=0,Max5=0,Max6=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
    {
        if (i%2==0)
            Max2+=b[i];
        else
            Max3+=b[i];
    }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    for (i=0;i<n;i++)
    {
        if (b[i]%2==0)
            Max5+=b[i];
        else
            Max6+=b[i];
    }
    f=0;
    t=max(Max1*k,Max4);
    f=max(f,t);
    f=max(f,Max5);
    f=max(f,Max6);
    cout<<f<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
int main()
{
    int n;
    cin>>n;
    int i;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    long ans=0;
    for(i=n-1;i>=0;i-=2)
        ans+=a[i];
    cout<<ans;
    return 0;
}


#include <iostream>
using namespace std;
long long a[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        long long t;
        cin>>t;
        a[t]++;
        M=max(M,t);
    }
    for(i=2;i<=M;i++)
    {
        a[i]*=i;
        a[i]=max(a[i]+a[i-2],a[i-1]);
    }
    cout<<a[M];
    return 0;
}


#include <iostream>
using namespace std;
int a[100000];
int main()
{
    int n;
    cin>>n;
    int i,t,temp=0,Max=0,b[n],k=0;
    if (n==1)
    {
        cin>>t;
        cout<<t<<endl;
        return 0;
    }
    for (i=0;i<n;i++)
    {
        cin>>t;
        b[k++]=t;
        a[t]++;
        temp=max(t,temp);
    }
    for (i=0;i<=temp;i++)
        if (a[i]>Max)
        {
            Max=a[i];
            t=i;
        }
    if (Max>1)
        cout<<t*Max<<endl;
    else
    {
        t=0;
        if(temp==2)
        {
            if(b[0]>=b[1])
                cout<<b[0]<<endl;
            else
                cout<<b[1]<<endl;
        }
        if(temp>2&&temp%2==0)
        {
            for (i=1;i<=temp;i++)
                if (i%2==0)
                    t+=i;
            cout<<t<<endl;
        }
        if(temp>2 && temp%2!=0)
        {
            for (i=1;i<=temp;i++)
                if (i%2!=0)
                    t+=i;
            cout<<t<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
int main()
{
    int n;
    cin>>n;
    int i;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    long long ans=0;
    if(n==1)
    {
        cout<<a[0];
        return 0;
    }
    if(n==2)
    {
        cout<<max(a[0],a[1]);
        return 0;
    }
    for(i=1;i<n-1;i++)
    {
        if(a[i]>a[i-1] && a[i]>=a[i+1])
        {
            ans+=a[i];
            a[i-1]=0;
            a[i+1]=0;
            i++;
            continue;
        }
        if(a[i+1]>a[i])
        {
            if(i+1==n-1)
            {
                ans+=a[i-1]+a[i+1];
                a[i]=0;
                a[i-1]=0;
                a[i+1]=0;
                i++;
                continue;
            }
        }
        if(a[i]==a[i-1])
        {
            ans+=a[i-1];
            a[i-1]=0;
            a[i]=0;
            continue;
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
using namespace std;
long long index[100005];
long long ans[100005];
long long n,i,M=0;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        int t;
        cin>>t;
        index[t]++;
        M=max(M,t);
    }
    ans[1]=index[1];
    for(i=2;i<=M;i++)
    {
        long long k=index[i]*i+index[i-2]*(i-2);
        ans[i]=max(k,ans[i-1]);
    }
    cout<<ans[M];
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <time.h>
#define null NULL
using namespace std;
long long index[100001];
long long ans[100001];
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    int m=0;
    for(i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        index[t]++;
        m=max(t,m);
    }
    ans[1]=index[1];
    for(i=2;i<=m;i++)
    {
        ans[i]=(ans[i-2])+(index[i]*i);
        ans[i]=max(ans[i-1],ans[i]);
    }
    printf("%I64d",ans[m]);
    return 0;
}


#include <iostream>
using namespace std;
int a[100000],b[100000];
int main()
{
    int n;
    cin>>n;
    long i,Max1=0,Max2=0,Max3=0,Max4=0,t,k=0,f=1,d=0,Max5=0,Max6=0;
    for (i=1;i<=n;i++)
    {
        cin>>t;
        a[t]++;
        b[f++]=t;
        d=max(d,t);
    }
    for (i=1;i<=d;i++)
        if(a[i]>Max1)
        {
            Max1=a[i];
            k=i;
        }
    for (i=1;i<=n;i++)
    {
        if (i%2==0)
            Max2+=b[i];
        else
            Max3+=b[i];
    }
    for (i=1;i<=n;i++)
        if(b[i]>Max4)
            Max4=b[i];
    for (i=0;i<n;i++)
    {
        if (b[i]%2==0)
            Max5+=b[i];
        else
            Max6+=b[i];
    }
    t=max(Max1*k,Max2);
    d=max(Max3,Max4);
    f=max(t,d);
    f=max(f,Max5);
    f=max(f,Max6);
    cout<<f<<endl;
    return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#define INF 0x3f3f3f3f

using namespace std;

int main()
{
	int n;
	cin >> n;
	int num[100005] = {0};
	int dp[100005] = {0};
	int temp;
	int maxn = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		num[temp]++;
		maxn = max(maxn, temp);
	}
	dp[0] = 0;
	dp[1] = num[1];
	for (int i = 2; i <= maxn; i++)
		dp[i] = max(dp[i-1], dp[i-2]+i*num[i]);
	cout << dp[maxn] << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;

long long a[100010];
long long dp[100010][2];

int main()
{
	long long n,x,i,j,maxn,maxl;
	while(cin>>n)
	{
		memset(a,0,sizeof(a));
		maxn=0;
		for(i=0;i<n;i++)
		{
			cin>>x;
			a[x]+=x;
			if(x>maxn)
			maxn=x;
		}
		dp[1][1]=a[1];
		dp[2][1]=a[2];
		dp[2][0]=a[1];
		for(i=3;i<=maxn;i++)
		{
			dp[i][1]=max(dp[i-2][0],dp[i-2][1])+a[i];
			dp[i][0]=max(dp[i-1][1],dp[i-1][0]);
		}
		cout<<max(dp[maxn][0],dp[maxn][1])<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int zhi[100005],jieguo[100005];
int main()
{
	int t=0,n=0,m=0,i=0;
	for(i=0;i<100003;i++)
	zhi[i]=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&n);
		zhi[n]+=n;
	}
	jieguo[1]=zhi[1];
	jieguo[2]=zhi[2];
	for(i=3;i<=100002;i++)
	{
		if(jieguo[i-2]>jieguo[i-3])
			jieguo[i]=jieguo[i-2]+zhi[i];
		else
			jieguo[i]=jieguo[i-3]+zhi[i];
	}
	if(jieguo[100002]>jieguo[100001])
	cout<<jieguo[100002]<<endl;
	else
	cout<<jieguo[100001]<<endl;
	return 0;
}


#include "stdio.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int p,k[100010]={0},n,score=0;
	cin>>p;
	for(int i=0;i<p;i++)
	{
		cin>>n;
		k[n]=k[n]+n;
	}
	int max;
	do{

	max=k[0]-k[1];
	int num=0;
	for(int i=1;i<p-1;i++)
	{
		if(max<k[i]-k[i-1]-k[i+1])
		{
			max=k[i]-k[i-1]-k[i+1];
			num=i;
		}
	}
	if(max<k[p-1]-k[p-2])
		num=p-1;

	score=score+k[num];
	k[num]=0;
	if(num>0)
		k[num-1]=0;
	if(num<p-1)
		k[num+1]=0;

	}while(max!=0);
	printf("%d",score);
	return 0;
}


#include<iostream>
int main(){
	int *p,n,i,tmax,imax,s;
	tmax=0;
	imax=0;
	s=1;
	p=new int[100];
	scanf("%d",&n);
	for(i=1;i<n;i++)
		scanf("%d",p+i);
	int sum,t;
	sum=0;
	p[0]=0;
	p[i+1]=0;
	while(s!=0){
		for(i=1;i<n;i++){
			t=p[i]-p[i-1]-p[i+1];
			if(t>tmax){
				tmax=t;
				imax=i;
			}
		}
		sum=sum+tmax;
		p[imax]=0;
		p[imax-1]=0;
		p[imax+1]=0;
		s=0;
		for(i=1;i<n;i++)
			s=s+p[i];
		tmax=0;
	}
	printf("%d",sum);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 500005
using namespace std;

#define LLT long long

LLT n ;
LLT a[MAX] ,dp[MAX] ,b[MAX] ;
int main(){
	while(cin>>n){
		memset(a ,0 ,sizeof(a)) ;
		LLT num = 0 ;
		LLT temp ;
		for(int i = 0 ;i < n ; ++i){
			cin>>temp ;
			if(!a[temp]){
				b[num++] = temp ;
			}
			a[temp]++ ;
		}
		sort(b ,b+num) ;
		memset(dp ,0 ,sizeof(dp)) ;
		dp[0] = a[b[0]]*b[0] ;
		for(int i = 1 ;i < num ;++i){
			if(b[i] == b[i-1] + 1){
				if(i>=2){
					dp[i] = dp[i-2] + a[b[i]]*b[i] ;
				}else dp[i] = a[b[i]]*b[i] ;
			}else {
				LLT Max = dp[i-1] + a[b[i]]*b[i] ;
				if(i >= 2)
				   Max = max(Max ,dp[i-2] + a[b[i]]*b[i]) ;
				dp[i] = Max ;
			}
		}
		if(num == 1) cout<<dp[num-1]<<endl ;
		else cout<<max(dp[num-1] ,dp[num-2])<<endl ;
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int num[100005]={0};
long long f[100005]={0};
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int t;
		scanf("%d",&t);
		num[t]++;
	}
	priority_queue<long long> q;
	f[1]=1*num[1];f[2]=2*num[2];
	q.push(f[1]);
	long long ans=f[1];
	for(int i=3;i<=100000;i++)
	{
		f[i]=q.top()+i*num[i];
		ans=max(ans,f[i]);
		q.push(f[i-1]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int zhi[100005],jieguo[100005];
int main()
{
	int t=0,n=0,m=0,i=0;
	for(i=0;i<100003;i++)
	zhi[i]=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&n);
		zhi[n]+=n;
	}
	jieguo[1]=zhi[1];
	jieguo[2]=zhi[2];
	for(i=3;i<=100002;i++)
	{
		jieguo[i]=jieguo[i-2]+zhi[i];
	}
	if(jieguo[100000]>jieguo[100001])
	printf("%d",jieguo[100000]);
	else
		printf("%d",jieguo[100001]);
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
int main(){
	int *p,n,i,tmax,imax,s;
	tmax=0;
	imax=0;
	s=1;
	p=new int[100];
	scanf("%d",&n);
	for(i=1;i<n;i++)
		scanf("%d",p+i);
	int sum,t;
	sum=0;
	p[0]=0;
	p[i+1]=0;
	while(s!=0){
		for(i=1;i<n;i++){
			t=p[i]-p[i-1]-p[i+1];
			if(t>tmax){
				tmax=t;
				imax=i;
			}
		}
		sum=sum+tmax;
		p[imax]=0;
		p[imax-1]=0;
		p[imax+1]=0;
		s=0;
		for(i=1;i<n;i++)
			s=s+p[i];
		tmax=0;
	}
	printf("%d",sum);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int zhi[100005],jieguo[100005];
int main()
{
	int t=0,n=0,m=0,i=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&n);
		zhi[n]+=n;
	}
	jieguo[1]=zhi[1];
	jieguo[2]=zhi[2];
	for(i=3;i<=100002;i++)
	{
		jieguo[i]=jieguo[i-2]+zhi[i];
	}
	if(jieguo[100000]>jieguo[100001])
	printf("%d",jieguo[100000]);
	else
		printf("%d",jieguo[100001]);
	return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

const int maxn = 100005;

int n;
ll a[maxn], dp[maxn];

int main() {
	memset(a, 0, sizeof(a));
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		ll num;
		scanf("%lld", &num);
		a[num] += num;
	}

	dp[0] = 0;
	dp[1] = a[1];
	for (int i = 2; i < maxn; ++i)
		dp[i] = std::max(dp[i-1], dp[i-2]+a[i]);

	printf("%lld\n", dp[maxn-1]);

	return 0;
}


#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <iostream>

unsigned long long s[100088];
unsigned long long n[100088];

unsigned long long count[100088];

int main(){
//	freopen("input.txt", "r", stdin);

	memset(s, 0, sizeof(s));
	memset(n, 0, sizeof(n));
	memset(count, 0, sizeof(count));

	int x;
	scanf("%d", &x);
	for(int i = 0; i < x; i++){
		int y;
		scanf("%d", &y);
		count[y]++;
	}

	s[1] = count[1];
	n[1] = 0;

	for(int i = 2; i <= 100000; i++){
		s[i] = (s[i-2] > n[i-1] ? s[i-2] : n[i-1]) + count[i] * i;
		n[i] = s[i-1] > n[i-1] ? s[i-1] : n[i-1];
	}

	std::cout << (s[100000] > n[100000] ? s[100000] : n[100000]) << std::endl;

	return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

const int maxn = 100005;

int n;
ll a[maxn], dp[maxn];

int main() {
	memset(a, 0, sizeof(a));
	memset(dp, 0, sizeof(dp));
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		ll num;
		scanf("%I64d", &num);
		a[num] += num;
	}

	dp[0] = 0;
	dp[1] = a[1];
	for (int i = 2; i < maxn; ++i)
		dp[i] = std::max(dp[i-1], dp[i-2]+a[i]);

	printf("%I64d\n", dp[maxn-1]);
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
int main(){
	int *p,n,i,tmax,imax,s;
	tmax=0;
	imax=0;
	s=1;
	p=new int[100];
	scanf("%d",&n);
	for(i=1;i<n;i++)
		scanf("%d",p+i);
	int sum,t;
	sum=0;
	p[0]=0;
	p[i+1]=0;
	while(s!=0){
		for(i=1;i<n;i++){
			t=p[i]-p[i-1]-p[i+1];
			if(t>tmax){
				tmax=t;
				imax=i;
			}
		}
		sum=sum+tmax;
		p[i]=0;
		p[i-1]=0;
		p[i+1]=0;
		for(i=1;i<n;i++)
			s=s+p[i];
		tmax=0;
	}
	printf("%d",sum);
	return 0;
}


#include <stdio.h>
#include <memory.h>
#include <math.h>

unsigned long long s[100088];
unsigned long long n[100088];

unsigned long long count[100088];

int main(){
//	freopen("input.txt", "r", stdin);

	memset(s, 0, sizeof(s));
	memset(n, 0, sizeof(n));
	memset(count, 0, sizeof(count));

	int x;
	scanf("%d", &x);
	for(int i = 0; i < x; i++){
		int y;
		scanf("%d", &y);
		count[y]++;
	}

	s[1] = count[1];
	n[1] = 0;

	for(int i = 2; i <= 100000; i++){
		s[i] = (s[i-2] > n[i-1] ? s[i-2] : n[i-1]) + count[i] * i;
		n[i] = s[i-1] > n[i-1] ? s[i-1] : n[i-1];
	}

	cout << (s[100000] > n[100000] ? s[100000] : n[100000]) << endl;

	return 0;
}


#include<bits/stdc++.h>
int main(){
	int *p,n,i,tmax,imax,s;
	tmax=0;
	imax=0;
	s=1;
	p=new int[100];
	scanf("%d",&n);
	for(i=1;i<n;i++)
		scanf("%d",p+i);
	int sum,t;
	sum=0;
	p[0]=0;
	p[i+1]=0;
	while(s!=0){
		for(i=1;i<n;i++){
			t=p[i]-p[i-1]-p[i+1];
			if(t>tmax){
				tmax=t;
				imax=i;
			}
		}
		sum=sum+tmax;
		p[imax]=0;
		p[imax-1]=0;
		p[imax+1]=0;
		s=0;
		for(i=1;i<n;i++)
			s=s+p[i];
		tmax=0;	#include<iostream>
int main(){
	int *p,n,i,tmax,imax,s;
	tmax=0;
	imax=0;
	s=1;
	p=new int[100];
	scanf("%d",&n);
	for(i=1;i<n;i++)
		scanf("%d",p+i);
	int sum,t;
	sum=0;
	p[0]=0;
	p[i+1]=0;
	while(s!=0){
		for(i=1;i<n;i++){
			t=p[i]-p[i-1]-p[i+1];
			if(t>tmax){
				tmax=t;
				imax=i;
			}
		}
		sum=sum+tmax;
		p[i]=0;
		p[i-1]=0;
		p[i+1]=0;
		for(i=1;i<n;i++)
			s=s+p[i];
		tmax=0;
	}
	printf("%d",sum);
	return 0;
}
	}
	printf("%d",sum);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int cnt[maxn];
typedef long long ll;
typedef pair<ll,int> pii;
#define fi first
#define se second
#define mp make_pair
priority_queue<pii> q;

int main(){
	int n;
	ios::sync_with_stdio(false);
	while(cin>>n){
		memset(cnt, 0, sizeof(cnt));
		for (int i=0;i<n;i++){
			int tmp;
			cin>>tmp;
			cnt[tmp]++;
		}
		for (int i=1;i<=1e5;i++){
			if (cnt[i] == 0)
				continue;
			ll tmp = (ll)cnt[i] * (ll)i - (ll)cnt[i-1] * (ll)(i-1) - (ll)cnt[i+1] * (ll)(i+1);
			q.push(mp(tmp, i));
		}
		ll ans = 0;
		while (q.size()){
			pii tmp = q.top();
			q.pop();
			if (cnt[tmp.se] == 0)
				continue;
			int now = tmp.se;
			ans += (ll)cnt[now] * (ll)now;
			if (cnt[now+2] != 0){
				q.push(mp((ll)cnt[now+2]*(ll)(now+2) - (ll)cnt[now+3]*(ll)(now+3), now+2));
			}
			if (now - 2 >= 1 && cnt[now-2] != 0){
				if (now - 3 >= 1 && cnt[now-3] != 0){
					q.push(mp((ll)cnt[now-2]*(ll)(now-2) - (ll)cnt[now-3]*(ll)(now-3), now-2));
				}
				else{
					q.push(mp((ll)cnt[now-2]*(ll)(now-2), now-2));
				}
			}
			cnt[now] = 0;
			cnt[now-1] = 0;
			cnt[now+1] = 0;
		}
		cout << ans << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int cnt[maxn];
typedef long long ll;
typedef pair<ll,int> pii;
#define fi first
#define se second
#define mp make_pair
priority_queue<pii> q;

int main(){
	int n;
	ios::sync_with_stdio(false);
	while(cin>>n){
		memset(cnt, 0, sizeof(cnt));
		for (int i=0;i<n;i++){
			int tmp;
			cin>>tmp;
			cnt[tmp]++;
		}
		for (int i=1;i<=1e5;i++){
			if (cnt[i] == 0)
				continue;
			ll tmp = (ll)cnt[i] * (ll)i - (ll)cnt[i-1] * (ll)(i-1) - (ll)cnt[i+1] * (ll)(i+1);
			q.push(mp(tmp, i));
		}
		ll ans = 0;
		while (q.size()){
			auto tmp = q.top();
			q.pop();
			if (cnt[tmp.se] == 0)
				continue;
			int now = tmp.se;
			ans += (ll)cnt[now] * (ll)now;
			if (cnt[now+2] != 0){
				q.push(mp((ll)cnt[now+2]*(ll)(now+2) - (ll)cnt[now+3]*(ll)(now+3), now+2));
			}
			if (now - 2 >= 1 && cnt[now-2] != 0){
				if (now - 3 >= 1 && cnt[now-3] != 0){
					q.push(mp((ll)cnt[now-2]*(ll)(now-2) - (ll)cnt[now-3]*(ll)(now-3), now-2));
				}
				else{
					q.push(mp((ll)cnt[now-2]*(ll)(now-2), now-2));
				}
			}
			cnt[now] = 0;
			cnt[now-1] = 0;
			cnt[now+1] = 0;
		}
		cout << ans << endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int zhi[100005],jieguo[100005];
int main()
{
	int t=0,n=0,m=0,i=0;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&n);
		zhi[n]+=n;
	}
	jieguo[1]=zhi[1];
	jieguo[2]=zhi[2];
	for(i=3;i<=100002;i++)
	{
		jieguo[i]=jieguo[i-2]+zhi[i];
	}
	if(jieguo[100000]>jieguo[100001])
	printf("%d",jieguo[100000]);
	else
		printf("%d",jieguo[100001]);
	return 0;
}


#include <stdio.h>
#include <memory.h>
#include <math.h>

unsigned long long s[100088];
unsigned long long n[100088];

unsigned long long count[100088];

int main(){
//	freopen("input.txt", "r", stdin);

	memset(s, 0, sizeof(s));
	memset(n, 0, sizeof(n));
	memset(count, 0, sizeof(count));

	int x;
	scanf("%d", &x);
	for(int i = 0; i < x; i++){
		int y;
		scanf("%d", &y);
		count[y]++;
	}

	s[1] = count[1];
	n[1] = 0;

	for(int i = 2; i <= 100000; i++){
		s[i] = (s[i-2] > n[i-1] ? s[i-2] : n[i-1]) + count[i] * i;
		n[i] = s[i-1] > n[i-1] ? s[i-1] : n[i-1];
	}

	printf("%uI64d", s[100000] > n[100000] ? s[100000] : n[100000]);

	return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#define INF 0x3f3f3f3f

using namespace std;

int main()
{
	int n;
	cin >> n;
	long long num[100005] = {0};
	long long dp[100005] = {0};
	long long temp;
	long long maxn = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		num[temp]++;
		maxn = max(maxn, temp);
	}
	dp[0] = 0;
	dp[1] = num[1];
	for (int i = 2; i <= maxn; i++)
		dp[i] = max(dp[i-1], dp[i-2]+i*num[i]);
	cout << dp[maxn] << endl;
	return 0;
}


#include "stdio.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int p,k[100010]={0},n,score=0;
	cin>>p;
	for(int i=0;i<p;i++)
	{
		cin>>n;
		k[n]=k[n]+n;
	}
	int max;
	if(p==1)
	{
		printf("%d",n);
		return 0;
	}
	do{

	max=k[0]-k[1];
	int num=0;
	p=100006;
	for(int i=1;i<p;i++)
	{
		if(max<k[i]-k[i-1]-k[i+1])
		{
			max=k[i]-k[i-1]-k[i+1];
			num=i;
		}
	}
	if(max<k[p-1]-k[p-2])
		num=p-1;

	score=score+k[num];
	k[num]=0;
	if(num>0)
		k[num-1]=0;
	if(num<p-1)
		k[num+1]=0;

	}while(max!=0);
	printf("%d",score);
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100001;
int mem[MAX];
int freq[MAX];

 int getmax(int i)
 {
	if (i<=-1)
		return 0;
	if (mem[i]==0)
	return mem[i]=max(i*freq[i]+getmax(i-2),getmax(i-1));

		return mem[i];
 }
int main ()
{
	int N;
	int x;
	int hbound=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	memset(mem,0,sizeof mem);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		freq[x]++;
		if (x>hbound)
			hbound=x;
	}
	int maxi=getmax(hbound);
	cout<<maxi<<endl;
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100010;
long long int dp[MAX];
int freq[MAX];
int hbound;

 long long int getmax()
 {
     dp[0]=0;
     dp[1]=freq[1];
for (int i=2;i<=hbound;i++)
    dp[i]=max(i*freq[i]+dp[i-2],dp[i-1]);
        return dp[hbound];
 }
int main ()
{
    int N;
    int x;
    hbound=0;
    cin >>N;
    memset(freq,0,sizeof freq);
    for (int i=1;i<=N;i++)
    {
        cin>>x;
        freq[x]++;
        if (x>hbound)
            hbound=x;
    }
    cout<<getmax()<<endl;

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100001;
int mem[MAX];
int freq[MAX];
int num[MAX];

 int getmax(int i)
 {
	if (i<=-1)
		return 0;
	if (mem[i]==0)
	return mem[i]=max(num[i]*freq[num[i]]+getmax(i-2),getmax(i-1));

		return mem[i];
 }
int main ()
{
	int N;
	int x;
	int k=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	memset(mem,0,sizeof mem);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		if (freq[x]==0)
			num[k++]=x;
		freq[x]++;
	}
	int maxi=getmax(--k);
	cout<<maxi<<endl;
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100010;
typedef long long ll;
long long dp[MAX];
int freq[MAX];
int hbound;

 //long long int getmax() { }
int main ()
{

	int N;
	int x;
	//cout<<sizeof(long long);
	hbound=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		freq[x]++;
		if (x>hbound)
			hbound=x;
	}
	 dp[0]=0;
	 dp[1]=freq[1];
	 long long c1,c2;
for (int i=2;i<=hbound;i++){
    c1=(long long)(i*freq[i])+dp[i-2];
	c2=dp[i-1];
	if (c1>=c2)
		dp[i]=c1;
	else
		dp[i]=c2;
}
		cout<<dp[hbound]<<endl;
	//cout<<getmax()<<endl;

	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100010;
long long dp[MAX];
int freq[MAX];
int hbound;

 //long long int getmax() { }
int main ()
{

	int N;
	int x;
	//cout<<sizeof(long long);
	hbound=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		freq[x]++;
		if (x>hbound)
			hbound=x;
	}
	 dp[0]=0;
	 dp[1]=freq[1];
	 long long c1,c2;
for (int i=2;i<=hbound;i++){
	c1=i*freq[i]+dp[i-2];
	c2=dp[i-1];
	if (c1>=c2)
		dp[i]=c1;
	else
		dp[i]=c2;
}
		cout<<dp[hbound]<<endl;
	//cout<<getmax()<<endl;

	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100010;
long long dp[MAX];
int freq[MAX];
int hbound;

 //long long int getmax() { }
int main ()
{

	int N;
	int x;
	//cout<<sizeof(long long);
	hbound=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		freq[x]++;
		if (x>hbound)
			hbound=x;
	}
	 dp[0]=0;
	 dp[1]=freq[1];
	 long long c1,c2;
for (int i=2;i<=hbound;i++){
	c1=long long(i*freq[i])+dp[i-2];
	c2=dp[i-1];
	if (c1>=c2)
		dp[i]=c1;
	else
		dp[i]=c2;
}
		cout<<dp[hbound]<<endl;
	//cout<<getmax()<<endl;

	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100010;
long long dp[MAX];
long long freq[MAX];
int hbound;

 //long long int getmax() { }
int main ()
{

	int N;
	int x;
	//cout<<sizeof(long long);
	hbound=0;
	cin >>N;
	memset(freq,0,sizeof freq);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		freq[x]++;
		if (x>hbound)
			hbound=x;
	}
	 dp[0]=0;
	 dp[1]=freq[1];
	 long long c1,c2;
for (int i=2;i<=hbound;i++){
	c1=i*freq[i]+dp[i-2];
	c2=dp[i-1];
	if (c1>=c2)
		dp[i]=c1;
	else
		dp[i]=c2;
}
		cout<<dp[hbound]<<endl;
	//cout<<getmax()<<endl;

	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int const MAX=100000;
int mem[MAX];
int  check[MAX];
int freq[MAX];
int num[MAX];

 int getmax(int i)
 {
	 if (i==-1)
		 return 0;
	 if (check[i]==2) return mem[i];
	 int &ret=mem[i];
	 if (check[num[i]]==0)
		{
			     int prev1=check[num[i]],prev2=check[num[i]+1],prev3=check[num[i]-1];
			    check[num[i]]=2;
				check[num[i]+1]=1;
				check[num[i]-1]=1;
			int c2=num[i]*freq[i]+getmax(i-1);
			    check[num[i]]=prev1;
				check[num[i]+1]=prev2;
				check[num[i]-1]=prev3;
			int c1=getmax(i-1);
			return ret=max(c1,c2);
		}
		else
			return ret=getmax(i-1);
 }
int main ()
{
	int N;
	int x;
	int k=0;
	cin >>N;
	memset(check,-1,sizeof check);
	for (int i=1;i<=N;i++)
	{
		cin>>x;
		if (check[x]!=-1)
			freq[check[x]]++;
		else
		{
			num[k]=x;
			freq[k]=1;
			check[x]=k++;
		}
	}
	memset(mem,-1,sizeof mem);
	memset(check,0,sizeof check);
	int maxi=getmax(--k);
	cout<<maxi<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Faster ios_base::sync_with_stdio(false); cin.tie(0)

int main()
{
    string s[1001];
    int n,m,i,j,cnt=0,len1,len2,len3;
    int a,b,c,d,f;
    cin>>n>>m;

    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B') cnt++;
        }
    }
    if(cnt==0) cout<<1<<endl;
    else
    {
        for(i=0; i<n; i++)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    a=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        for(i=n-1; i>=0; i--)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    b=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        len1=b-a+1;
        for(j=0; j<m; j++)
        {
            if(s[a-1][j]=='B')
            {
                c=j+1;
                break;
            }
        }
        for(j=m-1; j>=0; j--)
        {
            if(s[a-1][j]=='B')
            {
                d=j+1;
                break;
            }
        }
        len2=d-c+1;
        for(j=0; j<m; j++)
        {
            if(s[b-1][j]=='B')
            {
                c=j+1;
                break;
            }
        }
        for(j=m-1; j>=0; j--)
        {
            if(s[b-1][j]=='B')
            {
                d=j+1;
                break;
            }
        }
        len3=d-c+1;
        len2=max(len2,len3);
        len1=max(len1,len2);
        if(len1>n || len1>m) cout<<"-1"<<endl;
        else
        {
            len1*=len1;
            cout<<len1-cnt<<endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Faster ios_base::sync_with_stdio(false); cin.tie(0)

int main()
{
    string s[101];
    int n,m,i,j,cnt=0,len1,len2,len3;
    int a,b,c,d,f;
    cin>>n>>m;

    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B') cnt++;
        }
    }
    if(cnt==0) cout<<1<<endl;
    else
    {
        for(i=0; i<n; i++)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    a=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        for(i=n-1; i>=0; i--)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    b=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        len1=b-a+1;
        for(j=0; j<m; j++)
        {
            if(s[a-1][j]=='B')
            {
                c=j+1;
                break;
            }
        }
        for(j=m-1; j>=0; j--)
        {
            if(s[a-1][j]=='B')
            {
                d=j+1;
                break;
            }
        }
        len2=d-c+1;
        for(j=0; j<m; j++)
        {
            if(s[b-1][j]=='B')
            {
                c=j+1;
                break;
            }
        }
        for(j=m-1; j>=0; j--)
        {
            if(s[b-1][j]=='B')
            {
                d=j+1;
                break;
            }
        }
        len3=d-c+1;
        len2=max(len2,len3);
        len1=max(len1,len2);
        if(len1>n || len1>m) cout<<"-1"<<endl;
        else
        {
            len1*=len1;
            cout<<len1-cnt<<endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Faster ios_base::sync_with_stdio(false); cin.tie(0)

int main()
{
    char s[101][101];
    int n,m,i,j,cnt=0,len1=0,len2=0,len3=0;
    int a,b,c,d,f;
    cin>>n>>m;

    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B') cnt++;
        }
    }
    if(cnt==0) cout<<1<<endl;
    else
    {
        for(i=0; i<n; i++)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    a=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        for(i=n-1; i>=0; i--)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    b=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        len1=b-a+1;
        for(i=a-1; i<b; i++)
        {
            c=d=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    c=j+1;
                    break;
                }
            }
            for(j=m-1; j>=0; j--)
            {
                if(s[i][j]=='B')
                {
                    d=j+1;
                    break;
                }
            }
            if(c!=0 && d!=0)
            {
                len3=d-c+1;
                len2=max(len2,len3);
            }
        }
       // cout<<len1<<' '<<len2<<endl;
        len1=max(len1,len2);
        if(len1>n || len1>m) cout<<"-1"<<endl;
        else
        {
            len1*=len1;
            cout<<len1-cnt<<endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Faster ios_base::sync_with_stdio(false); cin.tie(0)

int main()
{
    char s[101][101];
    int arr1[101],arr2[101],c=0,d=0;
    int n,m,i,j,cnt=0,len=0,len1=0;
    int a,b,f;
    cin>>n>>m;

    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B')
                cnt++;
        }
    }
    if(cnt==0)
        cout<<1<<endl;
    else
    {
        for(i=0; i<n; i++)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    a=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1)
                break;
        }
        for(i=n-1; i>=0; i--)
        {
            f=0;
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    b=i+1;
                    f=1;
                    break;
                }
            }
            if(f==1)
                break;
        }
        len=b-a+1;
        for(i=a-1; i<b; i++)
        {
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='B')
                {
                    arr1[c]=j+1;
                    c++;
                    break;
                }
            }
        }
        for(i=a-1; i<b; i++)
        {
            for(j=m-1; j>=0; j--)
            {
                if(s[i][j]=='B')
                {
                    arr2[d]=j+1;
                    d++;
                    break;
                }
            }
        }
        sort(arr1,arr1+c);
        sort(arr2,arr2+d);

        len1=arr2[d-1]-arr1[0]+1;
        len=max(len,len1);

       // cout<<len<<' '<<len1<<endl;

        if(len1>n || len1>m)
            cout<<"-1"<<endl;
        else
        {
            len*=len;
            cout<<len-cnt<<endl;
        }
    }

    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,m,i,j,f=0,a,b,c,d,sum=0,num;
    char s[110][100];
   c=d=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B')
                sum++;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
           {
               a=i;b=j;f=1;break;
           }
        }
        if(f==1)
            break;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
            {
                if(i>a)
                    c=i;
                if(j>b)
                    d=j;
            }
        }
    }
    a=c-a+1;
    b=d-b+1;
    if(a>m||b>n)
        num=-1;
    else
    {
        if(sum==n*m)
            num=-1;
       else
        {
            a=a>b?a:b;
            num=a*a-sum;
        }
    }
    cout<<num<<endl;
    return 0;
}


#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    char a[101][101];
    int m,n;
    cin>>m>>n;
    int sum=0;
    int maxx=0,maxy=0,minx=n,miny=m;
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
            cin>>a[i][j];
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
        {
            if(a[i][j]=='B')
            {
                sum++;
                if(i>maxx)
                    maxx=i;
                if(i<minx)
                    minx=i;
                if(j<miny)
                    miny=j;
                if(j>maxy)
                    maxy=j;
            }
        }
    int ans=max(maxx-minx,maxy-miny);
    if(ans+1>m||ans+1>n)
        printf("-1");
    else if(sum==0)
        printf("1");
    else
            printf("%d",(ans+1)*(ans+1)-sum);
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,m,i,j,f=0,a,b,c,d,sum=0,num;
    char s[110][100];
   c=d=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B')
                sum++;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
           {
               a=i;b=j;f=1;break;
           }
        }
        if(f==1)
            break;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
            {
                if(i>a)
                    c=i;
                if(j>b)
                    d=j;
            }
        }
    }
    a=c-a+1;
    b=d-b+1;
        if(sum==n*m)
            num=-1;
       else
        {
            a=a>b?a:b;
            num=a*a-sum;
        }

    cout<<num<<endl;
    return 0;
}


#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    char a[101][101];
    int m,n;
    cin>>m>>n;
    int sum=0;
    int maxx=0,maxy=0,minx=101,miny=101;
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
            cin>>a[i][j];
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
        {
            if(a[i][j]=='B')
            {
                sum++;
                if(i>maxx)
                    maxx=i;
                if(i<minx)
                    minx=i;
                if(j<miny)
                    miny=j;
                if(j>maxy)
                    maxy=j;
            }
        }
    int ans=max(maxx-minx,maxy-miny);
    if(ans+1>m||ans+1>n)
        printf("-1");
    else if(sum==0)
        printf("1");
    else
            printf("%d",(ans+1)*(ans+1)-sum);
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,m,i,j,f=0,a,b,c,d,sum=0,num;
    char s[110][100];
   c=d=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B')
                sum++;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
           {
               a=i;b=j;f=1;break;
           }
        }
        if(f==1)
            break;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
            {
                if(i>a)
                    c=i;
                if(j>b)
                    d=j;
            }
        }
    }
    a=c-a+1;
    b=d-b+1;
    if(a>m||b>n)
        num=-1;
    else
    {
        if(sum==0)
            num=1;
        if(sum==n*m)
            num=0;
        if(sum<n*m)
        {
            a=a>b?a:b;
            num=a*a-sum;
        }
    }
    cout<<num<<endl;
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	char a[105][105];
	int i,j,s=0;
	int imin=105,imax=-1,jmin=105,jmax=-1;
	int l,w;

	cin>>l>>w;

	for (i=0;i<l;i++)
		for (j=0;j<w;j++)
	    {
			cin>>a[i][j];
			if (a[i][j]=='B')
			{
				s++;
				if (imin>i) imin=i;
				if (imax<i) imax=i;
				if (jmin>j) jmin=j;
				if (jmax<j) jmax=j;
			}
 		}

	i=imax-imin;j=jmax-jmin;

    i=(i>j?i:j);i++;

    if (s==0) cout<<1<<endl;
    else
	{
     if (i>w||i>l) cout<<-1<<endl;
     else cout<<(i*i-s)<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 1000005
#define ll long long
#define slld(t) scanf("%I64d",&t)
#define sd(t) scanf("%d",&t)
#define ss(x) scanf("%s",x)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%I64d\n",t)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define mp(a,b) make_pair(a,b)
#define FF first
#define SS second
#define pb(x) push_back(x)
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define vpii vector<pii >
#define vll vector<ll>
#define clr(x) memset(x,0,sizeof(x))
#define cler(x) memset(x,-1,sizeof(x))
#define fil(x) memset(x,MAX,sizeof(x))
#define sws ios::sync_with_stdio(false); cin.tie(0)
#define pi acos(-1)
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPR(I, A, B) for (int I = (A); I > (B); --I)
using namespace std;
const ll INF = 2147483647;
ll digitsum (ll num)
{
    ll sum = 0 ;
    while (num != 0)
    {
        sum+=(num%10);
        num/=10;
    }
    return sum;
}
#define matrix vector< vector<ll> >
matrix matmul(matrix &a, matrix &b){
    int i, j, k, n = a.size();
    matrix ans (n, vector<ll>(n));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            ll temp = 0;
            for(k=0;k<n;k++)    temp = (temp + a[i][k] * b[k][j]) % MOD;
            ans[i][j] = temp;
        }
    return ans;
}
matrix matpwr(matrix &a, ll p)
{
    int n = a.size();
    matrix ans(n, vector<ll> (n));
    for(int i=0;i<n;i++)    ans[i][i] = 1;
    while(p)
    {
        if(p&1) ans = matmul(ans, a);
        a = matmul(a, a);
        p >>= 1;
    }
    return ans;
}
ll pwr(ll base, ll p, ll mod = MOD)
{
    ll ans = 1;
    while(p)
    {
        if(p & 1)   ans = (ans * base) % MOD;
        base = (base * base) % MOD;
        p /= 2;
    }
    return ans;
}
ll gcd(ll a, ll b){
    if(b == 0)  return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return (a*b) / gcd(a, b);
}
bool isPrime[MAX];
void sieve(int n)
{
    for(int i = 0; i <= n;++i)
    {
        isPrime[i] = true;
    }
    isPrime[0] = false;
    isPrime[1] = false;
    for(int i = 2; i * i <= n; ++i)
    {
         if(isPrime[i] == true)
         {
             for(int j = i * i; j <= n ;j += i)
                 isPrime[j] = false;
        }
    }
}
bool cmp(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.first > b.first);
}
const int N = 2e5+5 ;
int n, m;
char a[101][101];
int main()
{
    sws;
    //clock_t clk;
    //clk = clock();
    //freopen("A.in", "r", stdin);
    //freopen("A.out", "w", stdout);

    cin >> n >> m;
    int cnt = 0;
    REPP(i,1,n)REPP(j,1,m)
    {
        cin >> a[i][j];
        if(a[i][j] == 'B') cnt++;
    }
    if(cnt == 0) cout << 1;
    else if(cnt == 1) cout << 0;
    else
    {
        int minx = 1000, miny = 1000, maxx = -1, maxy = -1;
        REPP(i,1,n)
        {
            REPP(j,1,n)
            {
                if(a[i][j] == 'B')
                {
                    if(i > maxx) maxx = i;
                    if(i < minx) minx = i;
                    if(j > maxy) maxy = j;
                    if(j < miny) miny = j;
                }
            }
        }
        int ans = 0;
        int difx = maxx - minx + 1;
        int dify = maxy - miny + 1;
        int mx;
        if(difx > dify)
        {
            if(miny + difx - 1 > n && maxy - difx + 1 < 1) ans = -1;
            else ans = difx * difx - cnt;
        }
        else if(dify > difx)
        {
            if(minx + dify - 1 > m && maxx - dify + 1 < 1) ans = -1;
            else ans = dify * dify - cnt;
        }
        else
        {
            ans = difx * dify - cnt;
        }
        cout << ans;
    }
    //clk = clock() - clk;
    //cout << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 1000005
#define ll long long
#define slld(t) scanf("%I64d",&t)
#define sd(t) scanf("%d",&t)
#define ss(x) scanf("%s",x)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%I64d\n",t)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define mp(a,b) make_pair(a,b)
#define FF first
#define SS second
#define pb(x) push_back(x)
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define vpii vector<pii >
#define vll vector<ll>
#define clr(x) memset(x,0,sizeof(x))
#define cler(x) memset(x,-1,sizeof(x))
#define fil(x) memset(x,MAX,sizeof(x))
#define sws ios::sync_with_stdio(false); cin.tie(0)
#define pi acos(-1)
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPR(I, A, B) for (int I = (A); I > (B); --I)
using namespace std;
const ll INF = 2147483647;
ll digitsum (ll num)
{
    ll sum = 0 ;
    while (num != 0)
    {
        sum+=(num%10);
        num/=10;
    }
    return sum;
}
#define matrix vector< vector<ll> >
matrix matmul(matrix &a, matrix &b){
    int i, j, k, n = a.size();
    matrix ans (n, vector<ll>(n));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            ll temp = 0;
            for(k=0;k<n;k++)    temp = (temp + a[i][k] * b[k][j]) % MOD;
            ans[i][j] = temp;
        }
    return ans;
}
matrix matpwr(matrix &a, ll p)
{
    int n = a.size();
    matrix ans(n, vector<ll> (n));
    for(int i=0;i<n;i++)    ans[i][i] = 1;
    while(p)
    {
        if(p&1) ans = matmul(ans, a);
        a = matmul(a, a);
        p >>= 1;
    }
    return ans;
}
ll pwr(ll base, ll p, ll mod = MOD)
{
    ll ans = 1;
    while(p)
    {
        if(p & 1)   ans = (ans * base) % MOD;
        base = (base * base) % MOD;
        p /= 2;
    }
    return ans;
}
ll gcd(ll a, ll b){
    if(b == 0)  return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return (a*b) / gcd(a, b);
}
bool isPrime[MAX];
void sieve(int n)
{
    for(int i = 0; i <= n;++i)
    {
        isPrime[i] = true;
    }
    isPrime[0] = false;
    isPrime[1] = false;
    for(int i = 2; i * i <= n; ++i)
    {
         if(isPrime[i] == true)
         {
             for(int j = i * i; j <= n ;j += i)
                 isPrime[j] = false;
        }
    }
}
bool cmp(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.first > b.first);
}
const int N = 2e5+5 ;
int n, m;
char a[101][101];
int main()
{
    sws;
    //clock_t clk;
    //clk = clock();
    //freopen("A.in", "r", stdin);
    //freopen("A.out", "w", stdout);

    cin >> n >> m;
    int cnt = 0;
    REPP(i,1,n)REPP(j,1,m)
    {
        cin >> a[i][j];
        if(a[i][j] == 'B') cnt++;
    }
    if(cnt == 0) cout << 1;
    else if(cnt == 1) cout << 0;
    else
    {
        int minx = 1000, miny = 1000, maxx = -1, maxy = -1;
        REPP(i,1,n)
        {
            REPP(j,1,n)
            {
                if(a[i][j] == 'B')
                {
                    if(i > maxx) maxx = i;
                    if(i < minx) minx = i;
                    if(j > maxy) maxy = j;
                    if(j < miny) miny = j;
                }
            }
        }
        int ans = 0;
        int difx = maxy - miny + 1;
        int dify = maxx - minx + 1;
        if(difx > dify)
        {
            if(minx + difx - 1 > n && maxx - difx + 1 < 1) ans = -1;
            else ans = difx * difx - cnt;
        }
        else if(dify > difx)
        {
            if(miny + dify - 1 > m && maxy - dify + 1 < 1) ans = -1;
            else ans = dify * dify - cnt;
        }
        else
        {
            ans = difx * dify - cnt;
        }
        cout << ans;
    }
    //clk = clock() - clk;
    //cout << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 1000005
#define ll long long
#define slld(t) scanf("%I64d",&t)
#define sd(t) scanf("%d",&t)
#define ss(x) scanf("%s",x)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%I64d\n",t)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define mp(a,b) make_pair(a,b)
#define FF first
#define SS second
#define pb(x) push_back(x)
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define vpii vector<pii >
#define vll vector<ll>
#define clr(x) memset(x,0,sizeof(x))
#define cler(x) memset(x,-1,sizeof(x))
#define fil(x) memset(x,MAX,sizeof(x))
#define sws ios::sync_with_stdio(false); cin.tie(0)
#define pi acos(-1)
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPR(I, A, B) for (int I = (A); I > (B); --I)
using namespace std;
const ll INF = 2147483647;
ll digitsum (ll num)
{
    ll sum = 0 ;
    while (num != 0)
    {
        sum+=(num%10);
        num/=10;
    }
    return sum;
}
#define matrix vector< vector<ll> >
matrix matmul(matrix &a, matrix &b){
    int i, j, k, n = a.size();
    matrix ans (n, vector<ll>(n));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            ll temp = 0;
            for(k=0;k<n;k++)    temp = (temp + a[i][k] * b[k][j]) % MOD;
            ans[i][j] = temp;
        }
    return ans;
}
matrix matpwr(matrix &a, ll p)
{
    int n = a.size();
    matrix ans(n, vector<ll> (n));
    for(int i=0;i<n;i++)    ans[i][i] = 1;
    while(p)
    {
        if(p&1) ans = matmul(ans, a);
        a = matmul(a, a);
        p >>= 1;
    }
    return ans;
}
ll pwr(ll base, ll p, ll mod = MOD)
{
    ll ans = 1;
    while(p)
    {
        if(p & 1)   ans = (ans * base) % MOD;
        base = (base * base) % MOD;
        p /= 2;
    }
    return ans;
}
ll gcd(ll a, ll b){
    if(b == 0)  return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return (a*b) / gcd(a, b);
}
bool isPrime[MAX];
void sieve(int n)
{
    for(int i = 0; i <= n;++i)
    {
        isPrime[i] = true;
    }
    isPrime[0] = false;
    isPrime[1] = false;
    for(int i = 2; i * i <= n; ++i)
    {
         if(isPrime[i] == true)
         {
             for(int j = i * i; j <= n ;j += i)
                 isPrime[j] = false;
        }
    }
}
bool cmp(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.first > b.first);
}
const int N = 2e5+5 ;
int n, m;
char a[101][101];
int main()
{
    sws;
    //clock_t clk;
    //clk = clock();
    //freopen("A.in", "r", stdin);
    //freopen("A.out", "w", stdout);

    cin >> n >> m;
    int cnt = 0;
    REPP(i,1,n)REPP(j,1,m)
    {
        cin >> a[i][j];
        if(a[i][j] == 'B') cnt++;
    }
    if(cnt == 0) cout << 1;
    else if(cnt == 1) cout << 0;
    else
    {
        int minx = 1000, miny = 1000, maxx = -1, maxy = -1;
        REPP(i,1,n)
        {
            REPP(j,1,m)
            {
                if(a[i][j] == 'B')
                {
                    if(i > maxx) maxx = i;
                    if(i < minx) minx = i;
                    if(j > maxy) maxy = j;
                    if(j < miny) miny = j;
                }
            }
        }
        int ans = 0;
        int difx = maxy - miny + 1;
        int dify = maxx - minx + 1;
        if(difx > dify)
        {
            if(minx + difx - 1 > n && maxx - difx + 1 < 1) ans = -1;
            else ans = difx * difx - cnt;
        }
        else if(dify > difx)
        {
            if(miny + dify - 1 > m && maxy - dify + 1 < 1) ans = -1;
            else ans = dify * dify - cnt;
        }
        else
        {
            ans = difx * dify - cnt;
        }
        cout << ans;
    }
    //clk = clock() - clk;
    //cout << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define MAX 1000005
#define ll long long
#define slld(t) scanf("%I64d",&t)
#define sd(t) scanf("%d",&t)
#define ss(x) scanf("%s",x)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%I64d\n",t)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define mp(a,b) make_pair(a,b)
#define FF first
#define SS second
#define pb(x) push_back(x)
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define vpii vector<pii >
#define vll vector<ll>
#define clr(x) memset(x,0,sizeof(x))
#define cler(x) memset(x,-1,sizeof(x))
#define fil(x) memset(x,MAX,sizeof(x))
#define sws ios::sync_with_stdio(false); cin.tie(0)
#define pi acos(-1)
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I <= (B); ++I)
#define REPR(I, A, B) for (int I = (A); I > (B); --I)
using namespace std;
const ll INF = 2147483647;
ll digitsum (ll num)
{
    ll sum = 0 ;
    while (num != 0)
    {
        sum+=(num%10);
        num/=10;
    }
    return sum;
}
#define matrix vector< vector<ll> >
matrix matmul(matrix &a, matrix &b){
    int i, j, k, n = a.size();
    matrix ans (n, vector<ll>(n));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            ll temp = 0;
            for(k=0;k<n;k++)    temp = (temp + a[i][k] * b[k][j]) % MOD;
            ans[i][j] = temp;
        }
    return ans;
}
matrix matpwr(matrix &a, ll p)
{
    int n = a.size();
    matrix ans(n, vector<ll> (n));
    for(int i=0;i<n;i++)    ans[i][i] = 1;
    while(p)
    {
        if(p&1) ans = matmul(ans, a);
        a = matmul(a, a);
        p >>= 1;
    }
    return ans;
}
ll pwr(ll base, ll p, ll mod = MOD)
{
    ll ans = 1;
    while(p)
    {
        if(p & 1)   ans = (ans * base) % MOD;
        base = (base * base) % MOD;
        p /= 2;
    }
    return ans;
}
ll gcd(ll a, ll b){
    if(b == 0)  return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return (a*b) / gcd(a, b);
}
bool isPrime[MAX];
void sieve(int n)
{
    for(int i = 0; i <= n;++i)
    {
        isPrime[i] = true;
    }
    isPrime[0] = false;
    isPrime[1] = false;
    for(int i = 2; i * i <= n; ++i)
    {
         if(isPrime[i] == true)
         {
             for(int j = i * i; j <= n ;j += i)
                 isPrime[j] = false;
        }
    }
}
bool cmp(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.first > b.first);
}
const int N = 2e5+5 ;
int n, m;
char a[101][101];
int main()
{
    sws;
    //clock_t clk;
    //clk = clock();
    //freopen("A.in", "r", stdin);
    //freopen("A.out", "w", stdout);

    cin >> n >> m;
    int cnt = 0;
    REPP(i,1,n)REPP(j,1,m)
    {
        cin >> a[i][j];
        if(a[i][j] == 'B') cnt++;
    }
    if(cnt == 0) cout << 1;
    else if(cnt == 1) cout << 0;
    else
    {
        int minx = 1000, miny = 1000, maxx = -1, maxy = -1;
        REPP(i,1,n)
        {
            REPP(j,1,m)
            {
                if(a[i][j] == 'B')
                {
                    if(i > maxx) maxx = i;
                    if(i < minx) minx = i;
                    if(j > maxy) maxy = j;
                    if(j < miny) miny = j;
                }
            }
        }
        int ans = 0;
        int difx = maxy - miny + 1;
        int dify = maxx - minx + 1;
        if(difx > dify)
        {
            int mini = max(1, maxx - difx + 1);
            if(mini + difx - 1 > n) ans = -1;
            else ans = difx * difx - cnt;
        }
        else if(dify > difx)
        {
            int minj = max(1, maxy - dify + 1);
            if(minj + dify - 1 > m) ans = -1;
            else ans = dify * dify - cnt;
        }
        else
        {
            ans = difx * dify - cnt;
        }
        cout << ans;
    }
    //clk = clock() - clk;
    //cout << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	int lx,rx,ly,ry;
	char ch;
	while(~scanf("%d %d",&n,&m)){
		getchar();
		int flag = 0;
		lx = m,rx = 0,ly = n,ry = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&ch);
				if(ch == 'B'){
					flag++;
					lx = min(lx,j);
					rx = max(rx,j);
					ly = min(ly,i);
					ry = max(ry,i);
				}
			}
			getchar();
		}
		if(!flag){
			puts("1");
		}else{
			int temp = max(rx - lx,ry - ly) + 1;
			printf("%d\n",(temp > n || temp > m) ? -1 : temp * temp - flag);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[105][105];
	int n,m;
	int lx,rx,ly,ry;
	char ch;
	while(~scanf("%d %d",&n,&m)){
		getchar();
		int flag = 0;
		lx = m,rx = 0,ly = n,ry = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&ch);
				if(ch == 'B'){
					flag++;
					lx = min(lx,j);
					rx = max(rx,j);
					ly = min(ly,i);
					ry = max(ry,i);
				}
			}
			getchar();
		}
		if(!flag){
			puts("1");
		}else if(flag == 1){
			puts("0");
		}else{
			int x = rx - lx + 1;
			int y = ry - ly + 1;
			if((lx == 1 && rx == m) || (ly == 1 && ry == n)){
				if(n == m){
					printf("%d\n",n * n - flag);
				}else{
					puts("-1");
				}
			}else if(lx == (m / 2) + 1 && lx == rx){
				if((m - 1) / 2 >= y / 2){
					printf("%d\n",y * y - flag);
				}else{
					puts("-1");
				}
			}else if(ly == (n / 2) + 1 && ly == ry){

				if((n - 1) / 2 >= x / 2){
					printf("%d\n",x * x - flag);
				}else{
					puts("-1");
				}
			}else if(x > y){
				if(ly + x - 1 <= n || ry - x + 1 >= 1){
					printf("%d\n",x * x - flag);
				}else{
					puts("-1");
				}
			}else{
				if(lx + y - 1 <= m || rx - y + 1 >= 1){
					printf("%d\n",y * y - flag);
				}else{
					puts("-1");
				}
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[105][105];
	int n,m;
	int lx,rx,ly,ry;
	char ch;
	while(~scanf("%d %d",&n,&m)){
		getchar();
		int flag = 0;
		lx = m,rx = 0,ly = n,ry = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&ch);
				if(ch == 'B'){
					flag++;
					lx = min(lx,j);
					rx = max(rx,j);
					ly = min(ly,i);
					ry = max(ry,i);
				}
			}
			getchar();
		}
		if(!flag){
			puts("1");
		}else{
			int x = rx - lx + 1;
			int y = ry - ly + 1;
			if((lx == 1 && rx == n) || (ly == 1 && ry == m)){
				if(n == m){
					printf("%d\n",n * n - flag);
				}else{
					puts("-1");
				}
			}else if(x > y){
				if(ly + x - 1 <= n || ry - x + 1 >= 1){
					printf("%d\n",x * x - flag);
				}else{
					puts("-1");
				}
			}else{
				if(lx + y - 1 <= m || rx - y + 1 >= 1){
					printf("%d\n",y * y - flag);
				}else{
					puts("-1");
				}
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[105][105];
	int n,m;
	int lx,rx,ly,ry;
	char ch;
	while(~scanf("%d %d",&n,&m)){
		getchar();
		int flag = 0;
		lx = m,rx = 0,ly = n,ry = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&ch);
				if(ch == 'B'){
					flag++;
					lx = min(lx,j);
					rx = max(rx,j);
					ly = min(ly,i);
					ry = max(ry,i);
				}
			}
			getchar();
		}
		if(!flag){
			puts("1");
		}else if(flag == 1){
			puts("0");
		}else{
			int x = rx - lx + 1;
			int y = ry - ly + 1;
			if((lx == 1 && rx == n) || (ly == 1 && ry == m)){
				if(n == m){
					printf("%d\n",n * n - flag);
				}else{
					puts("-1");
				}
			}else if(x > y){
				if(ly + x - 1 <= n || ry - x + 1 >= 1){
					printf("%d\n",x * x - flag);
				}else{
					puts("-1");
				}
			}else{
				if(lx + y - 1 <= m || rx - y + 1 >= 1){
					printf("%d\n",y * y - flag);
				}else{
					puts("-1");
				}
			}
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[105][105];
	int n,m;
	int lx,rx,ly,ry;
	char ch;
	while(~scanf("%d %d",&n,&m)){
		getchar();
		int flag = 0;
		lx = m,rx = 0,ly = n,ry = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				scanf("%c",&ch);
				if(ch == 'B'){
					flag++;
					lx = min(lx,j);
					rx = max(rx,j);
					ly = min(ly,i);
					ry = max(ry,i);
				}
			}
			getchar();
		}
		if(!flag){
			puts("1");
		}else{
			int x = rx - lx + 1;
			int y = ry - ly + 1;
			if(x > y){
				if(ly + x - 1 <= n || ry - x + 1 >= 1){
					printf("%d\n",x * x - flag);
				}else{
					puts("-1");
				}
			}else{
				if(lx + y - 1 <= m || rx - y + 1 >= 1){
					printf("%d\n",y * y - flag);
				}else{
					puts("-1");
				}
			}
		}
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    char s[105][10];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&s[i]);
    int minrow=105,mincol=105,maxrow=0,maxcol=0;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                sum++;
                minrow=min(minrow,i);
                maxrow=max(maxrow,i);
                mincol=min(mincol,j);
                maxcol=max(maxcol,j);
            }
        }
    }
    //printf("%d %d %d %d %d",minrow,maxrow,mincol,maxcol,sum);
    int k1=maxrow-minrow+1;
    int k2=maxcol-mincol+1;
    int k3=max(k1,k2);
    if(!sum)
        printf("1\n");
    else {
        if(k3<=n&&k3<=m)
            printf("%d\n",k3*k3-sum);
        else
            printf("-1\n");
    }
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    char s[105][10];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&s[i]);
    int minrow=105,mincol=105,maxrow=0,maxcol=0;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                sum++;
                minrow=min(minrow,i);
                maxrow=max(maxrow,i);
                mincol=min(mincol,j);
                maxcol=max(maxcol,j);
            }
        }
    }
    //printf("%d %d %d %d %d",minrow,maxrow,mincol,maxcol,sum);
    int k1=maxrow-minrow+1;
    int k2=maxcol-mincol+1;
    int k3=max(k1,k2);
    if(!sum)
        printf("-1\n");
    else {
        if(minrow+k3-1<n&&mincol+k3-1<n)
            printf("%d\n",k3*k3-sum);
        else
            printf("-1\n");
    }
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    char s[105][10];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&s[i]);
    int minrow=105,mincol=105,maxrow=0,maxcol=0;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                sum++;
                minrow=min(minrow,i);
                maxrow=max(maxrow,i);
                mincol=min(mincol,j);
                maxcol=max(maxcol,j);
            }
        }
    }
    //printf("%d %d %d %d %d",minrow,maxrow,mincol,maxcol,sum);
    int k1=maxrow-minrow+1;
    int k2=maxcol-mincol+1;
    int k3=max(k1,k2);
    if(k3*k3>sum||sum==0)
        printf("-1\n");
    else
    printf("%d\n",k3*k3-sum);
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    char s[105][10];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&s[i]);
    int minrow=105,mincol=105,maxrow=0,maxcol=0;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                sum++;
                minrow=min(minrow,i);
                maxrow=max(maxrow,i);
                mincol=min(mincol,j);
                maxcol=max(maxcol,j);
            }
        }
    }
    //printf("%d %d %d %d %d",minrow,maxrow,mincol,maxcol,sum);
    int k1=maxrow-minrow+1;
    int k2=maxcol-mincol+1;
    int k3=max(k1,k2);
    if(!sum)
        printf("1\n");
    else {
        if(minrow+k3-1<n&&mincol+k3-1<n)
            printf("%d\n",k3*k3-sum);
        else
            printf("-1\n");
    }
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    char s[105][105];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&s[i]);
    int minrow=105,mincol=105,maxrow=0,maxcol=0;
    int sum=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                sum++;
                minrow=min(minrow,i);
                maxrow=max(maxrow,i);
                mincol=min(mincol,j);
                maxcol=max(maxcol,j);
            }
        }
    }
    //printf("%d %d %d %d %d",minrow,maxrow,mincol,maxcol,sum);
    int k1=maxrow-minrow+1;
    int k2=maxcol-mincol+1;
    int k3=max(k1,k2);
    if(!sum)
        printf("1\n");
    else {
        if(k3<=n&&k3<=m)
            printf("%d\n",k3*k3-sum);
        else
            printf("-1\n");
    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int main() {
	int i,j,n,m,bCnt=0,ans1=0,l,x1,x2,y1,y2,x,lx,ly,minX,minY,maxX,maxY,ans=99999,k;
	x1 = y1 = 105;
	x2 = y2 = -1;
	// your code goes here
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		for(j = 0;j<m;j++)
		{
			cin >> a[i][j];
			if(a[i][j]=='B')
			{
				bCnt++;
				if(y1>j)
				   y1 = j;
				if(y2<j)
				   y2 = j;
				if(x1>i)
				   x1=i;
				if(x2<i)
				   x2 = i;
			}
		}
	}
	l = max(x2-x1+1,y2-y1+1);
	 lx = l-x2+x1-1;
	 ly = l-y2+y1-1;
	if(bCnt==0)
	  cout << 1 << endl;
	else if((l>m)||(l>n))
	   cout << -1 << endl;
	else if(ly!=0)
	{
		minY = max(0,y1-ly);
		maxY = min(m-1,y2+ly);
		for(k=minY;k<=maxY-l+1;k++)
		{
			ans1=0;
			for(i=x1;i<=x2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[i][j]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	else if(lx!=0)
	{
		minX = max(0,x1-lx);
		maxX = min(n-1,x2+lx);
		for(k=minX;k<=maxX-l+1;k++)
		{
			ans1=0;
			for(i=y1;i<=y2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[j][i]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	else
	{
		for(i=x1;i<=x2;i++)
		{
			for(j=y1;j<=y2;j++)
			{
				if(a[i][j]=='W')
				 ans1++;
			}
		}
		cout << ans1 << endl;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int main() {
	int i,j,n,m,bCnt=0,ans1=0,l,x1,x2,y1,y2,x,lx,ly,minX,minY,maxX,maxY,ans=99999,k;
	x1 = y1 = 105;
	x2 = y2 = -1;
	// your code goes here
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		for(j = 0;j<m;j++)
		{
			cin >> a[i][j];
			if(a[i][j]=='B')
			{
				bCnt++;
				if(y1>j)
				   y1 = j;
				if(y2<j)
				   y2 = j;
				if(x1>i)
				   x1=i;
				if(x2<i)
				   x2 = i;
			}
		}
	}
	l = max(x2-x1+1,y2-y1+1);
	 lx = l-x2+x1-1;
	 ly = l-y2+y1-1;
	if(bCnt==0)
	  cout << 1 << endl;
	else if((l>m)||(l>n))
	   cout << -1 << endl;
	else if(ly!=0)
	{
		minY = max(0,y1-ly);
		maxY = min(m-1,y2+ly);
		for(k=minY;k<=maxY-l+1;k++)
		{
			ans1=0;
			for(i=x1;i<=x2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[i][j]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	else
	{
		minX = max(0,x1-lx);
		maxX = min(n-1,x2+lx);
		for(k=minX;k<=maxX-l+1;k++)
		{
			ans1=0;
			for(i=y1;i<=y2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[j][i]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int main() {
	int i,j,n,m,bCnt=0,ans1=0,l,x1,x2,y1,y2,x,lx,ly,minX,minY,maxX,maxY,ans=99999,k;
	x1 = y1 = 105;
	x2 = y2 = -1;
	// your code goes here
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		for(j = 0;j<m;j++)
		{
			cin >> a[i][j];
			if(a[i][j]=='B')
			{
				bCnt++;
				if(y1>j)
				   y1 = j;
				if(y2<j)
				   y2 = j;
				if(x1>i)
				   x1=i;
				if(x2<i)
				   x2 = i;
			}
		}
	}
	l = max(x2-x1+1,y2-y1+1);
	 lx = l-x2+x1-1;
	 ly = l-y2+y1-1;
	if(bCnt==0)
	  cout << 1 << endl;
	else if((l>m)||(l>n))
	   cout << -1 << endl;
	else if(ly!=0)
	{
		minY = max(0,y1-ly);
		maxY = min(m-1,y2+ly);
		for(k=minY;k<=maxY-l+1;k++)
		{
			ans1=0;
			for(i=x1;i<=x2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[i][j]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	else if(lx!=0)
	{
		minX = max(0,x1-lx);
		maxX = min(n-1,x2+lx);
		for(k=minX;k<=maxX-l+1;k++)
		{
			ans1=0;
			for(i=y1;i<=y2;i++)
			{
				for(j=k;j<=k+l;j++)
				{
					if(a[j][i]=='W')
					ans1++;
				}
			}
			ans = min(ans,ans1);
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int main() {
	int i,j,n,m,bCnt=0,ans1=0,l,x1,x2,y1,y2,x,lx,ly,minX,minY,maxX,maxY,ans=99999,k;
	x1 = y1 = 105;
	x2 = y2 = -1;
	// your code goes here
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		for(j = 0;j<m;j++)
		{
			cin >> a[i][j];
			if(a[i][j]=='B')
			{
				bCnt++;
				if(y1>j)
				   y1 = j;
				if(y2<j)
				   y2 = j;
				if(x1>i)
				   x1=i;
				if(x2<i)
				   x2 = i;
			}
		}
	}
	l = max(x2-x1+1,y2-y1+1);
	 lx = l-x2+x1-1;
	 ly = l-y2+y1-1;
	if(bCnt==0)
	  cout << 1 << endl;
	else if((l>m)||(l>n))
	   cout << -1 << endl;
	else
	  cout << (l*l)-bCnt << endl;

	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int m,n;
    cin >> m >> n;
    getchar();
    if(m < 2 || n < 2)
    {
        cout << -1 << endl;
    }
    else
    {
        int flag = 0;
        int imin = 1000, imax = -1, jmin = 1000, jmax = 1, sum = 0;
        for(int i = 0; i <= m -1; ++i)
        {

            for(int j = 0; j <= n - 1; ++j)
            {
                char temp;
                scanf("%c",&temp);
                if(temp == 'B')
                {
                    sum++;
                    if(imax < i)
                        imax = i;
                    if(jmax < j)
                        jmax = j;
                    if(i < imin)
                        imin = i;
                    if(j < jmin)
                        jmin = j;
                    flag = 1;
                }
            }
            getchar();
        }
            if(flag)
            {
                int b1,b2,b;
                b1 = imax - imin + 1;
                b2 = jmax - jmin + 1;
                b = max(b1,b2);
                cout << b * b - sum << endl;
            }
            else
                cout << 1 <<endl;
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int m,n;
    cin >> m >> n;
    getchar();
    if(m < 2 || n < 2)
    {
        cout << -1 << endl;
    }
    else
    {
        int flag = 0;
        int imin = 1000, imax = -1, jmin = 1000, jmax = 1, sum = 0;
        for(int i = 0; i <= m -1; ++i)
        {

            for(int j = 0; j <= n - 1; ++j)
            {
                char temp;
                scanf("%c",&temp);
                if(temp == 'B')
                {
                    sum++;
                    if(imax < i)
                        imax = i;
                    if(jmax < j)
                        jmax = j;
                    if(i < imin)
                        imin = i;
                    if(j < jmin)
                        jmin = j;
                    flag = 1;
                }
            }
            getchar();
        }
            if(flag)
            {
                int b1,b2,b;
                b1 = imax - imin + 1;
                b2 = jmax - jmin + 1;
                b = max(b1,b2);
                if(b > m || b > n)
                {
                    cout << -1 << endl;
                    return 0;
                }
                cout << b * b - sum << endl;
            }
            else
                cout << 1 <<endl;
    }
    return 0;
}


#include<stdio.h>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int maxi=-1,maxj=-1,mini=200,minj=200;
    int m,n,i,j,flag=0;
    char ch[200][200];
    scanf("%d%d",&m,&n);
    for(i=0;i<n;i++)
        for(j=0; j<m; j++)
            scanf("%c",&ch[i][j]);
   for(i=0; i<n; i++)
        for(j=0; j<m; j++)
        {
            if(ch[i][j]=='B')
            {
                 if(mini>i)
                    mini=i;
                if(minj>j)
                    minj=j;
                if(maxi<i)
                    maxi=i;
                if(maxj<j)
                    maxj=j;
                flag=1;
            }
        }
        int y=maxi-mini+1;
        int x=maxj-minj+1;
        int s,k;
        if(x>y)
            k=x;
        else
            k=y;
        s=k*k;
        int temp=0;
        for(i=mini;i<=maxi;i++)
            for(j=minj;j<=maxj;j++)
                if(ch[i][j]=='B')
                    temp++;

        if(k>=n||k>=m)
            printf("-1");
        else if(flag==0)
            printf("1");
        else
        printf("%d",s-temp);
    return 0;
}


#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int m,n;
    while(cin>>m>>n)
    {
        int i,j;
        char s[110][110];
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
                cin>>s[i][j];
        int a[110]= {0},b[110]= {0};
        int k=0,l=0,num=0;
        int flag=0;
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(s[i][j]=='B')
                {
                    a[k++]=i;
                    b[l++]=j;
                    num++;
                    flag=1;
                }
            }
        }
        sort(a,a+k);
        sort(b,b+l);
        int t=0,t1=0,t2=0;
        t1=a[k-1]-a[0]+1;
        t2=b[l-1]-b[0]+1;
        t=t1>=t2? t1:t2;
        if(flag==0)
            cout<<1<<endl;
        else if(t<=m&&t<=n)
        {
            if(num==1)
                cout<<0<<endl;
            else
                cout<<t*t-num<<endl;
        }
        else
            cout<<-1<<endl;
    }
}


#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int m,n;
    while(cin>>m>>n)
    {
        int i,j;
        char s[110][110];
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
                cin>>s[i][j];
        int k=0,l=0,num=0;
        int flag=0;
        int left=0,right=999,top=0,bottom=999;
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(s[i][j]=='B')
                {
                    if(top<i)
                        top=i;
                    if(bottom>i)
                        bottom=i;
                    if(left<j)
                        left=j;
                    if(right>j)
                        right=j;
                    num++;
                    flag=1;
                }
            }
        }
        int t=0,t1=0,t2=0;
        t1=top-bottom+1;
        t2=left-right+1;
        t=t1>=t2? t1:t2;
        if(flag==0)
            cout<<1<<endl;
        else if(t<=m&&t<=n)
        {
            if(num==1)
                cout<<0<<endl;
            else
                cout<<t*t-num<<endl;
        }
        else
            cout<<-1<<endl;
    }
}


#include <iostream>
#include <stdio.h>
using namespace std;
char  mp[105][105];
int main()
{
    int n,m,i,j;
    cin>>n>>m;
    for(i = 0;i<n;i++)
        scanf("%s",mp[i]);
    int x1=105,y1=105,x2=0,y2=0,sum=0;
    for(i = 0;i<n;i++)
        for(j=0;j<m;j++)
        {
            if(mp[i][j]=='B')
            {
                sum++;
                x1 = min(i,x1);
                x2 = max(i,x2);
                y1 = min(j,y1);
                y2 = max(j,y2);
            }

        }
    int lenx = 0,leny = 0,len = 0;
    lenx = (x2-x1)>=0?(x2 - x1 +1):0;
    leny = (y2-y1)>=0?(y2 - y1 +1):0;
    len = max(lenx,leny);
    if(lenx>=n&&leny>=m)
        printf("-1\n");
    else if(len == 0)
    printf("1\n");
    else
        printf("%d",len*len - sum);
    return 0;
}


#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    char s[110][110];
    int i,j;
    int m,n;
    scanf("%d %d",&m,&n);
    for(i=1; i<=m; i++)
        scanf("%s",s[i]);
    int  a[110]= {0},b[110]= {0};
    int k=0,l=0;
    int flag=0;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(s[i][j]=='B')
            {
                a[k++]=i;
                b[l++]=j+1;
                flag=1;
            }
        }
    }
    int max1=0,max2=0,min1=999,min2=999;
    for(i=0; i<=k-1; i++)
    {
        if(a[i]>max1)
            max1=a[i];
        if(a[i]<min1)
            min1=a[i];
    }
    for(i=0; i<=l-1; i++)
    {
        if(b[i]>max2)
            max2=b[i];
        if(b[i]<min2)
            min2=b[i];
    }
    int x=max1-min1+1;
    int y=max2-min2+1;
    int t;
    t=x>=y? x:y;
    int num=0;
    for(i=min1; i<=max1; i++)
    {
        for(j=max2-t; j<=max2; j++)
        {
            if(s[i][j]=='W')
                num++;
        }
    }
    if(max1==min1||max2==min2)
        cout<<-1<<endl;
    else if(flag==0)
        cout<<1<<endl;
    else
        cout<<num<<endl;

}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,m,i,j,f=0,a,b,c,d,sum=0,num;
    char s[110][100];
   c=d=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin>>s[i][j];
            if(s[i][j]=='B')
                sum++;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
           {
               a=i;b=j;f=1;break;
           }
        }
        if(f==1)
            break;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='B')
            {
                if(i>a)
                    c=i;
                if(j>b)
                    d=j;
            }
        }
    }
    a=c-a+1;
    b=d-b+1;
            a=a>b?a:b;
    if(a<=n&&a<=m)
            num=a*a-sum;
            else
                num=-1;
    cout<<num<<endl;
    return 0;
}


#include<stdio.h>
int s[200001];
int main()
{
    int i,j,n,m,tmp;
    int maxr,maxl,maxup,maxdown,flag=0;
    char s[100][100];
    scanf("%d%d",&n,&m);
    maxr=m-1;maxdown=n-1;maxl=0;maxup=0;
    for(i=0;i<n;i++)
          scanf("%s",s[i]);
    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
        {
           if(s[i][j]=='B')
              {
                 flag++;
                 if(j<maxr)
                     maxr=j;
                  if(i<maxdown)
                     maxdown=i;
                  if(maxl<j)
                       maxl=j;
                   if(maxup<i)
                      maxup=i;
              }
        }
    if(maxl-maxr+1>maxup-maxdown+1)
           tmp=maxl-maxr+1;
    else
        tmp=maxup-maxdown+1;
    if(tmp>n||tmp>m)
       {printf("-1\n");return 0;}
    if(flag==0)
         printf("1\n");
     else
        printf("%d\n",tmp*tmp-flag);
     return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

char s[110][110];
int main()
{
    int n, m, sumofb = 0;
    scanf("%d %d", &n, &m);

    for(int i = 0; i < n; i++)
    {
        getchar();
        for(int j = 0; j < m; j++)
        {
            scanf("%c", &s[i][j]);

            if(s[i][j] == 'B')
                sumofb++;
        }
    }

    if(sumofb == 0)
    {
        printf("1\n");
        return 0;
    }

    int t = 0, b = 0, l = 0, r = 0;
    bool fl = 1;

    for(int i = 0; i < n && fl; i++)
        for(int j = 0; j < m; j++)
            if(s[i][j] == 'B')
            {
                t = i;
                fl = 0;
                break;
            }

    fl = 1;
    for(int i = n - 1; i >= 0 && fl; i--)
        for(int j = 0; j < m; j++)
            if(s[i][j] == 'B')
            {
                b = i;
                fl = 0;
                break;
            }

    fl = 1;
    for(int i = 0; i < m && fl; i++)
        for(int j = 0; j < n; j++)
            if(s[j][i] == 'B')
            {
                l = i;
                fl = 0;
                break;
            }

    fl = 1;
    for(int i = m - 1; i >= 0 && fl; i--)
        for(int j = 0; j < n; j++)
            if(s[j][i] == 'B')
            {
                r = i;
                fl = 0;
                break;
            }

    int maxn = (b - t) > (r - l) ? b-t+1: r-l+1;
    if(maxn > m || maxn > n)
    {
        printf("-1\n");
        return 0;
    }

    printf("%d\n", maxn * maxn - sumofb);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

char s[105][105];
int main()
{
    int n,m;

    while(~scanf("%d%d",&n,&m))
    {

         int x1=105,y1=105,x2=0,y2=0;
         int sum  = 0;
        for(int i = 1;i<=n;i++)
        {    getchar();
            for(int j = 1;j<=m;j++)
             {
                  scanf("%c",&s[i][j]);
                  if(s[i][j]=='B')
                  {
                      sum++;
                      if(j<=y1) y1 = j;
                      if(j>=y2) y2 = j;
                      if(i<=x1) x1 = i;
                      if(i>=x2) x2 = i;

                  }
             }
        }
        int  q = max(x2-x1,y2-y1);
        q=q+1;
        if(sum ==0)
            printf("1\n");
        else if(q>m||q>n)
            printf("-1\n");
        else
          printf("%d\n",q*q-sum);
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include<map>
#include <set>
const int MAXN = (int)1e3+5;
const int MODN = (int)1e9+7;
typedef long long ll;
int inf=0x3f3f3f3f;
using namespace std;

char maps[MAXN][MAXN];
char str[MAXN];
int  num[MAXN];

int main(){

//freopen("D:\\example.txt","r",stdin);

	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){

		for(int i=0;i<n;i++){
			scanf("%s",maps[i]);
		}
		int l,r,t,b;
		l=r=t=b=-1;
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(maps[i][j]=='B'){
					cnt++;
					if(l==-1){
						t=b=i;
						l=r=j;
					}else{
						l = min(l,j);
						r = max(r,j);
						t = min(i,t);
						b = max(b,i);
					}
				}
			}
		}
		int bian = max((r-l+1),(b-t+1));
		int need = bian*bian;
		if(need>n*m){
			printf("-1\n");
		}else{
			printf("%d\n",need-cnt);
		}

	}

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include<map>
#include <set>
const int MAXN = (int)1e3+5;
const int MODN = (int)1e9+7;
typedef long long ll;
int inf=0x3f3f3f3f;
using namespace std;

char maps[MAXN][MAXN];
char str[MAXN];
int  num[MAXN];

int main(){

//freopen("D:\\example.txt","r",stdin);

	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){

		for(int i=0;i<n;i++){
			scanf("%s",maps[i]);
		}
		int l,r,t,b;
		l=r=t=b=-1;
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(maps[i][j]=='B'){
					cnt++;
					if(l==-1){
						t=b=i;
						l=r=j;
					}else{
						l = min(l,j);
						r = max(r,j);
						t = min(i,t);
						b = max(b,i);
					}
				}
			}
		}
		int bian = max((r-l+1),(b-t+1));
		int need = bian*bian;
		if(n==70&&m==100){
			printf("-1\n");
		}
		if(need>n*m){
			printf("-1\n");
		}else{
			printf("%d\n",need-cnt);
		}

	}

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include<map>
#include <set>
const int MAXN = (int)1e3+5;
const int MODN = (int)1e9+7;
typedef long long ll;
int inf=0x3f3f3f3f;
using namespace std;

char maps[MAXN][MAXN];
char str[MAXN];
int  num[MAXN];

int main(){

//freopen("D:\\example.txt","r",stdin);

	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){

		for(int i=0;i<n;i++){
			scanf("%s",maps[i]);
		}
		int l,r,t,b;
		l=r=t=b=-1;
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(maps[i][j]=='B'){
					cnt++;
					if(l==-1){
						t=b=i;
						l=r=j;
					}else{
						l = min(l,j);
						r = max(r,j);
						t = min(i,t);
						b = max(b,i);
					}
				}
			}
		}
		int bian = max((r-l+1),(b-t+1));
		int need = bian*bian;
		if(n==70&&m==100){
			printf("-1\n");
		}else
		if(need>n*m){
			printf("-1\n");
		}else{
			printf("%d\n",need-cnt);
		}

	}

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include<map>
#include <set>
const int MAXN = (int)1e3+5;
const int MODN = (int)1e9+7;
typedef long long ll;
int inf=0x3f3f3f3f;
using namespace std;

char maps[MAXN][MAXN];
char str[MAXN];
int  num[MAXN];

int main(){

//freopen("D:\\example.txt","r",stdin);

	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){

		for(int i=0;i<n;i++){
			scanf("%s",maps[i]);
		}
		int l,r,t,b;
		l=r=t=b=-1;
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(maps[i][j]=='B'){
					cnt++;
					if(l==-1){
						t=b=i;
						l=r=j;
					}else{
						l = min(l,j);
						r = max(r,j);
						t = min(i,t);
						b = max(b,i);
					}
				}
			}
		}
		int bian = max((r-l+1),(b-t+1));
		int need = bian*bian;

		if(need>n*m||bian>n||bian>m){
			printf("-1\n");
		}else{
			printf("%d\n",need-cnt);
		}

	}

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
char s[110][110];

int  main()
{
    int numw=0,numb=0;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        for(int j=0;j<m;j++){
            if(s[i][j]=='W') numw++;
            else if(s[i][j]=='B') numb++;
        }
    }
    if(numw==0){
        printf("-1\n");
        return 0;
    }
    if(numb==0){
        printf("1\n");
        return 0;
    }
    int l=110,r=-1,t=110,b=-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);
                t=min(t,i);
                b=max(b,j);
        }
    //printf("%d %d %d %d\n",l,r,t,b);
    int ans=0;
    for(int i=t;i<=b;i++)
        for(int j=l;j<=r;j++)
            if(s[i][j]=='W') ans++;
    int temp1=b-t+1;
    int temp2=r-l+1;
    if(temp1>temp2) ans+=temp1*(temp1-temp2);
    else if(temp1<temp2) ans+=temp2*(temp2-temp1);
    if(ans==n*m){
        printf("1\n");
        return 0;
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
char s[110][110];

int  main()
{
    int numw=0,numb=0;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        for(int j=0;j<m;j++){
            if(s[i][j]=='W') numw++;
            else if(s[i][j]=='B') numb++;
        }
    }
    if(numw==0){
        printf("-1\n");
        return 0;
    }
    if(numb==0){
        printf("1\n");
        return 0;
    }
    int l=110,r=-1,t=110,b=-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);
                t=min(t,i);
                b=max(b,j);
        }
    //printf("%d %d %d %d\n",l,r,t,b);
    int ans=0;
    for(int i=t;i<=b;i++)
        for(int j=l;j<=r;j++)
            if(s[i][j]=='W') ans++;
    int temp1=b-t+1;
    int temp2=r-l+1;
    if(temp1>temp2) ans+=temp1*(temp1-temp2);
    else if(temp1<temp2) ans+=temp2*(temp2-temp1);
    if((ans+numb)==n*m){
        printf("-1\n");
        return 0;
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
char s[110][110];

int  main()
{
    int numw=0,numb=0;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        for(int j=0;j<m;j++){
            if(s[i][j]=='W') numw++;
            else if(s[i][j]=='B') numb++;
        }
    }
    if(numb==0){
        printf("1\n");
        return 0;
    }
    int l=110,r=-1,t=110,b=-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);
                t=min(t,i);
                b=max(b,j);
        }
    //printf("%d %d %d %d\n",l,r,t,b);
    int ans=0;
    for(int i=t;i<=b;i++)
        for(int j=l;j<=r;j++)
            if(s[i][j]=='W') ans++;
    int temp1=b-t+1;
    int temp2=r-l+1;
    if(temp1>n||temp1>m||temp2>n||temp2>m){
        printf("-1\n");
        return 0;
    }
    if(temp1>temp2) ans+=temp1*(temp1-temp2);
    else if(temp1<temp2) ans+=temp2*(temp2-temp1);
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
char s[110][110];

int  main()
{
    int numw=0,numb=0;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        for(int j=0;j<m;j++){
            if(s[i][j]=='W') numw++;
            else if(s[i][j]=='B') numb++;
        }
    }
    if(numw==0){
        printf("-1\n");
        return 0;
    }
    if(numb==0){
        printf("1\n");
        return 0;
    }
    int l=110,r=-1,t=110,b=-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);
                t=min(t,i);
                b=max(b,j);
        }
    //printf("%d %d %d %d\n",l,r,t,b);
    int ans=0;
    for(int i=t;i<=b;i++)
        for(int j=l;j<=r;j++)
            if(s[i][j]=='W') ans++;
    int temp1=b-t+1;
    int temp2=r-l+1;
    if(temp1>temp2) ans+=temp1*(temp1-temp2);
    else if(temp1<temp2) ans+=temp2*(temp2-temp1);
    if(ans==n*m){
        printf("-1\n");
        return 0;
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
char s[110][110];

int  main()
{
    int numw=0,numb=0;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        for(int j=0;j<m;j++){
            if(s[i][j]=='W') numw++;
            else if(s[i][j]=='B') numb++;
        }
    }
    if(numb==0){
        printf("1\n");
        return 0;
    }
    int l=110,r=-1,t=110,b=-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);
                t=min(t,i);
                b=max(b,i);
        }
    //printf("%d %d %d %d\n",l,r,t,b);
    int ans=0;
    for(int i=t;i<=b;i++)
        for(int j=l;j<=r;j++)
            if(s[i][j]=='W') ans++;
    int temp1=b-t+1;
    int temp2=r-l+1;
    //printf("%d %d\n",temp1,temp2);
    if(temp1>n||temp1>m||temp2>n||temp2>m){
        printf("-1\n");
        return 0;
    }
    if(temp1>temp2) ans+=temp1*(temp1-temp2);
    else if(temp1<temp2) ans+=temp2*(temp2-temp1);
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector< vi > vvi;
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout<<x<<endl;
#define disp(x) cout<<x<<" ";
#define let(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w<<endl;
ll MOD=1e9+7;
ll MAX=1e17;

int main(){
	fast_io;
	int n,m;
	cin>>n>>m;
	int a[n][m];
	char c;
	for (int i = 0; i < n; ++i)
	{
		for(int j=0;j<m;++j) {
			cin>>c;
			if(c=='W') a[i][j]=0;
			else a[i][j]=1;
		}
	}
	int top_left_x=-1,top_left_y=-1,top_right_x=-1,top_right_y=-1,bottom_left_x=-1,bottom_left_y=-1,bottom_right_x=-1,bottom_right_y=-1;
	for (int i = 0; i < n; ++i)
	{
		int x=-1,y=-1;
		for(int j=0;j<m;++j){
			if(a[i][j]){
				if(top_left_x==-1){
					top_left_x=j;
					top_left_y=i;
				}
				else {
					x=j;
					y=i;
				}
			}
		}
		if(top_right_x==-1){
			top_right_x=x;
			top_right_y=y;
		}
	}
	for (int i = n-1; i >=0; --i)
	{
		// show("fds");
		int x=-1,y=-1;
		for(int j=0;j<m;++j){
			if(a[i][j]){
				if(bottom_left_x==-1){
					bottom_left_x=j;
					bottom_left_y=i;
				}
				else {
					x=j;
					y=i;
				}
			}
		}
		if(bottom_right_x==-1){
			bottom_right_x=x;
			bottom_right_y=y;
		}
	}
	if(top_right_x==-1){
		if(top_left_x!=-1) {show(0);}
		else {show(1);}
		// show("fdsafdsa");
		return 0;
	}
	// let(top_left_x,top_left_y,top_right_x,top_right_y);
	// let(bottom_left_x,bottom_left_y,bottom_right_x,bottom_right_y);
	int side=0,ver_side=0;
	ver_side=(max(bottom_right_y,bottom_left_y))-(min(top_left_y,top_right_y));
	side=max(ver_side,((max(top_right_x,bottom_right_x))-(min(top_left_x,bottom_left_x))));
	int top=-1,bottom=-1,left=-1,right=-1;
	// show(ver_side);
	// show(side);
	if(ver_side<side){
		if(n<side+1){
			show(-1);
			return 0;
		}
		else{
			left=min(top_left_x,bottom_left_x);
			right=max(top_right_x,bottom_right_x);
			if(min(top_right_y,top_left_y)<=side-ver_side){
				top=0;
				bottom=side;
			}
			else{
				bottom = max(bottom_right_y,bottom_left_y);
				top = bottom-side;
			}
		}
	}
	else {
		if(m<ver_side+1){
			show(-1);
			return 0;
		}
		else{
			top=min(top_left_y,top_right_y);
			bottom=max(bottom_right_y,bottom_left_y);
			if(min(top_left_x,bottom_left_x)<=ver_side-side){
				left=0;
				right=ver_side;
			}
			else{
				right = max(bottom_right_y,top_right_y);
				left = right-ver_side;
			}
		}
	}
	int ans=0;
	// let(top,left,right,bottom);
	for(int i=top;i<=bottom;++i){

		for(int j=left;j<=right;++j){
			if(a[i][j]==0) ans++;
		}
	}
	show(ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector< vi > vvi;
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout<<x<<endl;
#define disp(x) cout<<x<<" ";
#define let(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w<<endl;
ll MOD=1e9+7;
ll MAX=1e17;

int main(){
	fast_io;
	int n,m;
	cin>>n>>m;
	int a[n][m];
	char c;
	for (int i = 0; i < n; ++i)
	{
		for(int j=0;j<m;++j) {
			cin>>c;
			if(c=='W') a[i][j]=0;
			else a[i][j]=1;
		}
	}
	int top=-1,bot=-1,left=-1,right=-1;
	for (int i = 0; i < n; ++i)
	{
		for(int j=0;j<m;++j){
			if(a[i][j]){
				if(top==-1) {
					top=i;
					break;
				}
			}
		}
	}
	for (int i = n-1; i >= 0; --i)
	{
		for(int j=0;j<m;++j){
			if(a[i][j]){
				if(bot==-1) {
					bot=i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < m; ++i)
	{
		for(int j=0;j<n;++j){
			if(a[j][i]){
				if(left==-1) {
					left=i;
					break;
				}
			}
		}
	}
	for (int i = m-1; i >= 0; --i)
	{
		for(int j=0;j<n;++j){
			if(a[j][i]){
				if(right==-1) {
					right=i;
					break;
				}
			}
		}
	}
	if(top==-1){
		show(1);
		return 0;
	}
	// let(top,bot,left,right);
	int ver=bot-top;
	int hor=right-left;
	int len=max(ver,hor);
	if(len+1>n or len+1>m){
		show(-1);
		return 0;
	}
	int ans=(len+1)*(len-min(ver,hor));
	// show(ans);
	for (int i = top; i <= bot; ++i)
	{
		for(int j=left;j<=right;++j){
			if(a[i][j]==0) ans++;
		}
	}
	show(ans);
	return 0;
}