#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int inf = 0X3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int mod = 1000000007;
char g[2010][2010];
int dif[2010][2010];
int main() {
  int n, k, sum = 0;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", g[i]);
  }
  for (int i = 1; i <= n; i++) {
    int st = -1, ed = -1;
    for (int j = 0; j < n; j++) {
      if (g[i][j] == 'B') {
        if (st == -1) st = j + 1;
        ed = j + 1;
      }
    }
    if (st == -1)
      sum++;
    else if (ed - st + 1 <= k) {
      int lx = max(1, i - k + 1), ly = max(1, ed - k + 1);
      int rx = i, ry = st;
      dif[lx][ly]++;
      dif[lx][ry + 1]--;
      dif[rx + 1][ly]--;
      dif[rx + 1][ry + 1]++;
    }
  }
  for (int j = 0; j < n; j++) {
    int st = -1, ed = -1;
    for (int i = 1; i <= n; i++) {
      if (g[i][j] == 'B') {
        if (st == -1) st = i;
        ed = i;
      }
    }
    if (st == -1)
      sum++;
    else if (ed - st <= k) {
      int lx = max(1, ed - k + 1), ly = max(1, j + 1 - k + 1);
      int rx = st, ry = j + 1;
      dif[lx][ly]++;
      dif[lx][ry + 1]--;
      dif[rx + 1][ly]--;
      dif[rx + 1][ry + 1]++;
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dif[i][j] = dif[i][j] + dif[i][j - 1] + dif[i - 1][j] - dif[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans = max(ans, dif[i][j]);
    }
  }
  printf("%d\n", sum + ans);
}
