#include <bits/stdc++.h>
using namespace std;
const int maxN = 2005;
int d[maxN][maxN], c[maxN][maxN], D[maxN], C[maxN], dx[maxN][maxN],
    cx[maxN][maxN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  char cur;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      cin >> cur;
      d[i][j] = d[i][j - 1] + (cur == 'W');
      c[i][j] = c[i - 1][j] + (cur == 'W');
    }
  for (int i = 1; i <= n; ++i) {
    D[i] = D[i - 1] + (d[i][n] == n);
    C[i] = C[i - 1] + (c[n][i] == n);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = k; j <= n; ++j)
      dx[i][j] = dx[i - 1][j] + (d[i][n] - d[i][j] + d[i][j - k] == n - k);
  for (int i = k; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cx[i][j] = cx[i][j - 1] + (c[n][j] - c[i][j] + c[i - k][j] == n - k);
  int ans = 0;
  for (int i = k; i <= n; ++i)
    for (int j = k; j <= n; ++j)
      ans = max(ans, D[n] - D[i] + D[i - k] + dx[i][j] - dx[i - k][j] + C[n] -
                         C[j] + C[j - k] + cx[i][j] - cx[i][j - k]);
  cout << ans;
  return 0;
}
