#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(-1,((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);
			// show(top);
			// show(bottom);
			if(min(top_left_x,bottom_left_x)<=ver_side-side){
				left=0;
				right=ver_side;
			}
			else{
				// show(min(top_left_x,bottom_left_x));
				right = max(bottom_right_x,top_right_x);
				// show(right);
				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_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=left;i<=right;++i){

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


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(c-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w,y;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		y  = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					y++;
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(r==100 && c==99)
			printf("%d %d %d\n",rowwise,clmwise,y);
			if(c>=rowwise)
				printf("%d\n",(rowwise*rowwise)-y);
			else
				printf("-1\n");
		}
		else if(rowwise<clmwise)
		{
			if(r==100 && c==99)
			printf("%d %d %d\n",rowwise,clmwise,y);
			if(r>=clmwise)
				printf("%d\n",(clmwise*clmwise)-y);
			else
				printf("-1\n");
		}
		else
			printf("%d\n",(clmwise*clmwise)-y);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(r-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
//			if(r<clmwise || c<clmwise)
//				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("0\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(r<rowwise || c<rowwise)
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(c-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
			if(r<clmwise || c<clmwise)
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(r<rowwise || c<rowwise)
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(c-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
			if(r<clmwise || c<clmwise)
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w,y;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		y  = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					y++;
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			printf("%d %d\n",rowwise,clmwise);
			if(c>=rowwise)
				printf("%d\n",(rowwise*rowwise)-y);
			else
				printf("-1\n");
		}
		else if(rowwise<clmwise)
		{
			printf("%d %d\n",rowwise,clmwise);
			if(r>=clmwise)
				printf("%d\n",(clmwise*clmwise)-y);
			else
				printf("-1\n");
		}
		else
			printf("%d %d\n",rowwise,clmwise),printf("%d\n",(clmwise*clmwise)-y);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(r-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
//			if(r<clmwise || c<clmwise)
//				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			cnt =0;
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w,y;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		y  = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					y++;
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c>=rowwise)
				printf("%d\n",(rowwise*rowwise)-y);
			else
				printf("-1\n");
		}
		else if(rowwise<clmwise)
		{
			if(r>=clmwise)
				printf("%d\n",(clmwise*clmwise)-y);
			else
				printf("-1\n");
		}
		else
			printf("%d\n",(clmwise*clmwise)-y);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(c-rt>=rowwise)
			{
				 ii = c-rt-rowwise;
				jj = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(r-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
//			if(r<clmwise || c<clmwise)
//				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			cnt =0;
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w,y;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		y  = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					y++;
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
//			if(r==100 && c==99)
//			printf("%d %d %d\n",rowwise,clmwise,y);
			if(c>=rowwise)
				printf("%d\n",(rowwise*rowwise)-y);
			else
				printf("-1\n");
		}
		else if(rowwise<clmwise)
		{
//			if(r==100 && c==99)
//			printf("%d %d %d\n",rowwise,clmwise,y);
			if(r>=clmwise)
				printf("%d\n",(clmwise*clmwise)-y);
			else
				printf("-1\n");
		}
		else
			printf("%d\n",(clmwise*clmwise)-y);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w,y;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		y  = 0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					y++;
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][j]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			if(r==100 && c==99)
			printf("%d %d\n",rowwise,clmwise);
			if(c>=rowwise)
				printf("%d\n",(rowwise*rowwise)-y);
			else
				printf("-1\n");
		}
		else if(rowwise<clmwise)
		{
			if(r==100 && c==99)
			printf("%d %d\n",rowwise,clmwise);
			if(r>=clmwise)
				printf("%d\n",(clmwise*clmwise)-y);
			else
				printf("-1\n");
		}
		else
			printf("%d\n",(clmwise*clmwise)-y);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Isvalid(nx,mx,n,m) nx>=0&&nx<n&&mx>=0&&mx<m
#define mem(a,b)      memset(a, b, sizeof (a))
#define pb            push_back
#define sz            size()
#define kohli         0
#define ll            long long
#define si(x)         scanf("%lld",&x)
#define vsort(v)      sort(v.begin(),v.end())
#define nl            printf("\n")
#define pi(x)         printf("%d",x)
#define ps(ch)        printf("%s",ch)
#define pll(x)        printf("%lld",x)
#define mod           1000000007
#define sp            printf(" ");
#define mx 179426549

int main()
{
    //seive();
    //totient();
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int r,c,i,j,rt,dow,lef,up,ii,jj,ito,jto,rowwise,clmwise,cnt,w;
    char ch[1010][1010];
    while(scanf("%d %d",&r,&c)==2)
	{
		for(i=0;i<r;i++)
		{
			scanf("%s",ch[i]);
		}
		int flag=0;
		rt = 0;
		dow = 0;
		lef = 0;
		up = 0;
		int fff=0;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
				{
					fff=1;
					break;
				}
			}
		}
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B')
					flag=1;
			}
			if(flag==0)
				up++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=r-1;i>=0;i--)
		{
			for(j=0;j<c;j++)
			{
				if(ch[i][j]=='B' && flag==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				dow++;
			else
				break;
			flag=0;
		}
		flag=0;
		for(i=0;i<c;i++)
		{
			for(j=0;j<r;j++)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				lef++;
			else
			    break;
			flag=0;
		}
		flag=0;
		for(i=c-1;i>=0;i--)
		{
			for(j=r-1;j>=0;j--)
			{
				if(ch[j][i]=='B')
				{
					flag=1;
				}
			}
			if(flag==0)
				rt++;
			else
				break;
			flag=0;
		}
		rowwise = r-(up+dow);
		clmwise = c-(rt+lef);
		cnt = 0;
		flag=0;
		if(fff==0)
		{
			printf("1\n");
			return 0;
		}
		if(rowwise>clmwise)
		{
			//printf("%d %d\n",rowwise,clmwise);
			if(c-rt>=rowwise)
			{
				 jj = c-rt-rowwise;
				ii = up;
				ito = ii+rowwise;
				jto = jj + rowwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else if(c-lef>=rowwise)
			{
				ii = up;
				jj = lef;
				ito = ii+rowwise;
				jto = jj + rowwise;
			}
			else
				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}

			w = rowwise * rowwise;
			printf("%d\n",w-cnt);
		}
		else if(rowwise<clmwise)
		{
			cnt = 0;
			if(r-up>=clmwise)
			{
				ii = up;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
			}
			else if(r-dow>=clmwise)
			{
				ii = r-dow-clmwise;
				jj = lef;
				ito = ii+clmwise;
				jto = jj + clmwise;
				//printf("%d %d %d %d\n",ii,jj,ito,jto);
			}
			else
				flag=1;
//			if(r<clmwise || c<clmwise)
//				flag=1;
			if(flag==1)
			{
				printf("-1\n");
				return 0;
			}

			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
		else
		{
			cnt =0;
			ii = up;
			jj = lef;
			ito = ii+clmwise;
			jto = jj + clmwise;
			for(i=ii;i<ito;i++)
			{
				for(j=jj;j<jto;j++)
				{
					if(ch[i][j]=='B')
						cnt++;
				}
			}
			w = clmwise*clmwise;
			printf("%d\n",w-cnt);
		}
	}
    return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

#define ii pair<int,int>

int n,m,num;
int l,r,u,d;
char a[110][110];

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("\n");
		for(int j=1;j<=m;j++)
			scanf("%c",&a[i][j]);
	}
	l=u=9999999;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='B')
			{
				l=min(j,l);
				r=max(j,r);
				u=min(i,u);
				d=max(d,i);
				num++;
			}
	if(num)
	{
		int leng=max(d-u+1,r-l+1);
		if(r-l+1<=d-u+1)
		{
			if(l+leng-1>m && r-leng+1<1)
				cout<<-1;
			else cout<<leng*leng-num;
		}
		else
		{
			if(d-leng+1<1 && u+leng-1>n)
				cout<<-1;
			else cout<<leng*leng-num;
		}
	}
	else cout<<1;
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

