#include <bits/stdc++.h>
using namespace std;
const char BLACK = 'B';
const char WHITE = 'W';
int main() {
  size_t n, m;
  cin >> n >> m;
  int count = 0;
  vector<vector<char>> paper(n, vector<char>(m, WHITE));
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < m; j++) cin >> paper[i][j];
  }
  int min_x = 1000, max_x = -1, min_y = 1000, max_y = -1;
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < m; j++)
      if (paper[i][j] == BLACK) {
        count++;
        int x = j, y = i;
        if (min_x > x) min_x = x;
        if (max_x < x) max_x = x;
        if (min_y > y) min_y = y;
        if (max_y < y) max_y = y;
      }
  }
  if (count == 0 || count == 1) {
    cout << 1 - count << endl;
    return 0;
  }
  int dim_x = max_x - min_x + 1;
  int dim_y = max_y - min_y + 1;
  int missing = 0;
  for (int i = min_y; i <= max_y; i++) {
    for (int j = min_x; j <= max_x; j++) {
      if (paper[i][j] == WHITE) missing++;
    }
  }
  int additional = 0;
  if (dim_x > dim_y) {
    int diff = dim_x - dim_y;
    int have = n - dim_y;
    if (diff > have) {
      cout << -1 << endl;
      return 0;
    }
    additional += diff * dim_x;
  } else if (dim_x < dim_y) {
    int diff = dim_y - dim_x;
    int have = m - dim_x;
    if (diff > have) {
      cout << -1 << endl;
      return 0;
    }
    additional += diff * dim_y;
  } else {
  }
  cout << missing + additional << endl;
  return 0;
}
