#include <bits/stdc++.h>
using namespace std;
namespace tttt {}
using namespace tttt;
const int maxn = 2e3 + 5;
int n, k;
char s[maxn][maxn];
int sumR[maxn][maxn], sumC[maxn][maxn];
int ansR[maxn][maxn], ansC[maxn][maxn];
set<int> mpR, mpC, mp;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) sumR[i][j] = sumR[i][j - 1] + (s[i][j] == 'W');
  for (int j = 1; j <= n; j++)
    for (int i = 1; i <= n; i++) sumC[j][i] = sumC[j][i - 1] + (s[i][j] == 'W');
  for (int i = 1; i <= n; i++) {
    if (sumR[i][n] == n) mpR.insert(i);
    if (sumC[i][n] == n) mpC.insert(i);
  }
  int aR = mpR.size();
  int aC = mpC.size();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) ansR[i][j] = aR, ansC[i][j] = aC;
  for (int i = 1; i <= n - k + 1; i++) {
    int X = i, Y = 1;
    mp = mpC;
    for (int ii = 0; ii <= k - 1; ii++)
      if (sumC[Y + ii][n] +
              (k - (sumC[Y + ii][min(X + k - 1, n)] - sumC[Y + ii][X - 1])) ==
          n)
        mp.insert(Y + ii);
    ansC[X][Y] = max(aC, (int)(mp.size()));
    while (Y < n - k + 1) {
      if (sumC[Y][n] != n) mp.erase(Y);
      Y++;
      if (sumC[Y + k - 1][n] + (k - (sumC[Y + k - 1][min(X + k - 1, n)] -
                                     sumC[Y + k - 1][X - 1])) ==
          n)
        mp.insert(Y + k - 1);
      ansC[X][Y] = max(aC, (int)(mp.size()));
    }
  }
  for (int i = 1; i <= n - k + 1; i++) {
    int X = 1, Y = i;
    mp = mpR;
    for (int ii = 0; ii <= k - 1; ii++)
      if (sumR[X + ii][n] +
              (k - (sumR[X + ii][min(Y + k - 1, n)] - sumR[X + ii][Y - 1])) ==
          n)
        mp.insert(X + ii);
    ansR[X][Y] = max(aR, (int)(mp.size()));
    while (X < n - k + 1) {
      if (sumR[X][n] != n) mp.erase(X);
      X++;
      if (sumR[X + k - 1][n] + (k - (sumR[X + k - 1][min(Y + k - 1, n)] -
                                     sumR[X + k - 1][Y - 1])) ==
          n)
        mp.insert(X + k - 1);
      ansR[X][Y] = max(aR, (int)(mp.size()));
    }
  }
  int ans = 0;
  for (int i = 1; i <= n - k + 1; i++)
    for (int j = 1; j <= n - k + 1; j++) {
      ;
      ans = max(ans, ansR[i][j] + ansC[i][j]);
    }
  printf("%d", ans);
  return 0;
  ;
}
