#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<string> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<pair<int, int>> r(n, make_pair(n, -1)), c(n, make_pair(n, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (v[i][j] == 'B') {
        r[i].first = min(r[i].first, j);
        r[i].second = max(r[i].second, j);
        c[j].first = min(c[j].first, i);
        c[j].second = max(c[j].second, i);
      }
    }
  }
  int res_r[n - k + 1][n - k + 1], res_c[n - k + 1][n - k + 1];
  for (int i = 0; i < n - k + 1; i++) {
    res_r[0][i] = 0;
    res_c[i][0] = 0;
    for (int j = 0; j < k; j++) {
      res_r[0][i] += (r[j].first > r[j].second ||
                      (i <= r[j].first && r[j].second <= i + k - 1))
                         ? 1
                         : 0;
      res_c[i][0] += (c[j].first > c[j].second ||
                      (i <= c[j].first && c[j].second <= i + k - 1))
                         ? 1
                         : 0;
    }
    for (int j = k; j < n; j++) {
      res_r[0][i] += (r[j].first > r[j].second) ? 1 : 0;
      res_c[i][0] += (c[j].first > c[j].second) ? 1 : 0;
    }
    for (int j = 0; j < n - k; j++) {
      res_r[j + 1][i] = res_r[j][i];
      res_c[i][j + 1] = res_c[i][j];
      if (!(r[j + k].first > r[j + k].second) &&
          (i <= r[j + k].first && r[j + k].second <= i + k - 1)) {
        res_r[j + 1][i]++;
      }
      if (!(r[j].first > r[j].second) &&
          (i <= r[j].first && r[j].second <= i + k - 1)) {
        res_r[j + 1][i]--;
      }
      if (!(c[j + k].first > c[j + k].second) &&
          (i <= c[j + k].first && c[j + k].second <= i + k - 1)) {
        res_c[i][j + 1]++;
      }
      if (!(c[j].first > c[j].second) &&
          (i <= c[j].first && c[j].second <= i + k - 1)) {
        res_c[i][j + 1]--;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n - k + 1; i++) {
    for (int j = 0; j < n - k + 1; j++) {
      res = max(res, res_r[i][j] + res_c[i][j]);
    }
  }
  cout << res << "\n";
}
