#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int l = 1000, r = -1, t = 1000, b = -1;
  int num = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char a;
      cin >> a;
      if (a == 'B') {
        l = min(l, j);
        r = max(r, j);
        t = min(t, i);
        b = max(b, i);
        num++;
      }
    }
  }
  if (!num) {
    cout << 1 << endl;
    return 0;
  }
  int w = r - l + 1, h = b - t + 1;
  int side = max(h, w);
  if (side > n || side > m) {
    cout << -1 << endl;
    return 0;
  }
  cout << side * side - num << endl;
  return 0;
}
