#include <bits/stdc++.h>
using namespace std;
char c[500][500];
int main() {
  int n, m, ans, mni = 500, mxi = 0, mnj = 500, mxj = 0, cnt = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'B') {
        cnt++;
        mnj = min(mnj, j), mni = min(mni, i);
        mxj = max(mxj, j), mxi = max(mxi, i);
      }
    }
  if (mxi - mni + 1 > m || mxj - mnj + 1 > n)
    return cout << -1, 0;
  else if (!cnt)
    return cout << 1, 0;
  ans = pow(max(mxi - mni + 1, mxj - mnj + 1), 2) - cnt;
  cout << ans;
  return 0;
}
