#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double num4, double b) { return fabs(num4 - b) < 1e-9; }
const int INF = 1 << 29;
inline int two(int len) { return 1 << len; }
inline int test(int len, int b) { return (len >> b) & 1; }
inline void set_bit(int &len, int b) { len |= two(b); }
inline void unset_bit(int &len, int b) { len &= ~two(b); }
inline int last_bit(int len) { return len & (-len); }
inline int ones(int len) {
  int res = 0;
  while (len && ++res) len -= len & (-len);
  return res;
}
template <class T>
void chmax(T &num4, const T &b) {
  num4 = max(num4, b);
}
template <class T>
void chmin(T &num4, const T &b) {
  num4 = min(num4, b);
}
inline void add(int &numx, int y) {
  numx += y;
  if (numx >= 1000000007) numx -= 1000000007;
  if (numx < 0) numx += 1000000007;
}
inline int mul(int numx, int y) { return ((long long)numx * y) % 1000000007; }
int gcd(int num4, int b) {
  if (b) return gcd(b, num4 % b);
  return num4;
}
int power(int num4, int pex) {
  int ret = 1;
  while (pex) {
    if (pex & 1) ret = mul(ret, num4);
    num4 = mul(num4, num4);
    pex /= 2;
  }
  return ret;
}
int phi(int len) {
  int ret = len;
  int idxx = 2;
  if (len % idxx == 0) {
    ret -= ret / idxx;
    while (len % idxx == 0) len /= idxx;
  }
  for (idxx = 3; idxx * idxx <= len; idxx++)
    if (len % idxx == 0) {
      ret -= ret / idxx;
      while (len % idxx == 0) len /= idxx;
    }
  if (len > 1) ret -= ret / len;
  return ret;
}
using namespace std;
int main() {
  bool flags = true;
  int num1, num2;
  cin >> num1 >> num2;
  int vals1 = num1 - 1, v2s = 0, v3s = 0, v4s = num2 - 1, bbs = 0;
  for (int i = 0; i < num1; ++i)
    for (int j = 0; j < num2; ++j) {
      char chh;
      cin >> chh;
      if (chh == 'B') {
        vals1 = min(vals1, i);
        v2s = max(v2s, i);
        v4s = min(v4s, j);
        v3s = max(v3s, j);
        flags = false;
        ++bbs;
      }
    }
  if (flags) {
    cout << 1;
    return 0;
  }
  int weights = v3s - v4s + 1, h = v2s - vals1 + 1;
  if (weights > num1 || h > num2) {
    cout << -1;
    return 0;
  }
  int len = max(weights, h);
  cout << len * len - bbs << endl;
  return 0;
}
