#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
char s[2035][2035];
int nxt[2035][2035], dn[2035][2035];
int x[2035], y[2035], dp[2035];
int A[2035][2035];
void fmain(int tid) {
  scanf("%d%d", &n, &k);
  for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%s", s[i] + 1);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    nxt[i][n + 1] = n + 1;
    for (int j = n; j; j--) {
      nxt[i][j] = nxt[i][j + 1];
      if (s[i][j] == 'B') nxt[i][j] = j;
    }
    if (nxt[i][1] == n + 1) x[i]++;
  }
  for (int(j) = 1; (j) <= (int)(n); (j)++) {
    dn[n + 1][j] = n + 1;
    for (int i = n; i; i--) {
      dn[i][j] = dn[i + 1][j];
      if (s[i][j] == 'B') dn[i][j] = i;
    }
    if (dn[1][j] == n + 1) y[j]++;
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    x[i] += x[i - 1];
    y[i] += y[i - 1];
  }
  for (int l = 1; l + k - 1 <= n; l++) {
    int r = l + k - 1;
    for (int(i) = 1; (i) <= (int)(n); (i)++) {
      if (nxt[i][1] >= l && nxt[i][r + 1] == n + 1)
        dp[i] = 1;
      else
        dp[i] = 0;
      dp[i] += dp[i - 1];
    }
    for (int i = k; i <= n; i++) {
      A[i - k + 1][l] = dp[i] - dp[i - k] + x[i - k] + x[n] - x[i];
    }
  }
  for (int u = 1; u + k - 1 <= n; u++) {
    int d = u + k - 1;
    for (int(j) = 1; (j) <= (int)(n); (j)++) {
      if (dn[1][j] >= u && dn[d + 1][j] == n + 1)
        dp[j] = 1;
      else
        dp[j] = 0;
      dp[j] += dp[j - 1];
    }
    for (int j = k; j <= n; j++) {
      A[u][j - k + 1] += dp[j] - dp[j - k] + y[j - k] + y[n] - y[j];
    }
  }
  int ans = 0;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    for (int(j) = 1; (j) <= (int)(n); (j)++) maxtt(ans, A[i][j]);
  printf("%d\n", ans);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
