#include <bits/stdc++.h>
using namespace std;
const int MaxN = 104;
char mapa[MaxN][MaxN];
bool ver[MaxN];
bool hor[MaxN];
int main() {
  int N, M, i, j, p, qk = 0;
  cin >> N >> M;
  for (i = 0; i < N; i++) {
    for (j = 0; j < M; j++) {
      cin >> mapa[i][j];
    }
  }
  int tot = 0;
  for (i = 0; i < N; i++) {
    for (j = 0; j < M; j++) {
      if (mapa[i][j] == 'B') {
        tot++;
      }
    }
  }
  if (tot == 0) {
    cout << "1";
    return 0;
  }
  for (i = 0; i < N; i++) {
    for (j = 0; j < M; j++) {
      if (mapa[i][j] == 'B') {
        hor[j] = 1;
      }
    }
  }
  for (j = 0; j < M; j++) {
    for (i = 0; i < N; i++) {
      if (mapa[i][j] == 'B') {
        ver[i] = 1;
      }
    }
  }
  int maxi = 0, in = 0, fin, longi;
  for (i = 0; i < N; i++) {
    if (ver[i] == 1) {
      in = i;
      break;
    }
  }
  for (i = N - 1; i >= 0; i--) {
    if (ver[i] == 1) {
      fin = i;
      break;
    }
  }
  longi = fin - in + 1;
  maxi = max(maxi, longi);
  for (i = 0; i < M; i++) {
    if (hor[i] == 1) {
      in = i;
      break;
    }
  }
  for (i = M; i >= 0; i--) {
    if (hor[i] == 1) {
      fin = i;
      break;
    }
  }
  maxi = max(longi, (fin - in + 1));
  if (maxi * maxi > N * M) {
    cout << "-1";
    return 0;
  }
  if (maxi > N || maxi > M) {
    cout << "-1";
    return 0;
  }
  int res = 0;
  cout << (maxi * maxi) - tot;
}
