#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j;
  char arr[101][101];
  int pos, first[101], last[101], top[101], bottom[101], minlast = 100000;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    pos = -1;
    for (j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'B') {
        if (pos == -1) {
          first[i] = j;
          pos = j;
        } else
          pos = j;
      }
    }
    if (pos == -1) {
      first[i] = -1;
      last[i] = -1;
    } else
      last[i] = pos;
  }
  for (j = 0; j < m; j++) {
    pos = -1;
    for (i = 0; i < n; i++) {
      if (arr[i][j] == 'B') {
        if (pos == -1) {
          top[j] = i;
          pos = i;
        } else
          pos = i;
      }
    }
    if (pos == -1) {
      top[j] = -1;
      bottom[j] = -1;
    } else
      bottom[j] = pos;
  }
  int diffrow = 0, diffcol = 0, minfirst = 1000000, maxlast = -1,
      mintop = 1000000, maxbottom = -1;
  for (i = 0; i < n; i++) {
    if (first[i] != -1) minfirst = min(minfirst, first[i]);
  }
  for (i = 0; i < n; i++) {
    if (last[i] != -1) maxlast = max(maxlast, last[i]);
  }
  if (minfirst != -1 && maxlast != -1) {
    diffrow = maxlast - minfirst + 1;
  }
  for (i = 0; i < m; i++) {
    if (top[i] != -1) mintop = min(mintop, top[i]);
  }
  for (i = 0; i < n; i++) {
    if (bottom[i] != -1) maxbottom = max(maxbottom, bottom[i]);
  }
  if (mintop != -1 && maxbottom != -1) diffcol = maxbottom - mintop + 1;
  if (mintop == -1 && minfirst == -1 ||
      mintop == 1000000 && minfirst == 1000000) {
    cout << "1";
    return 0;
  }
  if (diffcol > m || diffrow > n) {
    cout << "-1";
    return 0;
  }
  if (diffcol > diffrow) {
    i = mintop;
    if (m - minfirst < diffcol)
      j = m - diffcol;
    else
      j = minfirst;
  } else if (diffcol < diffrow) {
    j = minfirst;
    if (n - mintop < diffrow)
      i = n - diffrow;
    else
      i = mintop;
  } else {
    i = mintop;
    j = minfirst;
  }
  int ctr = 0, ctr1 = 0, ans = 0;
  int tempj;
  while (ctr < max(diffrow, diffcol)) {
    tempj = j;
    ctr1 = 0;
    while (ctr1 < max(diffrow, diffcol)) {
      ctr1++;
      if (arr[i][tempj++] != 'B') ans++;
    }
    i++;
    ctr++;
  }
  cout << ans << endl;
  return 0;
}
