#include <bits/stdc++.h>
using namespace std;
int g[2005], f[2005];
pair<int, int> G[2005], F[2005];
int a[2005][2005], p[2005][2005];
int n, k;
void mo() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void nhap() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      char s;
      cin >> s;
      if (s == 'B')
        a[i][j] = 1;
      else
        a[i][j] = 0;
    }
}
void tienxuli() {
  for (int i = 1; i <= n; i++) {
    g[i] = 1;
    f[i] = 1;
    G[i].first = n + 1;
    G[i].second = 0;
    F[i].first = n + 1;
    F[i].second = 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (a[i][j] == 1) {
        g[i] = 0;
        f[j] = 0;
        G[i].first = min(G[i].first, j);
        G[i].second = max(G[i].second, j);
        F[j].first = min(F[j].first, i);
        F[j].second = max(F[j].second, i);
      }
    }
}
void tham() {
  for (int i = k; i <= n; i++) {
    int dem = 0;
    for (int j = 1; j <= k; j++) {
      if (F[j].first >= i - k + 1 && F[j].second <= i) dem++;
    }
    for (int j = k + 1; j <= n; j++) {
      if (f[j] == 1) dem++;
    }
    p[i][k] += dem;
    for (int j = k + 1; j <= n; j++) {
      if (F[j - k].first >= i - k + 1 && F[j - k].second <= i) dem--;
      if (f[j] == 1) dem--;
      if (f[j - k] == 1) dem++;
      if (F[j].first >= i - k + 1 && F[j].second <= i) dem++;
      p[i][j] += dem;
    }
  }
  for (int j = k; j <= n; j++) {
    int dem = 0;
    for (int i = 1; i <= k; i++) {
      if (G[i].first >= j - k + 1 && G[i].second <= j) dem++;
    }
    for (int i = k + 1; i <= n; i++) {
      if (g[i] == 1) dem++;
    }
    p[k][j] += dem;
    for (int i = k + 1; i <= n; i++) {
      if (G[i - k].first >= j - k + 1 && G[i - k].second <= j) dem--;
      if (g[i] == 1) dem--;
      if (g[i - k] == 1) dem++;
      if (G[i].first >= j - k + 1 && G[i].second <= j) dem++;
      p[i][j] += dem;
    }
  }
  int res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      res = max(res, p[i][j]);
    }
  cout << res;
}
int main() {
  mo();
  nhap();
  tienxuli();
  tham();
}
