#include <bits/stdc++.h>
using namespace std;
const int N = 2020;
int Arr[N][N];
int Pre[N][N];
int Add[N][N];
bool Col[N];
bool Row[N];
int getSum(int x1, int y1, int x2, int y2) {
  return Pre[x2][y2] - Pre[x1 - 1][y2] - Pre[x2][y1 - 1] + Pre[x1 - 1][y1 - 1];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int k;
  cin >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      char ch;
      cin >> ch;
      Arr[i][j] = (ch == 'B');
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      Pre[i][j] = Pre[i - 1][j] + Pre[i][j - 1] - Pre[i - 1][j - 1] + Arr[i][j];
  int Total = 0;
  for (int i = 1; i <= n; i++) {
    if (!getSum(i, 1, i, n)) Row[i] = true, Total++;
    if (!getSum(1, i, n, i)) Col[i] = true, Total++;
  }
  for (int x = 1; x + k - 1 <= n; x++) {
    int Cur = Total;
    for (int i = 1; i < k; i++)
      if (!Col[i])
        Cur += (getSum(1, i, n, i) - getSum(x, i, x + k - 1, i) == 0);
    for (int y = 1; y + k - 1 <= n; y++) {
      if (!Col[y + k - 1])
        Cur += (getSum(1, y + k - 1, n, y + k - 1) -
                    getSum(x, y + k - 1, x + k - 1, y + k - 1) ==
                0);
      Add[x][y] += Cur;
      if (!Col[y])
        Cur -= (getSum(1, y, n, y) - getSum(x, y, x + k - 1, y) == 0);
    }
  }
  for (int x = 1; x + k - 1 <= n; x++) {
    int Cur = 0;
    for (int i = 1; i < k; i++)
      if (!Row[i])
        Cur += (getSum(i, 1, i, n) - getSum(i, x, i, x + k - 1) == 0);
    for (int y = 1; y + k - 1 <= n; y++) {
      if (!Row[y + k - 1])
        Cur += (getSum(y + k - 1, 1, y + k - 1, n) -
                    getSum(y + k - 1, x, y + k - 1, x + k - 1) ==
                0);
      Add[y][x] += Cur;
      if (!Row[y])
        Cur -= (getSum(y, 1, y, n) - getSum(y, x, y, x + k - 1) == 0);
    }
  }
  int Ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) Ans = max(Ans, Add[i][j]);
  cout << Ans << '\n';
  return 0;
}
