#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const int INF = numeric_limits<int>::max();
const long long LINF = numeric_limits<long long>::max();
const long long mod = 1000 * 1000 * 1000 + 7;
vector<vector<int> > solve1(vector<int> l, vector<int> r, int n, int k) {
  int m = n - k + 1;
  vector<vector<int> > res(n + 1, vector<int>(n + 1, 0));
  for (int i = 0; i < n; ++i) {
    if ((r[i] >= l[i]) && (r[i] - l[i] + 1 <= k)) {
      int x = max(r[i] - k + 1, 0);
      for (int j = max(i - k + 1, 0); j <= i; ++j) {
        res[j][x] += 1;
        res[j][l[i] + 1] -= 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= n; ++j) {
      res[i][j] += res[i][j - 1];
    }
  }
  res.resize(m);
  for (int i = 0; i < m; ++i) {
    res[i].resize(m);
  }
  return res;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<string> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int res = 0;
  vector<int> l(n, n), r(n, -1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[i][j] == 'B') {
        l[i] = min(l[i], j);
        r[i] = max(r[i], j);
      }
    }
    if (l[i] > r[i]) ++res;
  }
  vector<int> u(n, n), d(n, -1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[j][i] == 'B') {
        u[i] = min(u[i], j);
        d[i] = max(d[i], j);
      }
    }
    if (u[i] > d[i]) ++res;
  }
  vector<vector<int> > v1 = solve1(l, r, n, k), v2 = solve1(u, d, n, k);
  int m = n - k + 1;
  int add = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j) {
      add = max(add, v1[i][j] + v2[j][i]);
    }
  }
  cout << res + add << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}
