#include <bits/stdc++.h>
using namespace std;
const long long N = 2005;
long long n, k, a[N][N];
long long R[N], C[N], sum[N][N];
long long ans[N][N], Col[N], pre[N], cc[N][N];
long long get(long long x, long long y, long long u, long long v) {
  return sum[u][v] - sum[x - 1][v] - sum[u][y - 1] + sum[x - 1][y - 1];
}
void prepare() {
  for (long long i = (long long)1; i <= (long long)n; i++)
    for (long long j = (long long)1; j <= (long long)n; j++)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
  for (long long i = (long long)1; i <= (long long)n; i++) {
    R[i] = R[i - 1] + (get(i, 1, i, n) == 0);
    C[i] = C[i - 1] + (get(1, i, n, i) == 0);
  }
}
void solve() {
  memset(sum, 0, sizeof sum);
  for (long long i = (long long)1; i <= (long long)n; i++)
    for (long long j = (long long)1; j <= (long long)n; j++)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
  for (long long x = (long long)1; x <= (long long)n - k + 1; x++) {
    long long u = x + k - 1;
    for (long long y = (long long)1; y <= (long long)k; y++) {
      Col[y] = (get(1, y, x - 1, y) == 0 && get(u + 1, y, n, y) == 0);
      pre[y] = pre[y - 1] + Col[y];
    }
    ans[x][1] = pre[k];
    for (long long y = (long long)2; y <= (long long)n - k + 1; y++) {
      long long v = y + k - 1;
      Col[v] = (get(1, v, x - 1, v) == 0 && get(u + 1, v, n, v) == 0);
      pre[v] = pre[v - 1] + Col[v];
      ans[x][y] = (pre[v] - pre[y - 1]);
    }
    for (long long i = (long long)1; i <= (long long)n; i++)
      Col[i] = pre[i] = 0;
  }
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("main.in", "r")) freopen("main.in", "r", stdin);
  cin >> n >> k;
  cerr << n << ' ' << k << '\n';
  for (long long i = (long long)1; i <= (long long)n; i++) {
    string s;
    cin >> s;
    for (long long j = (long long)1; j <= (long long)n; j++)
      a[i][j] = (s[j - 1] == 'W' ? 0 : 1);
  }
  prepare();
  solve();
  for (long long i = (long long)1; i <= (long long)n; i++)
    for (long long j = (long long)1; j <= (long long)n; j++)
      cc[i][j] = ans[i][j], ans[i][j] = 0;
  for (long long i = (long long)1; i <= (long long)n; i++)
    for (long long j = (long long)i; j <= (long long)n; j++)
      swap(a[i][j], a[j][i]);
  solve();
  long long res = 0;
  for (long long x = (long long)1; x <= (long long)n - k + 1; x++)
    for (long long y = (long long)1; y <= (long long)n - k + 1; y++) {
      long long u = x + k - 1, v = y + k - 1;
      long long val = ans[y][x] + cc[x][y] + (R[x - 1] + (R[n] - R[u])) +
                      (C[y - 1] + (C[n] - C[v]));
      if (x == 2 && y == 3) cerr << val << '\n';
      res = max(res, val);
    }
  cout << res << '\n';
  return 0;
}
