#include <bits/stdc++.h>
using namespace std;
const int prime = 999983;
const int INF = 0x7FFFFFFF;
const long long INFF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const long long mod = 1e9 + 7;
long long qpow(long long a, long long b) {
  long long s = 1;
  while (b > 0) {
    if (b & 1) s = s * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1};
const int maxn = 2000 + 10;
char ar[maxn][maxn];
int u[maxn], d[maxn], l[maxn], r[maxn];
int x[maxn], y[maxn];
int A[maxn][maxn];
int main(void) {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) scanf("%s", ar[i] + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (ar[i][j] == 'B') {
        l[i] = j;
        break;
      }
    }
    for (int j = n; j >= 1; --j) {
      if (ar[i][j] == 'B') {
        r[i] = j;
        break;
      }
    }
    if (l[i] == r[i] && l[i] == 0) x[i] = 1;
    x[i] += x[i - 1];
  }
  for (int j = 1; j <= n; ++j) {
    for (int i = 1; i <= n; ++i) {
      if (ar[i][j] == 'B') {
        u[j] = i;
        break;
      }
    }
    for (int i = n; i >= 1; --i) {
      if (ar[i][j] == 'B') {
        d[j] = i;
        break;
      }
    }
    if (u[j] == 0 && d[j] == 0) y[j] = 1;
    y[j] += y[j - 1];
  }
  for (int i = 1; i <= n; ++i) {
    bool tmp[maxn];
    int t = 0;
    for (int j = 1; j <= n; ++j) {
      tmp[j] = 0;
      if (j > k && tmp[j - k]) t--;
      if ((i - k + 1 <= u[j] && d[j] <= i) || y[j] - y[j - 1] == 1)
        t++, tmp[j] = 1;
      A[i][j] += t;
    }
  }
  for (int j = 1; j <= n; ++j) {
    bool tmp[maxn];
    int t = 0;
    for (int i = 1; i <= n; ++i) {
      tmp[i] = 0;
      if (i > k && tmp[i - k]) t--;
      if ((j - k + 1 <= l[i] && r[i] <= j) || x[i] - x[i - 1] == 1)
        t++, tmp[i] = 1;
      A[i][j] += t;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      int t = A[i][j];
      t -= x[i] + y[j];
      if (i > k) t += x[i - k];
      if (j > k) t += y[j - k];
      ans = max(ans, t);
    }
  }
  cout << ans + x[n] + y[n] << endl;
  return 0;
}
