#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int t = n - 1, b = 0, l = m - 1, r = 0;
  char c;
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == 'B') {
        count++;
        t = min(t, i);
        b = max(b, i);
        l = min(l, j);
        r = max(r, j);
      }
    }
  }
  if (count == 0) {
    cout << "1\n";
    return 0;
  }
  int leny = b - t + 1;
  int lenx = r - l + 1;
  if (lenx > n || leny > m) {
    cout << "-1\n";
    return 0;
  }
  int len;
  len = max(lenx, leny);
  cout << len * len - count << endl;
  return 0;
}
