#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& a) {
  out << "";
  for (auto it = a.begin(); it != a.end(); it++) {
    if (it != a.begin()) out << " ";
    out << *it;
  }
  out << "";
  return out;
}
template <class A, class B>
istream& operator>>(istream& in, pair<A, B>& a) {
  in >> a.first >> a.second;
  return in;
}
template <class A>
istream& operator>>(istream& in, vector<A>& a) {
  for (A& i : a) in >> i;
  return in;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long u = 1000, d = -1000, l = 1000, r = -1000, black = 0;
  long long R, C;
  cin >> R >> C;
  vector<vector<char>> v(R, vector<char>(C));
  for (long long i = 0; i < R; i++)
    for (long long j = 0; j < C; j++) {
      cin >> v[i][j];
      if (v[i][j] == 'B') black++;
    }
  for (long long i = 0; i < C; i++) {
    for (long long j = 0; j < R; j++) {
      if (v[j][i] == 'B') {
        u = min(j, u);
        break;
      }
    }
  }
  for (long long i = 0; i < C; i++) {
    for (long long j = R - 1; j >= 0; j--) {
      if (v[j][i] == 'B') {
        d = max(j, d);
        break;
      }
    }
  }
  for (long long i = 0; i < R; i++) {
    for (long long j = 0; j < C; j++) {
      if (v[i][j] == 'B') {
        l = min(j, l);
        break;
      }
    }
  }
  for (long long i = 0; i < R; i++) {
    for (long long j = C - 1; j >= 0; j--) {
      if (v[i][j] == 'B') {
        r = max(j, r);
        break;
      }
    }
  }
  long long side = max(d - u + 1, r - l + 1);
  long long sqrr = side * side;
  long long def = min(R, C);
  if (sqrr <= def * def)
    cout << sqrr - black;
  else if (black != 0)
    cout << -1;
  else
    cout << 1;
}