#define ii pair<int,int>

int n,m,num;
int l,r,u,d;
char a[110][110];

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("\n");
		for(int j=1;j<=m;j++)
			scanf("%c",&a[i][j]);
	}
	l=u=9999999;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='B')
			{
				l=min(j,l);
				r=max(j,r);
				u=min(i,u);
				d=max(d,i);
				num++;
			}
	if(num)
	{
		int leng=max(d-u+1,r-l+1);
		if(r-l+1<=d-u+1)
		{
			if(l+leng-1>m && r-leng+1<1)
				cout<<-1;
			else cout<<leng*leng-num;
		}
		else
		{
			if(d-leng+1<1 || u+leng-1>n)
				cout<<-1;
			else cout<<leng*leng-num;
		}
	}
	else cout<<1;
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

#define ii pair<int,int>

int n,m,num;
int l,r,u,d;
char a[110][110];

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("\n");
		for(int j=1;j<=m;j++)
			scanf("%c",&a[i][j]);
	}
	l=u=9999999;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='B')
			{
				l=min(j,l);
				r=max(j,r);
				u=min(i,u);
				d=max(d,i);
				num++;
			}
	if(num)
	{
		int leng=max((r-l+1),(d-u+1));
		if((l+leng-1>m || u+leng-1>n) && (r-leng+1<1 || d-leng+1<1)) cout<<-1;
		else cout<<leng*leng-num;
	}
	else cout<<1;
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

