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