#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
char ch[maxn][maxn];
int sum1[maxn][maxn];
int sum2[maxn][maxn];
int R[maxn][maxn];
int C[maxn][maxn];
int ans1[maxn][maxn];
int ans2[maxn][maxn];
int n, k;
int presum;
void input() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cin >> ch[i][j];
}
void init1() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      sum1[i][j] = sum1[i][j - 1] + (ch[i][j] == 'B');
      sum2[i][j] = sum2[i - 1][j] + (ch[i][j] == 'B');
    }
}
void init2() {
  for (int i = 1; i <= n; i++) {
    if (sum1[i][n] == 0) presum++;
    if (sum2[n][i] == 0) presum++;
  }
}
void init3() {
  for (int i = 1; i <= n; i++) {
    if (sum1[i][n] != 0)
      for (int j = 1; j <= n - k + 1; j++) {
        if (sum1[i][n] == sum1[i][j + k - 1] - sum1[i][j - 1]) R[i][j] = 1;
      }
  }
  for (int i = 1; i <= n; i++) {
    if (sum2[n][i] != 0)
      for (int j = 1; j <= n - k + 1; j++) {
        if (sum2[n][i] == sum2[j + k - 1][i] - sum2[j - 1][i]) C[j][i] = 1;
      }
  }
}
void init4() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      ans1[i][j] = ans1[i][j - 1] + C[i][j];
      ans2[i][j] = ans2[i - 1][j] + R[i][j];
    }
}
int main() {
  input();
  init1();
  init2();
  init3();
  init4();
  int ans = 0;
  for (int i = 1; i <= n - k + 1; i++)
    for (int j = 1; j <= n - k + 1; j++)
      ans = max(ans, ans1[i][j + k - 1] - ans1[i][j - 1] + ans2[i + k - 1][j] -
                         ans2[i - 1][j]);
  cout << ans + presum << endl;
}