#define ii pair<int,int>

int n,m,num;
int l,r,u,d,flag;
char a[110][110];

bool judge(int x,int y,int len)
{
	int tx=x+len-1;
	int ty=y+len-1;
	if(x<=u && y<=l && tx>=d && ty>=r && tx<=n && ty<=m)
		return true;
	return false;
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("\n");
		for(int j=1;j<=m;j++)
			scanf("%c",&a[i][j]);
	}
	l=u=9999999;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='B')
			{
				l=min(j,l);
				r=max(j,r);
				u=min(i,u);
				d=max(d,i);
				num++;
			}
	if(num)
	{
		int leng=max(d-u+1,r-l+1);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
				if(judge(i,j,leng))
				{
					cout<<leng*leng-num;
					flag=1;
					break;
				}
			if(flag) break;
		}
		if(!flag) cout<<-1;
	}
	else cout<<1;
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

#define ii pair<int,int>

int n,m,num;
int l,r,u,d,flag;
char a[110][110];

bool judge(int x,int y,int len)
{
	int tx=x+len-1;
	int ty=y+len-1;
	if(x<=u && y<=l && tx>=d && ty>=r && tx<=n && ty<=m)
		return true;
	return false;
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("\n");
		for(int j=1;j<=m;j++)
			scanf("%c",&a[i][j]);
	}
	l=u=9999999;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='B')
			{
				l=min(j,l);
				r=max(j,r);
				u=min(i,u);
				d=max(d,i);
				num++;
			}
	if(num)
	{
		int leng=max(d-u+1,r-l+1);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(judge(i,j,leng))
				{
					cout<<leng*leng-num;
					flag=1;break;
				}
		if(!flag) cout<<-1;
	}
	else cout<<1;
	return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   if(pow((temp2-temp1+1)>(b)||pow((temp4-temp3+1),2)>(a))
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   if(pow((temp2-temp1+1),2)>(a*b)||pow((temp4-temp3+1),2)>(a*b))
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   it(temp==a*b)
   {
       cout<<"0"<<endl;
       return 0;
   }
   if(pow((temp2-temp1+1),2)>=(a*b)||pow((temp4-temp3+1),2)>=(a*b))
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   if((temp2-temp1+1)>(b)||(temp4-temp3+1)>a)
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   if(pow((temp2-temp1+1),2)>=(a*b)||pow((temp4-temp3+1),2)>=(a*b))
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


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

int main( ) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,n,i,sum=0,j;
    cin>>a>>b;
    string s[a];
   for(i=0;i<a;i++)
   cin>>s[i];
   int temp1=9999,temp2=0,temp3=9999,temp4=0;
   int temp=0;
   for(i=0;i<a;i++)
   {
       for(j=0;j<b;j++)
       {
           if(s[i][j]=='B')
           {
               temp++;
               if(temp1>i)
                temp1=i;
               if(temp2<i)
                 temp2=i;
               if(temp3>j)
                  temp3=j;
               if(temp4<j)
                  temp4=j;
           }
       }
   }

   if(temp==0)
   {
       cout<<"1"<<endl;
       return 0;
   }
   if(temp==a*b)
   {
       cout<<"0"<<endl;
       return 0;
   }
   if(pow((temp2-temp1+1),2)>=(a*b)||pow((temp4-temp3+1),2)>=(a*b))
   {
       cout<<"-1"<<endl;
       return 0;
   }
   if(temp2-temp1>temp4-temp3)
     cout<<pow((temp2-temp1+1),2)-temp;
   else if(temp2-temp1<=temp4-temp3)
     cout<<pow((temp4-temp3+1),2)-temp;

     return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }
        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }

        ans=INT_MAX;
        for(i=bottom_row-square+1; i<=upper_row and i+square-1<n; i++)
        {
            cnt=0;

            for(j=right_column-square+1; j<=left_column and j+square-1<m; j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                //pr3(i,j,cnt);
                /*if(upper_row>=i and bottom_row<=i and left_column>=j and right_column<=j)
                {
                    if(upper_row>=i+square-1 and bottom_row<=i+square-1 and left_column>=i+square-1 and right_column<=i+square-1) ans=min(ans,cnt);
                }*/
                ans=min(ans,cnt);
            }
            //break;
        }
        pr1(ans);
    }
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }
        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }

        ans=INT_MAX;
        for(i=max(0,bottom_row-square+1); i<=upper_row and i+square-1<n; i++)
        {
            cnt=0;

            for(j=max(0,right_column-square+1); j<=left_column and j+square-1<m; j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                ans=min(ans,cnt);
            }
        }
        pr1(ans);
    }
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }
        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }

        ans=INT_MAX;
        for(i=0; i<n; i++)
        {
            cnt=0;

            for(j=0; j<=m; j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                //break;
                if(i>=upper_row and i<=bottom_row and j>=left_column and j<=right_column) ans=min(ans,cnt);
            }
            //break;
        }
        pr1(ans);
    }
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        //pr4(upper_row,bottom_row,left_column,right_column);
        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }
        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }

        ans=INT_MAX;
        for(i=bottom_row-square+1; i<=upper_row and i+square-1<n; i++)
        {
            cnt=0;

            for(j=right_column-square+1; j<=left_column and j+square-1<m; j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                //pr3(i,j,cnt);
                /*if(upper_row>=i and bottom_row<=i and left_column>=j and right_column<=j)
                {
                    if(upper_row>=i+square-1 and bottom_row<=i+square-1 and left_column>=i+square-1 and right_column<=i+square-1) ans=min(ans,cnt);
                }*/
                ans=min(ans,cnt);
            }
            //break;
        }
        pr1(ans);
    }
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        //pr4(upper_row,bottom_row,left_column,right_column);
        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }
        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }

        ans=INT_MAX;
        for(i=max(0,bottom_row-square+1); i<=min(upper_row,n-1); i++)
        {
            cnt=0;

            for(j=max(0,right_column-square+1); j<=min(left_column,m-1); j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                //pr3(i,j,cnt);
                /*if(upper_row>=i and bottom_row<=i and left_column>=j and right_column<=j)
                {
                    if(upper_row>=i+square-1 and bottom_row<=i+square-1 and left_column>=i+square-1 and right_column<=i+square-1) ans=min(ans,cnt);
                }*/
                ans=min(ans,cnt);
            }
            //break;
        }
        pr1(ans);
    }
    return 0;
}


