#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= 1000000007;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
  }
  return res;
}
char arr[105][105];
int main() {
  int n, m, order, ans, ymax, ymin, xmax, xmin, cnt;
  cnt = ymax = xmax = 0;
  ymin = xmin = 105;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> arr[i][j];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (arr[i][j] == 'B') {
        cnt++;
        ymax = max(ymax, j);
        ymin = min(ymin, j);
        xmax = max(xmax, i);
        xmin = min(xmin, i);
      }
    }
  }
  order = max(ymax - ymin, xmax - xmin) + 1;
  if (ymin == 105) {
    printf("1\n");
  } else if (order > min(m, n)) {
    printf("-1\n");
  } else {
    ans = order * order - cnt;
    printf("%d\n", ans);
  }
  return 0;
}
