#include <bits/stdc++.h>
using namespace std;
long long a[500010];
map<long long, long long> mp, vis;
vector<long long> v;
vector<pair<long long, long long> > vp;
string s, q;
char c;
int main() {
  long long n, m, mnr = INT_MAX, mnc = INT_MAX, mxr = 0, mxc = 0, f = 0, mx = 0;
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> c;
      if (c == 'B') {
        f++;
        mxr = max(mxr, i);
        mnr = min(mnr, i);
        mxc = max(mxc, j);
        mnc = min(mnc, j);
      }
    }
  }
  if (f == 0)
    cout << 1;
  else {
    mx = mxr - mnr + 1;
    mx = max(mx, mxc - mnc + 1);
    if (mx > min(n, m))
      cout << "-1";
    else {
      cout << (mx * mx) - f;
    }
  }
}
