#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e3 + 2;
int n, k;
int g[MAX_N][MAX_N];
int l[MAX_N][MAX_N], c[MAX_N][MAX_N];
int ls[MAX_N][MAX_N], cs[MAX_N][MAX_N];
void init() {
  memset(l, 0, sizeof(l));
  memset(c, 0, sizeof(c));
  for (int i = 0; i < MAX_N; i++) ls[0][i] = 0, cs[i][0] = 0;
}
int main() {
  init();
  scanf("%d%d", &n, &k);
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char cc;
      scanf("%c", &cc);
      if (cc == 'B')
        g[i][j] = 1;
      else
        g[i][j] = 0;
      l[i][j] = l[i][j - 1] + g[i][j], c[i][j] = c[i - 1][j] + g[i][j];
    }
    getchar();
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j + k - 1 <= n; j++) {
      ls[i][j] = ls[i - 1][j] +
                 (!l[i][j - 1] && !(l[i][n] - l[i][j + k - 1]) && l[i][n]);
      cs[j][i] = cs[j][i - 1] +
                 (!c[j - 1][i] && !(c[n][i] - c[j + k - 1][i]) && c[n][i]);
    }
  }
  int ans = 0, sum = 0;
  for (int i = 1; i <= n; i++) sum += (l[i][n] == 0) + (c[n][i] == 0);
  for (int i = 1; i + k - 1 <= n; i++) {
    for (int j = 1; j + k - 1 <= n; j++) {
      int t = ls[i + k - 1][j] - ls[i - 1][j] + cs[i][j + k - 1] -
              cs[i][j - 1] + sum;
      ans = max(ans, t);
    }
  }
  cout << ans << endl;
  return 0;
}
