#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, maxr = INT_MIN, maxc = INT_MIN, minr = INT_MAX, minc = INT_MAX,
            c = 0;
  cin >> n >> m;
  char a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'B') {
        c++;
      }
    }
  }
  if (c == 0) {
    cout << 1;
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 'B') {
          minr = min(i, minr);
          minc = min(j, minc);
          maxr = max(i, maxr);
          maxc = max(j, maxc);
        }
      }
    }
    if (max(maxr - minr + 1, maxc - minc + 1) > m ||
        max(maxr - minr + 1, maxc - minc + 1) > n) {
      cout << -1;
    } else {
      c = 0;
      int R = maxr - max(maxr - minr, maxc - minc);
      int C = maxc - max(maxr - minr, maxc - minc);
      if (R < 0) {
        R = 0;
      }
      if (C < 0) {
        C = 0;
      }
      for (int i = R; i <= R + max(maxr - minr, maxc - minc); i++) {
        for (int j = C; j <= C + max(maxr - minr, maxc - minc); j++) {
          if (a[i][j] == 'W') {
            c++;
          }
        }
      }
      cout << c;
    }
  }
  return 0;
}
