#include <bits/stdc++.h>
using namespace std;
void find_first_last_occurence(string s, long long &p, long long &q) {
  for (long long j = 0; j < s.size(); j++) {
    if (s[j] == 'B') {
      p = min(p, j);
      q = max(q, j);
    }
  }
}
int main() {
  long long n, m;
  cin >> n >> m;
  long long ri, rj, ci, cj;
  ri = 101;
  rj = -1;
  ci = 101;
  cj = -1;
  long long total_count_black = 0;
  string s;
  bool found = false;
  for (long long i = 0; i < n; i++) {
    cin >> s;
    if (s.find("B") != string::npos) {
      found = true;
      total_count_black += count(s.begin(), s.end(), 'B');
      ri = min(ri, i);
      rj = max(rj, i);
      long long p = 101, q = -1;
      find_first_last_occurence(s, p, q);
      ci = min(ci, p);
      cj = max(cj, q);
    }
  }
  long long max_square_size = max((rj - ri + 1), (cj - ci + 1));
  if (total_count_black == 0) {
    cout << 1 << endl;
  } else {
    long long k = (long long)pow(max_square_size, 2);
    if (max_square_size <= n && max_square_size <= m) {
      cout << (k - total_count_black) << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
