#include <bits/stdc++.h>
using namespace std;
char s[100][100];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  int flag = 0, mark = 0;
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'B') flag = 1;
      if (s[i][j] == 'W') mark = 1;
    }
  int up = -1, dn, l = -1, r;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'B') {
        if (up == -1) up = i;
        dn = i;
      }
    }
  for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++) {
      if (s[i][j] == 'B') {
        if (l == -1) l = j;
        r = j;
      }
    }
  if (dn - up + 1 > m || r - l + 1 > n)
    ans = -1;
  else {
    if (dn - up + 1 > r - l + 1) {
      int x;
      for (int j = 0; j < m; j++) {
        x = j;
        if (x + dn - up >= r && l >= x) break;
      }
      for (int i = up; i <= dn; i++)
        for (int j = x; j <= x + dn - up; j++) {
          if (s[i][j] == 'W') ans++;
        }
    } else {
      int y;
      for (int i = 0; i < n; i++) {
        y = i;
        if (up >= y && y + r - l >= dn) break;
      }
      for (int i = y; i <= y + r - l; i++)
        for (int j = l; j <= r; j++)
          if (s[i][j] == 'W') ans++;
    }
  }
  if (flag == 0)
    printf("1\n");
  else if (mark == 0 && n != m)
    printf("-1\n");
  else if (mark == 0 && n == m)
    printf("0\n");
  else
    printf("%d\n", ans);
  return 0;
}
