#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int dp[2005][2005];
int n, k;
char s[2005][2005];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    int minn = 0x3f3f3f3f, maxx = -1;
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == 'B') {
        minn = min(minn, j);
        maxx = max(maxx, j);
      }
    }
    if (maxx == -1) {
      dp[1][1]++;
      dp[n + 1][n + 1]--;
    } else if (maxx - minn + 1 <= k) {
      int topx = max(1, i - k + 1);
      int topy = max(1, maxx - k + 1);
      int botx = min(n - k + 1, i);
      int boty = min(minn, n - k + 1);
      if (botx >= topx && boty >= topy) {
        dp[topx][topy]++;
        dp[botx + 1][topy]--;
        dp[topx][boty + 1]--;
        dp[botx + 1][boty + 1]++;
      }
    }
  }
  for (int j = 1; j <= n; j++) {
    int minn = 0x3f3f3f3f, maxx = -1;
    for (int i = 1; i <= n; i++) {
      if (s[i][j] == 'B') {
        minn = min(minn, i);
        maxx = max(maxx, i);
      }
    }
    if (maxx == -1) {
      dp[1][1]++;
      dp[n + 1][n + 1]++;
    } else if (maxx - minn + 1 <= k) {
      int topx = max(1, maxx - k + 1);
      int topy = max(1, j - k + 1);
      int botx = min(minn, n - k + 1);
      int boty = min(j, n - k + 1);
      if (botx >= topx && boty >= topy) {
        dp[topx][topy]++;
        dp[botx + 1][boty + 1]++;
        dp[botx + 1][topy]--;
        dp[topx][boty + 1]--;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] += (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) ans = max(ans, dp[i][j]);
  printf("%d\n", ans);
}
