#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 7;
char s[2005][2005];
int a[2005][2005];
int n, k;
void insert(int x1, int y1, int x2, int y2) {
  x1 = min(x1, n + 1);
  x2 = min(x2, n + 1);
  y1 = min(y1, n + 1);
  y2 = min(y2, n + 1);
  a[x1][y1]++;
  a[x2][y1]--;
  a[x1][y2]--;
  a[x2][y2]++;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  int ma, mi;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ma = 0, mi = n + 1;
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == 'B') {
        ma = max(j, ma);
        mi = min(j, mi);
      }
    }
    if (!ma) {
      ans++;
      continue;
    }
    if (ma - mi + 1 > k) continue;
    insert(i, ma, i + k, mi + k);
  }
  for (int i = 1; i <= n; i++) {
    ma = 0, mi = n + 1;
    for (int j = 1; j <= n; j++) {
      if (s[j][i] == 'B') {
        ma = max(j, ma);
        mi = min(j, mi);
      }
    }
    if (!ma) {
      ans++;
      continue;
    }
    if (ma - mi + 1 > k) continue;
    insert(ma, i, mi + k, i + k);
  }
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
      tot = max(tot, a[i][j]);
    }
  }
  printf("%d\n", tot + ans);
  return 0;
}
