#include <bits/stdc++.h>
using namespace std;
bool a[105][105];
int main() {
  int n, m, i, j, mini = 100, maxi = 1, minj = 100, maxj = 1, ss = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (j = 1; j <= m; j++) {
      if (s[j - 1] == 'W')
        a[i][j] = false;
      else
        a[i][j] = true, ss++;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (a[i][j]) {
        if (i < mini) mini = i;
        if (i > maxi) maxi = i;
        if (j < minj) minj = j;
        if (j > maxj) maxj = j;
      }
    }
  }
  int t = max(maxi - mini + 1, maxj - minj + 1);
  if (t > n || t > m)
    puts("-1");
  else if (ss == 0)
    puts("1");
  else
    printf("%d\n", t * t - ss);
  return 0;
}
