#include <bits/stdc++.h>
using namespace std;
char b[2005][2005];
int a[2005][2005];
int qian[2005][2005] = {0};
int pre[2005][2005];
int main() {
  int n, len;
  scanf("%d%d", &n, &len);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> b[i][j];
      if (b[i][j] == 'W')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = n + 1;
    int r = 0;
    for (int j = 1; j <= n; j++) {
      if (a[i][j] == 1) {
        l = min(l, j);
        r = max(r, j);
      }
    }
    if (r == 0) {
      ans++;
      continue;
    }
    if (r - l + 1 > len) continue;
    int x, y;
    x = max(1, i - len + 1);
    y = max(1, r - len + 1);
    qian[i][l]++;
    qian[i][y - 1]--;
    qian[x - 1][l]--;
    qian[x - 1][y - 1]++;
  }
  for (int i = 1; i <= n; i++) {
    int l = n + 1;
    int r = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j][i]) {
        l = min(l, j);
        r = max(r, j);
      }
    }
    if (r == 0) {
      ans++;
      continue;
    }
    if (r - l + 1 > len) continue;
    int x1 = max(1, r - len + 1);
    int y1 = max(1, i - len + 1);
    qian[l][i]++;
    qian[l][y1 - 1]--;
    qian[x1 - 1][i]--;
    qian[x1 - 1][y1 - 1]++;
  }
  for (int i = n; i >= 1; i--) {
    for (int j = n; j >= 1; j--) {
      pre[i][j] =
          -pre[i + 1][j + 1] + pre[i][j + 1] + pre[i + 1][j] + qian[i][j];
    }
  }
  int maxn = -1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) maxn = max(maxn, pre[i][j]);
  }
  printf("%d\n", ans + maxn);
  return 0;
}
