#include <bits/stdc++.h>
using namespace std;
bool dbg = 0;
clock_t start_time = clock();
void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}
template <typename T>
string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}
template <typename T>
void amax(T& x, T y) {
  x = max(x, y);
}
template <typename T>
void amin(T& x, T y) {
  x = min(x, y);
}
template <typename T>
T input() {
  T ans = 0, m = 1;
  char c = ' ';
  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }
  if (c == '-') m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}
template <typename T>
void read(T& a) {
  a = input<T>();
}
template <typename T>
void read(T& a, T& b) {
  read(a), read(b);
}
template <typename T>
void read(T& a, T& b, T& c) {
  read(a, b), read(c);
}
template <typename T>
void read(T& a, T& b, T& c, T& d) {
  read(a, b), read(c, d);
}
const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e5 + 12;
const long long llinf = 2e18 + 5;
const int mod = 1e9 + 7;
template <typename T>
T binpow(T n, T second) {
  if (second <= 0) return 1LL;
  if (second % 2 == 0) {
    T b = binpow(n, second / 2);
    return (1LL * b * b) % mod;
  } else {
    return (1LL * binpow(n, second - 1) * n) % mod;
  }
}
void upmin(int& x, int y) {
  if (x > y) {
    x = y;
  }
}
void upmax(int& x, int y) {
  if (x < y) {
    x = y;
  }
}
char a[2010][2010];
int frow[2010], lrow[2010];
int fcol[2010], lcol[2010];
int res[2010][2010];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    frow[i] = inf, lrow[i] = -1;
    fcol[i] = inf, lcol[i] = -1;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'B') {
        upmin(frow[i], j);
        upmax(lrow[i], j);
        upmin(fcol[j], i);
        upmax(lcol[j], i);
      }
    }
  for (int i = 0; i < n; i++) {
    if (lrow[i] - frow[i] + 1 > k) continue;
    if (frow[i] == inf) {
      res[0][0] += 1;
      continue;
    }
    int x1 = i - k + 1, y1 = lrow[i] - k + 1;
    int x2 = i, y2 = frow[i];
    upmax(x1, 0);
    upmax(y1, 0);
    upmin(x2, n - k);
    upmin(y2, n - k);
    res[x1][y1] += 1;
    res[x2 + 1][y1] -= 1;
    res[x1][y2 + 1] -= 1;
    res[x2 + 1][y2 + 1] += 1;
  }
  for (int i = 0; i < n; i++) {
    if (lcol[i] - fcol[i] + 1 > k) continue;
    if (fcol[i] == inf) {
      res[0][0] += 1;
      continue;
    }
    int x1 = lcol[i] - k + 1, y1 = i - k + 1;
    int x2 = fcol[i], y2 = i;
    upmax(x1, 0);
    upmax(y1, 0);
    upmin(x2, n - k);
    upmin(y2, n - k);
    res[x1][y1] += 1;
    res[x2 + 1][y1] -= 1;
    res[x1][y2 + 1] -= 1;
    res[x2 + 1][y2 + 1] += 1;
  }
  int ans = 0;
  for (int i = 0; i <= n - k; i++)
    for (int j = 0; j <= n - k; j++) {
      if (i) res[i][j] += res[i - 1][j];
      if (j) res[i][j] += res[i][j - 1];
      if (i && j) res[i][j] -= res[i - 1][j - 1];
      upmax(ans, res[i][j]);
    }
  cout << ans;
  return 0;
}
