#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c, cnt = 0;
  cin >> r >> c;
  int ul = INT_MAX, ur = 0, ll = INT_MAX, lr = 0;
  for (int i = 0; i < r; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < c; j++) {
      if (s[j] == 'B') {
        cnt++;
        ul = min(ul, i);
        ur = max(ur, i);
        ll = min(ll, j);
        lr = max(lr, j);
      }
    }
  }
  if (!cnt) {
    cout << 1;
    return 0;
  }
  int rn = ur - ul + 1;
  int cn = lr - ll + 1;
  if (rn < cn)
    rn += abs(rn - cn);
  else
    cn += abs(rn - cn);
  if (rn > r || cn > c) {
    cout << -1;
    return 0;
  }
  cout << rn * cn - cnt;
  return 0;
}
