#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 2;
using namespace std;
void solve();
void fastIO() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  fastIO();
  solve();
}
void solve() {
  int n, m;
  cin >> n >> m;
  char a[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  int rs = 200, re = -1, cs = 200, ce = -1;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'B') {
        if (i < rs) rs = i;
        if (i > re) re = i;
        if (j < cs) cs = j;
        if (j > ce) ce = j;
        flag = false;
      }
    }
  }
  if (flag) {
    cout << 1;
    return;
  }
  int x = re - rs + 1;
  int y = ce - cs + 1;
  int mx = max(x, y);
  if (n < mx || m < mx) {
    cout << -1;
    return;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'B') ans++;
    }
  }
  cout << (mx * mx - ans);
}
