#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char grid[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> grid[i][j];
  int leftMostB = -1;
  int topMostB = -1;
  int rightMostB = m;
  int bottomMostB = n;
  int bCount = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (grid[i][j] == 'B') {
        bCount++;
        if (i > topMostB) topMostB = i;
        if (i < bottomMostB) bottomMostB = i;
        if (j > leftMostB) leftMostB = j;
        if (j < rightMostB) rightMostB = j;
      }
  if (bCount == 0)
    cout << 1 << endl;
  else if (bCount == 1)
    cout << 0 << endl;
  else {
    int squareSideLength =
        max(topMostB - bottomMostB + 1, leftMostB - rightMostB + 1);
    if (squareSideLength > m || squareSideLength > n)
      cout << -1 << endl;
    else
      cout << squareSideLength * squareSideLength - bCount << endl;
  }
  return 0;
}
