#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << '{';
  for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) {
    os << vec[i];
    if (i + 1 != ((int)(vec).size())) os << ',';
  }
  os << '}';
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &par) {
  os << '(' << par.first << ',' << par.second << ')';
  return os;
}
int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
template <class T>
T sqr(T x) {
  return x * x;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  string s[100];
  for (int i = (0), __i = (n); i < __i; i++) cin >> s[i];
  int l = m, r = -1, b = -1, t = n;
  for (int i = (0), __i = (n); i < __i; i++) {
    for (int j = (0), __j = (m); j < __j; j++) {
      if (s[i][j] == 'B') {
        t = i;
        i = n;
        break;
      }
    }
  }
  for (int i = (0), __i = (n); i < __i; i++) {
    for (int j = (0), __j = (m); j < __j; j++) {
      if (s[n - i - 1][j] == 'B') {
        b = n - i - 1;
        i = n;
        break;
      }
    }
  }
  for (int j = (0), __j = (m); j < __j; j++) {
    for (int i = (0), __i = (n); i < __i; i++) {
      if (s[i][j] == 'B') {
        l = j;
        j = m;
        break;
      }
    }
  }
  for (int j = (0), __j = (m); j < __j; j++) {
    for (int i = (0), __i = (n); i < __i; i++) {
      if (s[i][m - j - 1] == 'B') {
        r = m - j - 1;
        j = m;
        break;
      }
    }
  }
  int black = 0;
  for (int j = (0), __j = (m); j < __j; j++) {
    for (int i = (0), __i = (n); i < __i; i++) {
      if (s[i][j] == 'B') {
        black++;
      }
    }
  }
  if (l > r)
    cout << 1;
  else {
    if (r - l > b - t) {
      if (n < r - l + 1)
        cout << -1;
      else
        cout << (r - l + 1) * (r - l + 1) - black;
    } else {
      if (m < b - t + 1)
        cout << -1;
      else
        cout << (b - t + 1) * (b - t + 1) - black;
    }
  }
  return 0;
}
