#include <bits/stdc++.h>
int G[105][105];
int n, m;
int ask(int, int, int, int);
int main() {
  int x1, y1, x2, y2;
  while (scanf("%d%d", &n, &m) != EOF) {
    char ch;
    x1 = -1;
    y1 = -1;
    x2 = -1;
    y2 = -1;
    getchar();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%c", &ch);
        if (ch == 'W')
          G[i][j] = 0;
        else {
          if (x1 == -1 || x1 > i) x1 = i;
          if (x2 == -1 || x2 < i) x2 = i;
          if (y1 == -1 || y1 > j) y1 = j;
          if (y2 == -1 || y2 < j) y2 = j;
          G[i][j] = 1;
        }
      }
      getchar();
    }
    printf("%d\n", ask(x1, y1, x2, y2));
  }
  return 0;
}
int ask(int x1, int y1, int x2, int y2) {
  int w = y2 - y1 + 1, h = x2 - x1 + 1;
  int ans = 0;
  for (int i = x1; i <= x2; i++) {
    for (int j = y1; j <= y2; j++) {
      ans += G[i][j];
    }
  }
  if (w == h) {
    return w * h - ans;
  }
  if (w > h) {
    if (n >= w) {
      return (w - h) * w + w * h - ans;
    } else
      return -1;
  } else {
    if (m >= h) {
      return (h - w) * h + w * h - ans;
    } else
      return -1;
  }
}
