#include <bits/stdc++.h>
using namespace std;
string s[105];
int main() {
  int n, m, cnt = 0, t, tt, temp, dis = 0, ans = 0, len;
  int row = 0, rowup = 2e9, rowdn = -1, col = 0, collft = 2e9, colrt = -1;
  int rowupcol, rowdncol, collftrow, colrtrow;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'B') {
        cnt++;
        if (i < rowup) {
          rowup = i;
          rowupcol = j;
        }
        if (rowdn < i) {
          rowdn = i;
          rowdncol = j;
        }
        if (j < collft) {
          collft = j;
          collftrow = i;
        }
        if (colrt < j) {
          colrt = j;
          colrtrow = i;
        }
      }
    }
  }
  if (!cnt)
    printf("1\n");
  else if (cnt == 1)
    printf("0\n");
  else {
    len = max(rowdn - rowup + 1, colrt - collft + 1);
    if (len > n || len > m)
      printf("-1\n");
    else
      printf("%d\n", (len * len) - cnt);
  }
  return 0;
}