/* -------------------------------- */
/* Name: MD. Khairul Basar          */
/* Institute: HSTU                  */
/* Dept: CSE                        */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */

#include <bits/stdc++.h>
/* all header files */

#define fs            first
#define sc            second
#define sp            printf(" ")
#define nl            printf("\n")
#define pb(a)         push_back(a)

#define setzero(a)    memset(a,0,sizeof(a))
#define setneg(a)     memset(a,-1,sizeof(a))
#define setinf(a)     memset(a,126,sizeof(a))

#define tc1(x)        printf("Case %d: ",x)
#define tc2(x)        printf("Case #%d: ",x)
#define tc3(x)        printf("Case %d:\n",x)
#define tc4(x)        printf("Case #%d:\n",x)

#define iin(x)        scanf("%d",&x)
#define din(x)        scanf("%lf",&x)
#define lin(x)        scanf("%lld",&x)
#define clin(x)       scanf("%I64d",&x)

#define pr1(x)        cout<<x<<"\n"
#define pr2(x,y)      cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)    cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z)  cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast          ios::sync_with_stdio(0)
#define read          freopen("input.txt","r",stdin)
#define write         freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */

LL MOD_EXPO(LL b, LL p, LL m)
{
    if(p==0) return 1;
    LL ret=MOD_EXPO(b,p/2,m)%m;
    ret=(ret*ret)%m;
    return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
    LL i,ans=1;
    for(i=1; i<=K; i++) ans*=N;
    return ans;
}
int SET(int N, int pos)
{
    return (N | (1<<pos));
}
int RESET(int N, int pos)
{
    return (N & !(1<<pos));
}
bool CHECK(int N, int pos)
{
    return (N & (1<<pos));
}
/* necessary functions */

