#include <bits/stdc++.h>
using namespace std;
int main() {
  char s[400][500];
  long long n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) cin >> s[i][j];
  long long cnt = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (s[i][j] == 'B') cnt++;
    }
  }
  long long ll, lr, rl, rr;
  if (cnt == 0)
    cout << 1 << endl;
  else {
    long long f = 0;
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        if (s[j][i] == 'B') {
          ll = i;
          f = 1;
        }
        if (f) break;
      }
      if (f) break;
    }
    f = 0;
    for (i = m - 1; i >= 0; i--) {
      for (j = 0; j < n; j++) {
        if (s[j][i] == 'B') {
          lr = i;
          f = 1;
        }
        if (f) break;
      }
      if (f) break;
    }
    f = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (s[i][j] == 'B') {
          rl = i;
          f = 1;
        }
        if (f) break;
      }
      if (f) break;
    }
    f = 0;
    for (i = n - 1; i >= 0; i--) {
      for (j = 0; j < m; j++) {
        if (s[i][j] == 'B') {
          rr = i;
          f = 1;
        }
        if (f) break;
      }
      if (f) break;
    }
    long long mxx, mxy;
    mxx = lr - ll + 1;
    mxy = rr - rl + 1;
    if (mxx >= mxy) {
      if (mxx <= n) {
        cout << mxx * mxx - cnt << endl;
      } else
        cout << -1 << endl;
    } else {
      if (mxy <= m) {
        cout << mxy * mxy - cnt << endl;
      } else
        cout << -1 << endl;
    }
  }
}
