#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, black = 0;
  string s;
  cin >> n >> m;
  int left = m, right = 0;
  int top = n, bottom = 0;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'B') {
        black += 1;
        left = min(j, left);
        right = max(j, right);
        top = min(i, top);
        bottom = max(i, bottom);
      }
    }
  }
  if (black == 0) {
    cout << 1 << endl;
    return 0;
  }
  int side = max(abs(left - right), abs(top - bottom)) + 1;
  if (side > n) {
    cout << -1 << endl;
    return 0;
  }
  if (side > m) {
    cout << -1 << endl;
    return 0;
  }
  cout << (side * side) - black << endl;
}
