#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int N, M, U = 110, D = 0, L = 110, R = 0, cnt;
char s[maxn][maxn];
inline int gi() {
  char ch;
  int ret = 0, f = 1;
  do ch = getchar();
  while (!(ch >= '0' && ch <= '9') && ch != '-');
  if (ch == '-') f = -1, ch = getchar();
  do ret = ret * 10 + ch - '0', ch = getchar();
  while (ch >= '0' && ch <= '9');
  return ret * f;
}
int main() {
  N = gi();
  M = gi();
  for (int i = 1; i <= N; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= M; ++j)
      if (s[i][j] == 'B') {
        ++cnt;
        U = min(i, U);
        D = max(i, D);
        L = min(j, L);
        R = max(j, R);
      }
  int W = max(R - L + 1, D - U + 1);
  if (!cnt)
    puts("1");
  else if (W > N || W > M)
    puts("-1");
  else
    printf("%d\n", W * W - cnt);
  return 0;
}
