#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int solve() {
  int n, m;
  cin >> n >> m;
  vector<string> v;
  string buf;
  for (int i = 0; i < n; ++i) {
    cin >> buf;
    v.push_back(buf);
  }
  bool notStarted;
  bool notStartedCol;
  int startPoint, endPoint, startPointCol, endPointCol;
  int maxLengthString = 0;
  int maxLengthCol = 0;
  int counterB = 0;
  endPoint = -1;
  startPoint = 2147483647;
  for (int i = 0; i < n; ++i) {
    notStarted = true;
    for (int j = 0; j < m; ++j) {
      if ((v[i][j] == 'B') && (notStarted)) {
        if (j < startPoint) startPoint = j;
        if (j > endPoint) endPoint = j;
        notStarted = false;
      } else if ((v[i][j] == 'B') && (!notStarted)) {
        if (j > endPoint) endPoint = j;
      }
      if (v[i][j] == 'B') counterB++;
    }
    if (startPoint != -1)
      if (endPoint - startPoint + 1 > maxLengthString)
        maxLengthString = endPoint - startPoint + 1;
  }
  endPointCol = -1;
  startPointCol = 2147483647;
  for (int i = 0; i < m; ++i) {
    notStartedCol = true;
    for (int j = 0; j < n; ++j) {
      if ((v[j][i] == 'B') && (notStartedCol)) {
        if (j < startPointCol) startPointCol = j;
        if (j > endPointCol) endPointCol = j;
        notStartedCol = false;
      } else if ((v[j][i] == 'B') && (!notStartedCol)) {
        if (j > endPointCol) endPointCol = j;
      }
    }
    if (startPointCol != -1)
      if (endPointCol - startPointCol + 1 > maxLengthCol)
        maxLengthCol = endPointCol - startPointCol + 1;
  }
  if (maxLengthCol < maxLengthString)
    maxLengthCol = maxLengthString;
  else
    maxLengthString = maxLengthCol;
  if ((maxLengthCol > n) || (maxLengthCol > m)) {
    cout << "-1";
    return 0;
  }
  if (counterB == 0) {
    cout << "1";
    return 0;
  }
  cout << maxLengthCol * maxLengthString - counterB;
  return 0;
}
int main() {
  ofstream coutf;
  coutf.open("output.txt");
  ifstream cinf;
  cinf.open("input.txt");
  solve();
  cinf.close();
  coutf.close();
  return 0;
}
