#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int n, k;
int a[maxn + 10][maxn + 10];
int col[maxn + 10][maxn + 10], row[maxn + 10][maxn + 10];
int csum[maxn + 10][maxn + 10], rsum[maxn + 10][maxn + 10];
int ans;
void init() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char x;
      cin >> x;
      if (x == 'W')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    rsum[i][0] = 0;
    for (int j = 1; j <= n; j++) rsum[i][j] = rsum[i][j - 1] + a[i][j];
  }
  for (int j = 1; j <= n; j++) {
    csum[0][j] = 0;
    for (int i = 1; i <= n; i++) csum[i][j] = csum[i - 1][j] + a[i][j];
  }
}
void dump(int a[][maxn + 10]) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cerr << a[i][j] << " ";
    }
    cerr << endl;
  }
  cerr << endl;
}
void work() {
  for (int i = 1; i <= n; i++) {
    int ct = 0;
    for (int j = 1; j + k - 1 <= n; j++) {
      if (j == 1) {
        for (int jj = 1; jj <= n; jj++) {
          if (jj > j + k - 1) {
            if (csum[n][jj] == 0) ct++;
          } else {
            if (csum[i - 1][jj] == 0 &&
                (csum[n][jj] - csum[i + k - 1][jj]) == 0)
              ct++;
          }
        }
      } else {
        int prev_col = j - 1;
        if (csum[i - 1][prev_col] == 0 &&
            (csum[n][prev_col] - csum[i + k - 1][prev_col] == 0))
          ct--;
        if (csum[n][prev_col] == 0) ct++;
        int next_col = j + k - 1;
        if (csum[n][next_col] == 0) ct--;
        if (csum[i - 1][next_col] == 0 &&
            (csum[n][next_col] - csum[i + k - 1][next_col] == 0))
          ct++;
      }
      col[i][j] = ct;
    }
  }
  for (int j = 1; j <= n; j++) {
    int ct = 0;
    for (int i = 1; i + k - 1 <= n; i++) {
      if (i == 1) {
        for (int ii = 1; ii <= n; ii++) {
          if (ii > i + k - 1) {
            if (rsum[ii][n] == 0) ct++;
          } else {
            if (rsum[ii][j - 1] == 0 &&
                (rsum[ii][n] - rsum[ii][j + k - 1]) == 0)
              ct++;
          }
        }
      } else {
        int prev_row = i - 1;
        if (rsum[prev_row][j - 1] == 0 &&
            (rsum[prev_row][n] - rsum[prev_row][j + k - 1] == 0))
          ct--;
        if (rsum[prev_row][n] == 0) ct++;
        int next_row = i + k - 1;
        if (rsum[next_row][n] == 0) ct--;
        if (rsum[next_row][j - 1] == 0 &&
            (rsum[next_row][n] - rsum[next_row][j + k - 1] == 0))
          ct++;
      }
      row[i][j] = ct;
    }
  }
  ans = 0;
  for (int i = 1; i + k - 1 <= n; i++) {
    for (int j = 1; j + k - 1 <= n; j++) {
      if (col[i][j] + row[i][j] > ans) {
        ans = col[i][j] + row[i][j];
      }
    }
  }
  cout << ans << endl;
}
int main() {
  init();
  work();
  return 0;
}
