#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, ans = 0, toAdd = 0;
  cin >> n >> m;
  int a[n][m];
  char c;
  int left = 101, right = -1, up = 101, down = -1;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> c;
      if (c == 'B')
        a[i][j] = 1;
      else
        a[i][j] = 0;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        if (left > j) left = j;
        if (right < j) right = j;
        if (up > i) up = i;
        if (down < i) down = i;
      }
    }
  }
  int x = right - left;
  int y = down - up;
  if (x > y) {
    if (x <= n - 1) {
    } else {
      cout << -1 << endl;
      return 0;
    }
    toAdd = x - y;
  }
  if (x < y) {
    if (y <= m - 1) {
    } else {
      cout << -1 << endl;
      return 0;
    }
    toAdd = y - x;
  }
  if (left == 101) {
    cout << 1 << endl;
    return 0;
  }
  for (i = up; i <= down; i++) {
    for (j = left; j <= right; j++) {
      if (a[i][j] == 0) ans++;
    }
  }
  ans += toAdd * (max(x, y) + 1);
  cout << ans << endl;
}
