#include <bits/stdc++.h>
int main(void) {
  int n, m;
  char g[1000][1000];
  int min_row = 1000, max_row = -1;
  int min_col = 1000, max_col = -1;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf(" %c", &g[i][j]);
      if (g[i][j] == 'B') {
        min_row = std::min(min_row, i);
        max_row = std::max(max_row, i);
        min_col = std::min(min_col, j);
        max_col = std::max(max_col, j);
      }
    }
  }
  if (max_row == -1) {
    puts("1");
  } else {
    int missing_from_rect = 0;
    for (int i = min_row; i <= max_row; i++) {
      for (int j = min_col; j <= max_col; j++) {
        missing_from_rect += (g[i][j] == 'W');
      }
    }
    const int n_rows = (max_row - min_row + 1);
    const int n_cols = (max_col - min_col + 1);
    bool feasible = true;
    int additional = 0;
    if (n_rows > n_cols) {
      const int needed = n_rows - n_cols;
      if (n_cols + needed > m) {
        feasible = false;
      } else {
        additional = needed * n_rows;
      }
    } else if (n_rows < n_cols) {
      const int needed = n_cols - n_rows;
      if (n_rows + needed > n) {
        feasible = false;
      } else {
        additional = needed * n_cols;
      }
    }
    if (feasible) {
      printf("%d\n", missing_from_rect + additional);
    } else {
      puts("-1");
    }
  }
  return 0;
}