int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */

int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=100;
const long long int mod=1e9+7;
/* global declarations */

string str[mx+5];
int sum[mx+5][mx+5];

int main()
{
    int n,m,i,j,upper_row,bottom_row,left_column,right_column,rows,columns,square,ans,cnt,k;
    while(cin>>n>>m)
    {
        for(i=0; i<n; i++) cin>>str[i];

        setzero(sum);
        upper_row=INT_MAX;
        bottom_row=INT_MIN;
        left_column=INT_MAX;
        right_column=INT_MIN;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(j==0) sum[i][j]=(str[i][j]=='W');
                else sum[i][j]=sum[i][j-1]+(str[i][j]=='W');
                if(i) sum[i][j]+=sum[i-1][j];
                if(i and j)sum[i][j]-=sum[i-1][j-1];
            }
        }

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and upper_row==INT_MAX)
                {
                    upper_row=i;
                    break;
                }
            }
            if(upper_row<INT_MAX) break;
        }
        for(i=n-1; i>=0; i--)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='B' and bottom_row==INT_MIN)
                {
                    bottom_row=i;
                    break;
                }
            }
            if(bottom_row<INT_MIN) break;
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and left_column==INT_MAX)
                {
                    left_column=i;
                    break;
                }
            }
            if(left_column<INT_MAX) break;
        }
        for(i=m-1; i>=0; i--)
        {
            for(j=0; j<n; j++)
            {
                if(str[j][i]=='B' and right_column==INT_MIN)
                {
                    right_column=i;
                    break;
                }
            }
            if(right_column<INT_MIN) break;
        }
        rows=bottom_row-upper_row+1;
        columns=right_column-left_column+1;
        square=max(rows,columns);

        //pr4(upper_row,bottom_row,left_column,right_column);
        if(upper_row==INT_MAX)
        {
            pr1(1);
            continue;
        }
        if(min(n,m)<square)
        {
            pr1(-1);
            continue;
        }

        ans=INT_MAX;
        for(i=max(0,bottom_row-square+1); i<=upper_row and i+square-1<n; i++)
        {
            cnt=0;

            for(j=max(0,right_column-square+1); j<=left_column and j+square-1<m; j++)
            {
                cnt=sum[i+square-1][j+square-1];
                if(i) cnt-=sum[i-1][j+square-1];
                if(j) cnt-=sum[i+square-1][j-1];
                if(i and j) cnt+=sum[i-1][j-1];
                //pr3(i,j,cnt);
                /*if(upper_row>=i and bottom_row<=i and left_column>=j and right_column<=j)
                {
                    if(upper_row>=i+square-1 and bottom_row<=i+square-1 and left_column>=i+square-1 and right_column<=i+square-1) ans=min(ans,cnt);
                }*/
                ans=min(ans,cnt);
            }
            //break;
        }
        pr1(ans);
    }
    return 0;
}


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

typedef long long ll;
const int maxn=100+10;

char mp[maxn][maxn];

int main()
{
        int n,m;
        cin >> n >> m;
        for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
        int cnt=0;
        int w1=-1,w2=-1,b1=-1,b2=-1;
        for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                        if(mp[i][j]=='B')
                        {
                                w2=max(w2,i);
                                b2=max(b2,j);
                                if(w1==-1)
                                        w1=i;
                                if(b1==-1)
                                        b1=j;
                        }
                }
