#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int inf = 0x3f3f3f3f;
char maze[105][105];
int row, col;
int main() {
  memset(maze, 0, sizeof(maze));
  scanf("%d %d", &row, &col);
  int uprow = 105, downrow = 0, leftcol = 105, rightcol = 0;
  int cnt = 0;
  for (int i = 1; i <= row; i++) {
    for (int j = 1; j <= col; j++) {
      scanf(" %c", &maze[i][j]);
      if (maze[i][j] == 'B') {
        cnt++;
        uprow = min(uprow, i);
        downrow = max(downrow, i);
        leftcol = min(j, leftcol);
        rightcol = max(rightcol, j);
      }
    }
  }
  if (cnt == 0) {
    printf("1\n");
    return 0;
  }
  if (cnt == 1) {
    printf("0\n");
    return 0;
  }
  if (row != col) {
    int maxd = max(downrow - uprow + 1, rightcol - leftcol + 1);
    if (maxd > min(row, col)) {
      printf("-1\n");
      return 0;
    }
  }
  int ans = 0;
  if (downrow - uprow >= rightcol - leftcol) {
    int len = downrow - uprow + 1;
    if (rightcol - len <= 0) {
      for (int i = uprow; i <= downrow; i++) {
        for (int j = 1; j <= len; j++) {
          if (maze[i][j] == 'W') {
            ans++;
          }
        }
      }
    } else {
      for (int i = uprow; i <= downrow; i++) {
        for (int j = rightcol - len + 1; j <= rightcol; j++) {
          if (maze[i][j] == 'W') {
            ans++;
          }
        }
      }
    }
  } else {
    int len = rightcol - leftcol + 1;
    if (downrow - len <= 0) {
      for (int i = 1; i <= len; i++) {
        for (int j = leftcol; j <= rightcol; j++) {
          if (maze[i][j] == 'W') {
            ans++;
          }
        }
      }
    } else {
      for (int i = downrow - len + 1; i <= downrow; i++) {
        for (int j = leftcol; j <= rightcol; j++) {
          if (maze[i][j] == 'W') {
            ans++;
          }
        }
      }
    }
  }
  printf("%d\n", ans);
}
