#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
int n, k, ans;
char ch[N];
int sumR[N][N], sumC[N][N];
int eR[N][N], eC[N][N];
int ansR[N][N], ansC[N][N];
void read(int& x) {
  x = 0;
  bool y = 1;
  char ch = getchar();
  if (ch == '-') y = 0;
  while (!(ch >= '0' && ch <= '9')) {
    ch = getchar();
    if (ch == '-') y = 0;
  }
  while ((ch >= '0' && ch <= '9'))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  if (!y) x = -x;
  return;
}
int main() {
  read(n);
  read(k);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", ch + 1);
    for (int j = 1; j <= n; ++j) {
      sumR[i][j] = sumR[i][j - 1] + (ch[j] == 'B');
      sumC[i][j] = sumC[i - 1][j] + (ch[j] == 'B');
    }
  }
  int pre_s = 0;
  for (int i = 1; i <= n; ++i) {
    if (sumR[i][n] == 0) ++pre_s;
    if (sumC[n][i] == 0) ++pre_s;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n - k + 1; ++j)
      if (sumR[i][n] != 0)
        if (sumR[i][n] == sumR[i][j + k - 1] - sumR[i][j - 1]) eR[i][j] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n - k + 1; ++j)
      if (sumC[n][i] != 0)
        if (sumC[n][i] == sumC[j + k - 1][i] - sumC[j - 1][i]) eC[j][i] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      ansR[i][j] = ansR[i][j - 1] + eC[i][j];
      ansC[i][j] = ansC[i - 1][j] + eR[i][j];
    }
  ans = 0;
  for (int i = 1; i <= n - k + 1; ++i)
    for (int j = 1; j <= n - k + 1; ++j) {
      int yu = ansR[i][j + k - 1] - ansR[i][j - 1] + ansC[i + k - 1][j] -
               ansC[i - 1][j];
      ans = ((ans > yu) ? (ans) : (yu));
    }
  printf("%d\n", ans + pre_s);
  return 0;
}
