#include <bits/stdc++.h>
using namespace std;
int main() {
  char c;
  int n, m, NumB = 0, L, R, T, P;
  cin >> n >> m;
  L = m + 1;
  R = -1;
  T = n + 1;
  P = -1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> c;
      if (c == 'B') {
        ++NumB;
        L = min(L, j);
        R = max(R, j);
        T = min(T, i);
        P = max(P, i);
      }
    }
  }
  if (!NumB) {
    cout << 1;
    return 0;
  }
  if (NumB == n * m) {
    if (n == m) {
      cout << 0;
    } else {
      cout << -1;
    }
    return 0;
  }
  int Size = max(R - L + 1, P - T + 1);
  if (Size > min(n, m)) {
    cout << -1;
  } else
    cout << Size * Size - NumB;
  return 0;
}
