#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
const int inf = 1034567891;
const long long LL_INF = 1234567890123456789ll;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T>
T GCD(T a, T b) {
  long long t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T>
string toString(T a) {
  return to_string(a);
}
template <typename T>
void toInt(string s, T& x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, long long b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  char arr[n][m];
  int x1, y1, x2, y2;
  x1 = -1;
  y1 = inf;
  y2 = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'B') {
        if (x1 == -1) {
          x1 = i;
        }
        y1 = min(y1, j);
        y2 = max(y2, j);
        x2 = i;
      }
    }
  }
  if (x1 == -1) {
    cout << 1 << '\n';
    return 0;
  }
  int ans = 0;
  for (int i = x1; i <= x2; i++) {
    for (int j = y1; j <= y2; j++) {
      ans += (arr[i][j] == 'W');
      arr[i][j] = 'B';
    }
  }
  int l = y2 - y1 + 1;
  int b = x2 - x1 + 1;
  if (l == b) {
    cout << ans << '\n';
    return 0;
  } else {
    if (b > l) {
      int d = b - l;
      y1--;
      y2++;
      while (d--) {
        int c1 = 0, c2 = 0;
        if (y1 >= 0 && y2 < m) {
          for (int i = x1; i <= x2; i++) {
            c1 += (arr[i][y1] == 'W');
            c2 += (arr[i][y2] == 'W');
          }
          if (c1 <= c2) {
            ans += c1;
            y1--;
          } else {
            ans += c2;
            y2++;
          }
          l++;
        } else if (y1 >= 0) {
          for (int i = x1; i <= x2; i++) {
            c1 += (arr[i][y1] == 'W');
          }
          ans += c1;
          y1--;
          l++;
        } else if (y2 < m) {
          for (int i = x1; i <= x2; i++) {
            c2 += (arr[i][y2] == 'W');
          }
          ans += c2;
          y2++;
          l++;
        }
      }
    } else {
      int d = l - b;
      x1--;
      x2++;
      while (d--) {
        int c1 = 0, c2 = 0;
        if (x1 >= 0 && x2 < n) {
          for (int i = y1; i <= y2; i++) {
            c1 += (arr[x1][i] == 'W');
            c2 += (arr[x2][i] == 'W');
          }
          if (c1 <= c2) {
            ans += c1;
            x1--;
          } else {
            ans += c2;
            x2++;
          }
          b++;
        } else if (x1 >= 0) {
          for (int i = y1; i <= y2; i++) {
            c1 += (arr[x1][i] == 'W');
          }
          ans += c1;
          x1--;
          b++;
        } else if (x2 < n) {
          for (int i = y1; i <= y2; i++) {
            c2 += (arr[x2][i] == 'W');
          }
          ans += c2;
          x2++;
          b++;
        }
      }
    }
  }
  if (l != b) ans = -1;
  cout << ans << '\n';
  return 0;
}
