#include <bits/stdc++.h>
using namespace std;
char G[105][105];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", G[i]);
  }
  int lx = 105;
  int rx = -1;
  int ly = 105;
  int ry = -1;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (G[i][j] == 'B') {
        cnt++;
        lx = min(lx, i);
        rx = max(rx, i);
        ly = min(ly, j + 1);
        ry = max(ry, j + 1);
      }
    }
  }
  int a = (rx - lx + 1);
  int b = (ry - ly + 1);
  int len = max(a, b);
  int flag = 0;
  if (len > n || len > m) flag = 1;
  if (flag)
    printf("-1\n");
  else {
    if (cnt)
      printf("%d\n", len * len - cnt);
    else
      printf("1\n");
  }
  return 0;
}
