#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int minRow = n + 1;
  int maxRow = 0;
  int minCol = m + 1;
  int maxCol = 0;
  int cntBlack = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      if (c == 'B') {
        ++cntBlack;
        minRow = min(minRow, i);
        maxRow = max(maxRow, i);
        minCol = min(minCol, j);
        maxCol = max(maxCol, j);
      }
    }
  }
  int side = max(maxRow - minRow, maxCol - minCol) + 1;
  if (cntBlack == 0) {
    cout << 1;
  } else if (side > n || side > m) {
    cout << -1;
  } else {
    cout << side * side - cntBlack;
  }
  return 0;
}
