#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m;
char grid[N][N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (int)(n); ++i) scanf("%s", grid[i] + 1);
  int mnX = 1000000000ll, mnY = 1000000000ll, mxX = 0, mxY = 0, numOfB = 0;
  for (int i = (1); i <= (int)(n); ++i)
    for (int j = (1); j <= (int)(m); ++j)
      if (grid[i][j] == 'B') {
        mnX = min(mnX, i);
        mxX = max(mxX, i);
        mnY = min(mnY, j);
        mxY = max(mxY, j);
        ++numOfB;
      }
  int side = max((mxX - mnX + 1), (mxY - mnY + 1));
  if (numOfB) {
    if (side > m || side > n)
      printf("-1\n");
    else
      printf("%d\n", side * side - numOfB);
  } else {
    printf("1\n");
  }
  return 0;
}
