#include <bits/stdc++.h>
using namespace std;
string mapp[105];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> mapp[i];
  int sx = 0x3f3f3f3f, sy = 0x3f3f3f3f, ex = 0, ey = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (mapp[i][j] == 'B') {
        sx = min(sx, i);
        sy = min(sy, j);
        ex = max(ex, i);
        ey = max(ey, j);
        cnt++;
      }
    }
  }
  if (cnt == 0) {
    cout << 1;
    return 0;
  }
  int len = max(ex - sx + 1, ey - sy + 1);
  if (len > n || len > m) {
    cout << -1;
    return 0;
  }
  cout << len * len - cnt;
  return 0;
}
