#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const long long MOD2 = 998244353;
const long long N = 1000 * 100 + 5;
const long long N2 = 1000 * 1000;
const double PI = 3.14159265;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  char a[n][n];
  int r[n][n], c[n][n], row[n], col[n], prefr[n], prefc[n];
  memset(row, 0, sizeof(row));
  memset(col, 0, sizeof(col));
  memset(prefr, 0, sizeof(prefr));
  memset(prefc, 0, sizeof(prefc));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'B') {
        row[i]++;
        col[j]++;
        r[i][j] = c[i][j] = 1;
      } else
        r[i][j] = c[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!row[i]) prefr[i]++;
    if (!col[i]) prefc[i]++;
  }
  for (int i = 1; i < n; ++i) {
    prefr[i] += prefr[i - 1];
    prefc[i] += prefc[i - 1];
  }
  int tr = prefr[n - 1];
  int tc = prefc[n - 1];
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < n; ++j) {
      r[i][j] += r[i][j - 1];
    }
  }
  for (int j = 0; j < n; ++j) {
    for (int i = 1; i < n; ++i) {
      c[i][j] += c[i - 1][j];
    }
  }
  int si = n - k + 1, count = 0;
  int calcr[si][si], calcc[si][si];
  for (int i = 0; i < si; ++i) {
    int count = 0;
    for (int j = 0; j < k; ++j) {
      int dum;
      if (!i)
        dum = c[k - 1][j];
      else
        dum = c[k + i - 1][j] - c[i - 1][j];
      if ((col[j] - dum) == 0) count++;
    }
    calcc[i][0] = count;
    for (int j = 1; j < si; ++j) {
      int dum1, dum2;
      if (i)
        dum1 = c[i + k - 1][j - 1] - c[i - 1][j - 1];
      else
        dum1 = c[k - 1][j - 1];
      if (i)
        dum2 = c[i + k - 1][j + k - 1] - c[i - 1][j + k - 1];
      else
        dum2 = c[k - 1][j + k - 1];
      if (col[j - 1] == dum1) count--;
      if (col[j + k - 1] == dum2) count++;
      calcc[i][j] = count;
    }
  }
  for (int i = 0; i < si; ++i) {
    int count = 0;
    for (int j = 0; j < k; ++j) {
      int dum;
      if (!i)
        dum = r[j][k - 1];
      else
        dum = r[j][k + i - 1] - r[j][i - 1];
      if (row[j] == dum) count++;
    }
    calcr[0][i] = count;
    for (int j = 1; j < si; ++j) {
      int dum1, dum2;
      if (i)
        dum1 = r[j - 1][i + k - 1] - r[j - 1][i - 1];
      else
        dum1 = r[j - 1][k - 1];
      if (i)
        dum2 = r[j + k - 1][i + k - 1] - r[j + k - 1][i - 1];
      else
        dum2 = r[j + k - 1][k - 1];
      if (row[j - 1] == dum1) count--;
      if (row[j + k - 1] == dum2) count++;
      calcr[j][i] = count;
    }
  }
  int ans = 0;
  for (int i = 0; i < si; ++i) {
    for (int j = 0; j < si; ++j) {
      int dum = calcr[i][j] + calcc[i][j];
      int dum2, dum3;
      if (i)
        dum2 = prefr[i + k - 1] - prefr[i - 1];
      else
        dum2 = prefr[k - 1];
      if (j)
        dum3 = prefc[j + k - 1] - prefc[j - 1];
      else
        dum3 = prefc[k - 1];
      ans = max(ans, dum + tr + tc - dum2 - dum3);
    }
  }
  cout << ans;
}
