#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
const int Mod = 1e9 + 7;
inline int read();
inline void write(int x);
char s[N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> rf(n, -1), rs(n, -1), cf(n, -1), cs(n, -1);
  for (register int r = 0; r <= n - 1; ++r) {
    scanf("%s", s);
    for (register int c = 0; c <= n - 1; ++c) {
      if (s[c] == 'B') {
        if (rf[r] == -1) rf[r] = c;
        rs[r] = c;
        if (cf[c] == -1) cf[c] = r;
        cs[c] = r;
      }
    }
  }
  int now = 0;
  for (register int i = 0; i <= n - 1; ++i) {
    if (rf[i] == -1) now += 1;
    if (cf[i] == -1) now += 1;
  }
  int best = 0;
  for (int r = 0; r + k <= n; r += 1) {
    int res = now;
    vector<int> gain(n, 0);
    vector<int> loss(n, 0);
    for (register int i = r; i <= r + k - 1; ++i) {
      if (rf[i] != -1 && rs[i] - rf[i] < k) {
        gain[rs[i]] += 1;
        loss[rf[i]] += 1;
      }
    }
    for (register int i = 0; i <= n - 1; ++i) {
      res += gain[i];
      if (r <= cf[i] && cs[i] < r + k) res += 1;
      if (i >= k) {
        res -= loss[i - k];
        if (r <= cf[i - k] && cs[i - k] < r + k) --res;
      }
      best = max(best, res);
    }
  }
  printf("%d\n", best);
  return 0;
}
inline int read() {
  int x = 0, symbol = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') symbol = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * symbol;
}
inline void write(int x) {
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
