#include <bits/stdc++.h>
using namespace std;
int mat[101][101];
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m;
  int i, j;
  string s;
  cin >> n >> m;
  for (i = 0; i < n; ++i) {
    cin >> s;
    for (j = 0; j < m; ++j) {
      if (s[j] == 'W')
        mat[i][j] = 0;
      else
        mat[i][j] = 1;
    }
  }
  int left = -1, right = 101, top = -1, bot = 101;
  int bcount = 0;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (mat[i][j] == 1) {
        ++bcount;
        if (left == -1)
          left = j;
        else
          left = min(left, j);
        if (right == 101)
          right = j;
        else
          right = max(right, j);
        if (top == -1)
          top = i;
        else
          top = min(top, i);
        if (bot == 101)
          bot = i;
        else
          bot = max(bot, i);
      }
    }
  }
  int c = 0;
  if (bot == 101 && right == 101 && left == -1 && top == -1)
    c = 1;
  else if (bot == top && right == left)
    c = 0;
  else {
    int hor = bot - top + 1;
    int ver = right - left + 1;
    int side = max(hor, ver);
    if (side > n || side > m)
      c = -1;
    else {
      c = side * side - bcount;
    }
  }
  cout << c << endl;
  ;
  return 0;
}
