#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  static int page[111][111]{};
  int n, m, a = 1e9, b{}, c = 1e9, d{};
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == 'B') {
        page[i][j] = 1;
      }
    }
  }
  int s{};
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (page[i][j] == 1) {
        s++;
        a = min(a, i);
        b = max(b, i);
        c = min(c, j);
        d = max(d, j);
      }
    }
  }
  if (a == 1e9) return cout << 1, 0;
  int mx = max(b - a + 1, d - c + 1);
  if (mx > n || mx > m) return cout << -1, 0;
  cout << mx * mx - s;
  return 0;
}
