#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char a[n + 1][m + 1];
  int tot = 0, sr = INT_MAX, er = INT_MIN, sc = INT_MAX, ec = INT_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'B') {
        sr = min(sr, i);
        er = max(er, i);
        sc = min(sc, j);
        ec = max(ec, j);
        tot++;
      }
    }
  }
  if (sr == INT_MAX) {
    cout << "1\n";
    return 0;
  }
  int p = max(er - sr + 1, ec - sc + 1);
  if (p <= n && p <= m)
    printf("%d\n", p * p - tot > 10000 ? 1 : (p * p - tot));
  else
    cout << "-1"
         << "\n";
  return 0;
}
