#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, m;
char mp[maxn][maxn];
int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
    int l = m, r = 1, u = n, d = 1;
    bool has = false;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (mp[i][j] == 'B') {
          l = min(l, j);
          r = max(r, j);
          u = min(u, i);
          d = max(d, i);
          has = true;
        }
    if (!has) {
      puts("1");
      continue;
    }
    bool f = true;
    int w = r - l + 1, h = d - u + 1;
    if (w < h) {
      int tmp = h - w;
      if (m - r >= tmp)
        r += tmp;
      else {
        tmp -= (m - r);
        r = m;
        if (l - 1 >= tmp)
          l -= tmp;
        else
          f = false;
      }
    } else if (w > h) {
      int tmp = w - h;
      if (n - d >= tmp)
        d += tmp;
      else {
        tmp -= (n - d);
        d = n;
        if (u - 1 >= tmp)
          u -= tmp;
        else
          f = false;
      }
    }
    if (!f)
      puts("-1");
    else {
      int ans = 0;
      for (int i = l; i <= r; i++)
        for (int j = u; j <= d; j++)
          if (mp[j][i] == 'W') ans++;
      printf("%d\n", ans);
    }
  }
  return 0;
}
