#include <bits/stdc++.h>
using namespace std;
const int N = 111;
char s[N][N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s[i]);
  }
  int l = N, r = -1, t = N, b = -1, all_black = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'B') {
        l = min(l, j);
        r = max(r, j);
        t = min(t, i);
        b = max(b, i);
        ++all_black;
      }
    }
  }
  int dx = abs(l - r), dy = abs(t - b);
  if (dy < dx) {
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < m; ++j) {
        swap(s[i][j], s[j][i]);
      }
    }
    swap(n, m);
    swap(dx, dy);
    swap(l, t);
    swap(r, b);
  }
  int side = max(dx, dy) + 1, res = all_black == 0 ? 1 : N * N;
  for (int k = 0; k + side <= m; ++k) {
    int black = 0, white = 0;
    for (int i = t; i <= b; ++i) {
      for (int j = k; j < k + side; ++j) {
        black += s[i][j] == 'B';
        white += s[i][j] == 'W';
      }
    }
    if (black == all_black) {
      res = min(res, white);
    }
  }
  printf("%d\n", res == N * N ? -1 : res);
}