//      cout << w1 << w2 << b1 << b2;
        if(w1==-1&&w2==-1&&b1==-1&&b2==-1)
        {
                cout << 1 << endl;
                return 0;
        }
        for(int i=w1;i<=w2;i++)
        {
                for(int j=w1;j<=w2;j++)
                {
                        if(j>=m)
                        {
                                cout << -1 << endl;
                                return 0;
                        }
                        if(mp[i][j]=='W')
                                cnt++;
                }
        }
        for(int i=b1;i<=b2;i++)
        {
                if(i>=n)
                {
                        cout << -1 << endl;
                        return 0;
                }
                for(int j=b1;j<=b2;j++)
                {
                        if(mp[i][j]=='W')
                                                                                49,1-8       顶端


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

typedef long long ll;
const int maxn=100+10;

char mp[maxn][maxn];

int main()
{
        int n,m;
        cin >> n >> m;
        for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
        int cnt=0;
        int w1=-1,w2=-1,b1=-1,b2=-1;
        int flag=1;
        for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                        if(mp[i][j]=='B')
                        {
                                w2=max(w2,i);
                                b2=max(b2,j);
                                if(w1==-1)
                                        w1=i;
                                if(b1==-1)
                                        b1=j;
                                flag=0;
                        }
                }
//      cout << w1 << w2 << b1 << b2;
        if(flag)
        {
                cout << 1 << endl;
                return 0;
        }
        int res=max(w2-w1,b2-b1);
        if(res>=n||res>=m)
        {
                cout << -1 << endl;
                return 0;
        }
        int x,y;
        if(w2-w1>b2-b1)
                x=w1,y=w2;
        else
                x=b1,y=b2;
        for(int i=x;i<=y;i++)
        {
                for(int j=x;j<=y;j++)
                {
                        if(mp[i][j]=='W')
                                cnt++;
                }
        }
        cout << cnt << endl;
        return 0;
}


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

typedef long long ll;
const int maxn=100+10;

char mp[maxn][maxn];

int main()
{
        int n,m;
        cin >> n >> m;
        for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
        int cnt=0;
        int w1=-1,w2=-1,b1=-1,b2=-1;
        for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                        if(mp[i][j]=='B')
                        {
                                w2=max(w2,i);
                                b2=max(b2,j);
                                if(w1==-1)
                                        w1=i;
                                if(b1==-1)
                                        b1=j;
                        }
                }
//      cout << w1 << w2 << b1 << b2;
        if(w1==-1&&w2==-1&&b1==-1&&b2==-1)
        {
                cout << 1 << endl;
                return 0;
        }
        if(b2-b1>=n||w2-w1>=m)
        {
                cout << -1 << endl;
                return 0;
        }
        int x,y;
        if(b2-b1<w2-w1)
                x=w1,y=w2;
        else
                x=b1,y=b2;
        for(int i=x;i<=y;i++)
                for(int j=x;j<=y;j++)
                        if(mp[i][j]=='W')
                                cnt++;
        cout << cnt << endl;
        return 0;
}


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

typedef long long ll;
const int maxn=100+10;

char mp[maxn][maxn];

int main()
{
        int n,m;
        cin >> n >> m;
        for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
        int cnt=0;
        int w1=maxn,w2=-1,b1=maxn,b2=-1;
        int flag=1;
        for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                        if(mp[i][j]=='B')
                        {
                                w2=max(w2,i);
                                b2=max(b2,j);
                                w1=min(w1,i);
                                b1=min(b1,j);
                                flag=0;
                        }
                }
//      cout << w1 << w2 << b1 << b2;
        if(flag)
        {
                cout << 1 << endl;
                return 0;
        }
        int res=max(w2-w1,b2-b1);
//      cout << res << endl;
        if(res>=n||res>=m)
        {
                cout << -1 << endl;
                return 0;
        }
        int x,y;
        if(w2-w1<res)
        {
                int r1=res-(w2-w1);
                if(w1-0>=r1)
                        w1=w1-r1;
                else
                {
                        r1-=w1;
                        w1=0;
                        w2+=r1;
                }
        }
        if(b2-b1<res)
        {
                int r1=res-(b2-b1);
                if(b1-0>=r1)
                        b1=b1-r1;
                else
                {

                        r1-=b1;
                        b1=0;
                        b2+=r1;
                }
        }
