#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long max_n = 100005;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  long long arr[n][n];
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      char c;
      cin >> c;
      arr[i][j] = (c == 'B' ? 1 : 0);
    }
  }
  long long rmx[n], rmn[n], cmx[n], cmn[n];
  for (long long i = 0; i < n; i++) {
    rmn[i] = rmx[i] = cmx[i] = cmn[i] = -1;
    for (long long j = 0; j < n; j++) {
      if (arr[i][j] == 1) {
        if (rmn[i] == -1) rmn[i] = j;
        rmx[i] = max(rmx[i], j);
      }
      if (arr[j][i] == 1) {
        if (cmn[i] == -1) cmn[i] = j;
        cmx[i] = max(cmx[i], j);
      }
    }
    if (rmn[i] == -1) ans++;
    if (cmn[i] == -1) ans++;
  }
  long long row[n][n], coloumn[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j + k - 1 < n; j++) {
      row[i][j] = 0;
      coloumn[j][i] = 0;
      if (rmn[i] >= j && rmx[i] < j + k) {
        row[i][j] = 1;
      }
      if (cmn[i] >= j && cmx[i] < j + k) {
        coloumn[j][i] = 1;
      }
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 1; j < n; j++) {
      row[j][i] += row[j - 1][i];
      coloumn[i][j] += coloumn[i][j - 1];
    }
  }
  long long temp = ans;
  for (long long i = 0; i + k - 1 < n; i++) {
    for (long long j = 0; j + k - 1 < n; j++) {
      long long check1 = row[i + k - 1][j];
      long long check2 = coloumn[i][j + k - 1];
      if (i != 0) {
        check1 -= row[i - 1][j];
      }
      if (j != 0) {
        check2 -= coloumn[i][j - 1];
      }
      ans = max(ans, temp + check1 + check2);
    }
  }
  cout << ans;
  return 0;
}
