#include <bits/stdc++.h>
using namespace std;
template <class T>
inline int chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline int chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const int maxn = 100 + 15;
int n, m, s[maxn][maxn];
char a[maxn][maxn];
int query(int x, int y, int u, int v) {
  return s[u][v] - s[u][y - 1] - s[x - 1][v] + s[x - 1][y - 1];
}
bool inside(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (1), _b = (n); i <= _b; ++i)
    for (int j = (1), _b = (m); j <= _b; ++j) {
      cin >> a[i][j];
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i][j] == 'W');
    }
  int mnr = INF, mxr = -INF;
  int mnc = INF, mxc = -INF;
  bool allwhite = true;
  for (int i = (1), _b = (n); i <= _b; ++i)
    for (int j = (1), _b = (m); j <= _b; ++j)
      if (a[i][j] == 'B') {
        chkmin(mnr, i);
        chkmax(mxr, i);
        chkmin(mnc, j);
        chkmax(mxc, j);
        allwhite = false;
      }
  int len = max(mxr - mnr + 1, mxc - mnc + 1);
  if (allwhite) return cout << 1, 0;
  int ans = INF;
  for (int i = (len), _b = (n); i <= _b; ++i)
    for (int j = (len), _b = (m); j <= _b; ++j)
      if (query(i - len + 1, j - len + 1, i, j) >= 0) {
        chkmin(ans, query(i - len + 1, j - len + 1, i, j));
      }
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
  return 0;
}