//      cout << w1 << w2 << b1 << b2 << endl;
        for(int i=w1;i<=w2;i++)
        {
                for(int j=b1;j<=b2;j++)
                {
                        if(mp[i][j]=='W')
                                cnt++;
                }
        }
        cout << cnt << endl;
        return 0;
}


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

typedef long long ll;
const int maxn=100+10;

char mp[maxn][maxn];

int main()
{
        int n,m;
        cin >> n >> m;
        for(int i=0;i<n;i++)
                scanf("%s",mp[i]);
        int cnt=0;
        int w1=-1,w2=-1,b1=-1,b2=-1;
        for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                        if(mp[i][j]=='B')
                        {
                                w2=max(w2,i);
                                b2=max(b2,j);
                                if(w1==-1)
                                        w1=i;
                                if(b1==-1)
                                        b1=j;
                        }
                }
//      cout << w1 << w2 << b1 << b2;
        if(w1==-1&&w2==-1&&b1==-1&&b2==-1)
        {
                cout << 1 << endl;
                return 0;
        }
        for(int i=w1;i<=w2;i++)
        {
                for(int j=w1;j<=w2;j++)
                {
                        if(j>=m)
                        {
                                cout << -1 << endl;
                                return 0;
                        }
                        if(mp[i][j]=='W')
                                cnt++;
                }
        }
        for(int i=b1;i<=b2;i++)
        {
                if(i>=n)
                {
                        cout << -1 << endl;
                        return 0;
                }
                for(int j=b1;j<=b2;j++)
                {
                        if(mp[i][j]=='W')
                                cnt++;
                }
        }
        cout << cnt << endl;
        return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstddef>
#include <complex>
#include <ctime>
#include <climits>

#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <functional>
#include <iterator>
#include <typeinfo>
#include <utility>
#include <memory>

#include <cassert>

#if __cplusplus >= 201103L
#include <tuple>
#include <ratio>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long int64;
const int inf = 2000000000;
static inline int Rint()
{
	struct X{ int dig[256]; X(){
	for(int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1;
	}};
	static 	X fuck;int s = 1, v = 0, c;
	for (;!fuck.dig[c = getchar()];);
	if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48;
	for (;fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48));
	return s ? v : -v;
}
typedef vector<int> vi;
typedef map<int, int> mii;
typedef set<int> si;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, s, e) for (int i = (s); i < (e); ++i)

#if __cplusplus >= 201103L
#define foreach(iter, c) for(auto& iter : c)
#else
#define foreach(itr, c) for(__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#endif

template<typename T> static inline void cmax(T& a, const T& b){if(b>a)a=b;}
template<typename T> static inline void cmin(T& a, const T& b){if(b<a)a=b;}
#define SL static inline

#define dbg(x) cerr << (#x) << " = " << (x) << endl

const int maxn = 100005;

