#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 10;
char mp[MAXN][MAXN];
int sum[MAXN][MAXN], res[MAXN][MAXN], r[MAXN][MAXN], c[MAXN][MAXN];
int main() {
  int n, k;
  int ans = 0;
  int cnt = 0;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> mp[i] + 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      sum[i][j] = sum[i][j - 1] + ((mp[i][j] == 'W') ? 1 : 0);
      if (j == n && sum[i][j] == n) ans++;
      res[i][j] = res[i - 1][j] + ((mp[i][j] == 'W') ? 1 : 0);
      if (i == n && res[i][j] == n) ans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (sum[i][j - 1] + sum[i][n] - sum[i][j + k - 1] == n - k &&
          sum[i][j + k - 1] - sum[i][j - 1] != k)
        r[i][j] = r[i - 1][j] + 1;
      else
        r[i][j] = r[i - 1][j];
      if (res[i - 1][j] + res[n][j] - res[i + k - 1][j] == n - k &&
          res[i + k - 1][j] - res[i - 1][j] != k)
        c[i][j] = c[i][j - 1] + 1;
      else
        c[i][j] = c[i][j - 1];
    }
  }
  for (int i = 1; i + k - 1 <= n; i++) {
    for (int j = 1; j + k - 1 <= n; j++) {
      int cot = r[i + k - 1][j] - r[i - 1][j];
      cot += c[i][j + k - 1] - c[i][j - 1];
      cnt = max(cnt, cot);
    }
  }
  cout << ans + cnt << endl;
}
