#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> c(n);
  for (int i = 0; i < n; i++) c[i].resize(m);
  int u = 1000, l = 1000, r = -1, d = -1, ok = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'B') {
        u = min(u, i);
        l = min(l, j);
        r = max(r, j);
        d = max(d, i);
        ok = 0;
      }
    }
  }
  if (ok) return cout << 1 << endl, 0;
  int s = max(d - u + 1, r - l + 1);
  if (s > m || s > n) return cout << -1 << endl, 0;
  int b = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == 'B') b++;
    }
  }
  cout << s * s - b << endl;
  return 0;
}