int answer[maxn];
int main()
{
	const int n = Rint(), m = Rint();
	for (int id = 0; id < m; ++id)
	{
		int u = Rint(), v = Rint(), w = Rint();
		int used[5] = {0};
		if (answer[u]) used[answer[u]] = 1;
		if (answer[v]) used[answer[v]] = 1;
		if (answer[w]) used[answer[w]] = 1;
		int can = 1;
		if (answer[u] == 0)
		{
			while (used[can]) ++can;
			answer[u] = can;
			used[can] = 1;
		}
		if (answer[v] == 0)
		{
			while (used[can]) ++can;
			answer[v] = can;
			used[can] = 1;
		}
		if (answer[w] == 0)
		{
			while (used[can]) ++can;
			answer[w] = can;
			used[can] = 1;
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		int val = answer[i];
		if (val == 0) val = 1;
		printf("%d ", val);
	}
	puts("");
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int arr[300005],color[100005],psnt[6];

main()
{
    int n,m,l;

    scanf("%d%d",&n,&m);
    l=m*3;

    for(int i=1; i<=l; i++)
    {
        scanf("%d",&arr[i]);
    }

    for(int i=1; i<=l; i+=3)
    {
        memset(psnt,0,sizeof psnt);
        for(int j=0; j<3; j++)
        {
            if(color[arr[i+j]])
            {
                psnt[ color[arr[i+j]] ]=1;
                break;
            }
        }

        for(int j=0; j<3; j++)
        {
            if(!color[arr[i+j]])
            {
                for(int k=1; k<=3; k++)
                {
                    if(!psnt[k])
                    {
                        psnt[k]=1;
                        color[arr[i+j]]=k;
                        break;
                    }
                }
            }
        }
    }

    for(int i=1; i<=n; i++)
        printf("%d ",color[i]);
}


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

int main()
{
	int n,m;
	int i;
	int x,y,z;
	int *color;
	scanf("%d%d",&n,&m);
	color = (int*)malloc((n+1)*sizeof(int));
	for(i = 0 ; i < n+1 ; i++)
	{
		color[i] = 0;
	}

	for(i = 0 ; i < m ; i++)
	{
		scanf("%d %d %d",&x,&y,&z);
		if(color[x] == 0 && color[y]==0 && color[z]==0)
		{
			color[x] = 1;
			color[y] = 2;
			color[z] = 3;
		}
		if(color[x] != 0 && color[y] == 0 && color[z] == 0)
		{
			color[y] = (color[x]%3) + 1;
			color[z] = (color[x]%3) + 2;
			if(color[z] > 3)
				color[z] = color[z]%3;
		}
		if(color[y]!= 0 && color[x] == 0 && color[z] == 0)
		{
			color[x] = (color[y]%3) + 1;
			color[z] = (color[y]%3) + 2;
			if(color[z] > 3)
				color[z] = color[z]%3;
		}
		if(color[z]!= 0 && color[y] == 0 && color[x] == 0)
		{
			color[y] = (color[z]%3) + 1;
			color[x] = (color[z]%3) + 2;
			if(color[x] > 3)
				color[x] = color[x]%3;
		}
		if(color[x]!= 0 && color[y] != 0 && color[z] == 0)
		{
			color[z] = 3 -(color[x]+color[y])%3;
		}

		if(color[x]!= 0 && color[y] == 0 && color[z] != 0)
		{
			color[y] = 3 -(color[x]+color[z])%3;
		}

		if(color[x]== 0 && color[y] != 0 && color[z] != 0)
		{
			color[x] = 3 -(color[z]+color[y])%3;
		}
	}
	for(i = 1 ; i < n+1 ; i++)
	{
		printf("%d ",color[i]);
	}
	return 0;
}


#include <stdio.h>

int a[200001];

int nextInt() {
    char ch;
    int sign, x;

    do {
        ch = getchar();
    } while (ch < '-');

    if (ch == '-') {
        sign = -1;
        ch = getchar();
    } else
        sign = 1;

    x = 0;
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }

    return sign * x;
}

void printInt(int x) {
    char ch[10];
    int sign, cnt;

    if (x < 0) {
        putchar('-');
        sign = -1;
    } else
        sign = 1;

    cnt = 0;
    do {
        ch[cnt++] = '0' + x % 10 * sign;
        x /= 10;
    } while (x);

    while (cnt--)
        putchar(ch[cnt]);
}

int color[100001];

int main(void) {
    int i;
    int n, m;

    n = nextInt();
    m = nextInt();

    while (m--) {
        int n1 = nextInt(), n2 = nextInt(), n3 = nextInt();
        if (color[n1]) {
            color[n2] = (color[n1] + 1) % 3;
            color[n3] = (color[n1] + 2) % 3;
        } else if (color[n2]) {
            color[n3] = (color[n2] + 1) % 3;
            color[n1] = (color[n2] + 2) % 3;
        } else if (color[n3]) {
            color[n1] = (color[n3] + 1) % 3;
            color[n2] = (color[n3] + 2) % 3;
        } else {
            color[n1] = 1;
            color[n2] = 2;
            color[n3] = 3;
        }
    }

    for (i = 1; i <= n; i++) {
        printInt(color[i]);
        putchar(' ');
    }
    putchar('\n');

    return 0;
}