#include <bits/stdc++.h>
using namespace std;
int main() {
  int arr[100][100], n, m, l = 100, r = -1, t = 100, d = -1, s = 1, t1 = 0, t2;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string st1;
    cin >> st1;
    for (int j = 0; j < m; j++) {
      if (st1[j] == 'W')
        arr[i][j] = 0;
      else {
        t1++;
        arr[i][j] = 1;
        if (i > d) d = i;
        if (i < t) t = i;
        if (j < l) l = j;
        if (j > r) r = j;
      }
    }
  }
  if (t1 < 1) {
    cout << 1;
    return 0;
  }
  if (r - l > d - t) {
    s = r - l + 1;
    t2 = s - (d - t + 1);
    if (t + n - 1 - d >= t2) {
      cout << s * s - t1;
      return 0;
    } else {
      cout << -1;
      return 0;
    }
  } else {
    s = d - t + 1;
    t2 = s - (r - l + 1);
    if (l + m - 1 - r >= t2) {
      cout << s * s - t1;
      return 0;
    } else {
      cout << -1;
      return 0;
    }
  }
  return 0;
}
