#include <bits/stdc++.h>
using namespace std;
int dirx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int diry[8] = {1, -1, 0, 0, 1, -1, 1, -1};
string str[105];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) cin >> str[i];
  int mnr = 1e9 + 5, mnc = 1e9 + 5, mxr = -1e9 + 5, mxc = -1e9 + 5, cntb = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (str[i][j] == 'B') {
        cntb++;
        mnr = min(mnr, i);
        mnc = min(mnc, j);
        mxr = max(mxr, i);
        mxc = max(mxc, j);
      }
    }
  }
  int rr = (mxr - mnr) + 1;
  int cc = (mxc - mnc) + 1;
  int needed = max(rr, cc);
  if (cntb == 0)
    printf("1\n");
  else if (needed > n || needed > m)
    printf("-1\n");
  else
    printf("%d\n", (needed * needed) - cntb);
  return 0;
}
