#include <bits/stdc++.h>
using namespace std;
int n, K, ans, sum;
char s[2003][2003];
int a[2003][2003], sum1[2003][2003], sum2[2003][2003], g1[2003][2003],
    g2[2003][2003], w1[2003][2003], w2[2003][2003];
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (ch < '0' || ch > '9') w |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
int main() {
  n = read();
  K = read();
  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++)
      if (s[i][j] == 'W')
        a[i][j] = 1;
      else
        a[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) sum1[i][j] = sum1[i][j - 1] + a[i][j];
  }
  for (int j = 1; j <= n; j++)
    for (int i = 1; i <= n; i++) sum2[i][j] = sum2[i - 1][j] + a[i][j];
  ans = 0;
  for (int i = 1; i <= n; i++)
    if (sum1[i][n] == n) ans++;
  for (int i = 1; i <= n; i++)
    if (sum2[n][i] == n) ans++;
  sum = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n - K + 1; j++)
      if (sum1[i][n] != n &&
          sum1[i][j - 1] + K + (sum1[i][n] - sum1[i][j + K - 1]) == n)
        g1[i][j]++;
  for (int j = 1; j <= n; j++)
    for (int i = 1; i <= n - K + 1; i++)
      if (sum2[n][j] != n &&
          sum2[i - 1][j] + K + (sum2[n][j] - sum2[i + K - 1][j]) == n)
        g2[i][j]++;
  for (int j = 1; j <= n - K + 1; j++)
    for (int i = 1; i <= n; i++) w1[i][j] = w1[i - 1][j] + g1[i][j];
  for (int i = 1; i <= n - K + 1; i++)
    for (int j = 1; j <= n; j++) w2[i][j] = w2[i][j - 1] + g2[i][j];
  for (int i = 1; i <= n - K + 1; i++)
    for (int j = 1; j <= n - K + 1; j++) {
      sum = max(sum, (w1[i + K - 1][j] - w1[i - 1][j]) +
                         (w2[i][j + K - 1] - w2[i][j - 1]));
    }
  ans += sum;
  printf("%d\n", ans);
  return 0;
}
