#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
bool maze[maxn][maxn];
struct LR {
  int l, r;
  LR(int l = maxn, int r = 0) : l(l), r(r) {}
  void Change(int _l, int _r) {
    l = min(l, _l);
    r = max(r, _r);
  }
} lr[maxn];
struct UD {
  int u, d;
  UD(int u = maxn, int d = 0) : u(u), d(d) {}
  void Change(int _u, int _d) {
    u = min(u, _u);
    d = max(d, _d);
  }
} ud[maxn];
int n, k;
int ans[maxn][maxn][2], origin = 0;
bool Check(int type, int x, int y, int pos) {
  if (type) {
    if (x <= ud[pos].u && ud[pos].d <= x + k - 1 && ud[pos].u != maxn) return 1;
  } else {
    if (y <= lr[pos].l && lr[pos].r <= y + k - 1 && lr[pos].l != maxn) return 1;
  }
  return 0;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      char t;
      cin >> t;
      if (t == 'B') {
        maze[i][j] = 1;
        lr[i].Change(j, j);
        ud[j].Change(i, i);
      } else
        maze[i][j] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (ud[i].u == maxn) origin++;
    if (lr[i].l == maxn) origin++;
  }
  for (int row = 1; row + k - 1 <= n; ++row) {
    int o = 0;
    for (int j = 1; j <= k; ++j) {
      if (Check(0, 1, row, j)) o++;
    }
    ans[1][row][0] = o;
    for (int line = 2; line + k - 1 <= n; ++line) {
      if (Check(0, line - 1, row, line - 1)) o--;
      if (Check(0, line + k - 1, row, line + k - 1)) o++;
      ans[line][row][0] = o;
    }
  }
  for (int line = 1; line + k - 1 <= n; ++line) {
    int o = 0;
    for (int i = 1; i <= k; ++i) {
      if (Check(1, line, 1, i)) o++;
    }
    ans[line][1][1] = o;
    for (int row = 2; row + k - 1 <= n; ++row) {
      if (Check(1, line, row - 1, row - 1)) o--;
      if (Check(1, line, row + k - 1, row + k - 1)) o++;
      ans[line][row][1] = o;
    }
  }
  int _ans = 0;
  for (int i = 1; i + k - 1 <= n; ++i) {
    for (int j = 1; j + k - 1 <= n; ++j) {
      _ans = max(_ans, ans[i][j][0] + ans[i][j][1]);
    }
  }
  cout << _ans + origin << endl;
}
