#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <sstream>
#include <climits>
#include <fstream>
#include <cmath>

using namespace std;

/// ********* debug template by Bidhan Roy *********

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const unordered_set< T > &v ) {
    os << "[";
    typename unordered_set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const unordered_map< F, S > &v ) {
    os << "[";
    typename unordered_map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

#define debug(x) cerr << #x << " = " << x << endl;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VC> VVC;
typedef vector<VLL> VVLL;
typedef vector<VD> VVD;
typedef vector<VS> VVS;

// TEMPLATE DEFINITIONS END

int n, k;
VS a;
vector<PII> cb, rb;

void read_input(){

    cin >> n >> k;
    a.resize(n);
    cb.resize(n, {INT_MAX, INT_MIN});
    rb.resize(n, {INT_MAX, INT_MIN});

    for(int i=0;i<n;i++){
        cin >> a[i];
        for(int j=0;j<n;j++){
            if(a[i][j] == 'B'){
                cb[j].first = min(cb[j].first, i);
                cb[j].second = max(cb[j].second, i);

                rb[i].first = min(rb[i].first, j);
                rb[i].second = max(rb[i].second, j);
            }
        }
    }
}

int white_lines(){
    LL lines = 0;
    for(int i=0;i<n;i++){
        if(cb[i].first == INT_MAX && cb[i].second == INT_MIN) lines++;
        if(rb[i].first == INT_MAX && rb[i].second == INT_MIN) lines++;
    }
    return lines;
}

void solve(){

    int lines = white_lines(), curr_lines, max_lines = 0;

    VVI lines_c(n, VI(n, 0)), lines_r(n, VI(n, 0));

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(cb[t].first != INT_MAX && cb[t].first >= i && cb[t].second < i + k) curr_lines++;
        }

        lines_c[i][0] = max(lines_c[i][0], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(cb[j-1].first != INT_MAX && cb[j-1].first >= i && cb[j-1].second < i + k)
                curr_lines--;

            if(cb[j+k-1].first != INT_MAX && cb[j+k-1].first >= i && cb[j+k-1].second < i + k)
                curr_lines++;

            lines_c[i][j] = max(lines_c[i][j], curr_lines);
        }
    }

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(rb[t].second != INT_MIN && rb[t].first >= i && rb[t].second < i + k) curr_lines++;
        }

        lines_r[0][i] = max(lines_r[0][i], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(rb[j-1].first != INT_MAX && rb[j-1].first >= i && rb[j-1].second < i + k)
                curr_lines--;

            if(rb[j+k-1].first != INT_MAX && rb[j+k-1].first >= i && rb[j+k-1].second < i + k)
                curr_lines++;

            lines_r[j][i] = max(lines_r[j][i], curr_lines);
        }
    }

    // debug(lines_c);
    // debug(lines_r);

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            max_lines = max(max_lines, lines_c[i][j] + lines_r[i][j]);
        }
    }

    cout << lines + max_lines << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read_input();
    solve();
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <sstream>
#include <climits>
#include <fstream>
#include <cmath>

using namespace std;

/// ********* debug template by Bidhan Roy *********

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const unordered_set< T > &v ) {
    os << "[";
    typename unordered_set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const unordered_map< F, S > &v ) {
    os << "[";
    typename unordered_map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

#define debug(x) cerr << #x << " = " << x << endl;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VC> VVC;
typedef vector<VLL> VVLL;
typedef vector<VD> VVD;
typedef vector<VS> VVS;

// TEMPLATE DEFINITIONS END

int n, k;
string s;
vector<PII> cb, rb;

void read_input(){

    cin >> n >> k;
    cb.resize(n, {INT_MAX, INT_MIN});
    rb.resize(n, {INT_MAX, INT_MIN});

    for(int i=0;i<n;i++){
        cin >> s;
        for(int j=0;j<n;j++){
            if(s[j] == 'B'){
                cb[j].first = min(cb[j].first, i);
                cb[j].second = max(cb[j].second, i);

                rb[i].first = min(rb[i].first, j);
                rb[i].second = max(rb[i].second, j);
            }
        }
    }
}

int white_lines(){
    LL lines = 0;
    for(int i=0;i<n;i++){
        if(cb[i].first == INT_MAX && cb[i].second == INT_MIN) lines++;
        if(rb[i].first == INT_MAX && rb[i].second == INT_MIN) lines++;
    }
    return lines;
}

VVI process(vector<PII> &b, bool swap){
    VVI lines(n, VI(n, 0));
    int curr_lines;

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(b[t].first != INT_MAX && b[t].first >= i && b[t].second < i + k) curr_lines++;
        }

        if(!swap)
            lines[i][0] = max(lines[i][0], curr_lines);
        else
            lines[0][i] = max(lines[0][i], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(b[j-1].first != INT_MAX && b[j-1].first >= i && b[j-1].second < i + k)
                curr_lines--;

            if(b[j+k-1].first != INT_MAX && b[j+k-1].first >= i && b[j+k-1].second < i + k)
                curr_lines++;

            if(!swap)
                lines[i][j] = max(lines[i][j], curr_lines);
            else
                lines[j][i] = max(lines[j][i], curr_lines);
        }
    }

    return lines;
}

void solve(){

    int lines = white_lines(), max_lines = 0;
    VVI lines_c = process(cb, false);
    VVI lines_r = process(rb, true);

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            max_lines = max(max_lines, lines_c[i][j] + lines_r[i][j]);
        }
    }

    cout << lines + max_lines << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read_input();
    solve();
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <sstream>
#include <climits>
#include <fstream>
#include <cmath>

using namespace std;

/// ********* debug template by Bidhan Roy *********

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const unordered_set< T > &v ) {
    os << "[";
    typename unordered_set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const unordered_map< F, S > &v ) {
    os << "[";
    typename unordered_map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

#define debug(x) cerr << #x << " = " << x << endl;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VC> VVC;
typedef vector<VLL> VVLL;
typedef vector<VD> VVD;
typedef vector<VS> VVS;

// TEMPLATE DEFINITIONS END

int n, k;
VS a;
vector<PII> cb, rb;

void read_input(){

    cin >> n >> k;
    a.resize(n);
    cb.resize(n, {INT_MAX, INT_MIN});
    rb.resize(n, {INT_MAX, INT_MIN});

    for(int i=0;i<n;i++){
        cin >> a[i];
        for(int j=0;j<n;j++){
            if(a[i][j] == 'B'){
                cb[j].first = min(cb[j].first, i);
                cb[j].second = max(cb[j].second, i);

                rb[i].first = min(rb[i].first, j);
                rb[i].second = max(rb[i].second, j);
            }
        }
    }
}

int white_lines(){
    LL lines = 0;
    for(int i=0;i<n;i++){
        if(cb[i].first == INT_MAX && cb[i].second == INT_MIN) lines++;
        if(rb[i].first == INT_MAX && rb[i].second == INT_MIN) lines++;
    }
    return lines;
}

void solve(){

    int lines = white_lines(), curr_lines, max_lines = 0;

    VVI lines_c(n, VI(n, 0)), lines_r(n, VI(n, 0));

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(cb[t].first != INT_MAX && cb[t].first >= i && cb[t].second < i + k) curr_lines++;
        }

        lines_c[i][0] = max(lines_c[i][0], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(cb[j-1].first != INT_MAX && cb[j-1].first >= i && cb[j-1].second < i + k)
                curr_lines--;

            if(cb[j+k-1].first != INT_MAX && cb[j+k-1].first >= i && cb[j+k-1].second < i + k)
                curr_lines++;

            lines_c[i][j] = max(lines_c[i][j], curr_lines);
        }
    }

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(rb[t].second != INT_MIN && rb[t].first >= i && rb[t].second < i + k) curr_lines++;
        }

        lines_r[0][i] = max(lines_r[0][i], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(rb[j-1].first != INT_MAX && rb[j-1].first >= i && rb[j-1].second < i + k)
                curr_lines--;

            if(rb[j+k-1].first != INT_MAX && rb[j+k-1].first >= i && rb[j+k-1].second < i + k)
                curr_lines++;

            lines_r[j][i] = max(lines_r[j][i], curr_lines);
        }
    }

    // debug(lines_c);
    // debug(lines_r);

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            max_lines = max(max_lines, lines_c[i][j] + lines_r[j][i]);
        }
    }

    cout << lines + max_lines << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read_input();
    solve();
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <sstream>
#include <climits>
#include <fstream>
#include <cmath>

using namespace std;

/// ********* debug template by Bidhan Roy *********

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const unordered_set< T > &v ) {
    os << "[";
    typename unordered_set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const unordered_map< F, S > &v ) {
    os << "[";
    typename unordered_map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

#define debug(x) cerr << #x << " = " << x << endl;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VC> VVC;
typedef vector<VLL> VVLL;
typedef vector<VD> VVD;
typedef vector<VS> VVS;

// TEMPLATE DEFINITIONS END

int n, k;
VS a;
vector<PII> cb, rb;

void read_input(){

    cin >> n >> k;
    a.resize(n);
    cb.resize(n, {INT_MAX, INT_MIN});
    rb.resize(n, {INT_MAX, INT_MIN});

    for(int i=0;i<n;i++){
        cin >> a[i];
        for(int j=0;j<n;j++){
            if(a[i][j] == 'B'){
                cb[j].first = min(cb[j].first, i);
                cb[j].second = max(cb[j].second, i);

                rb[i].first = min(rb[i].first, j);
                rb[i].second = max(rb[i].second, j);
            }
        }
    }
}

int white_lines(){
    LL lines = 0;
    for(int i=0;i<n;i++){
        if(cb[i].first == INT_MAX && cb[i].second == INT_MIN) lines++;
        if(rb[i].first == INT_MAX && rb[i].second == INT_MIN) lines++;
    }
    return lines;
}

void solve(){

    int lines = white_lines(), curr_lines, max_lines = 0;

    VVI lines_c(n, VI(n, 0)), lines_r(n, VI(n, 0));

    for(int i=0;i<=n-k;i++){
        curr_lines = 0;
        for(int t=0;t<k;t++){
            if(cb[t].first != INT_MAX && cb[t].first >= i && cb[t].second < i + k) curr_lines++;
            // if(rb[i+t].second != INT_MIN && rb[i+t].first >= 0 && rb[i+t].second < k) curr_lines++;
        }

        lines_c[i][0] = max(lines_c[i][0], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(cb[j-1].first != INT_MAX && cb[j-1].first >= i && cb[j-1].second < i + k)
                curr_lines--;

            if(cb[j+k-1].first != INT_MAX && cb[j+k-1].first >= i && cb[j+k-1].second < i + k)
                curr_lines++;

            lines_c[i][j] = max(lines_c[i][j], curr_lines);
        }
    }

    for(int i=0;i<=n-k;i++){

        curr_lines = 0;
        for(int t=0;t<k;t++){
            // if(cb[i+t].first != INT_MAX && cb[i+t].first >= 0 && cb[i+t].second < k) curr_lines++;
            if(rb[t].second != INT_MIN && rb[t].first >= i && rb[t].second < i + k) curr_lines++;
        }

        lines_r[0][i] = max(lines_r[0][i], curr_lines);

        for(int j=1;j<=n-k;j++){

            if(rb[j-1].first != INT_MAX && rb[j-1].first >= i && rb[j-1].second < i + k)
                curr_lines--;

            if(rb[j+k-1].first != INT_MAX && rb[j+k-1].first >= i && rb[j+k-1].second < i + k)
                curr_lines++;

            lines_r[j][i] = max(lines_r[i][j], curr_lines);
        }
    }

    // debug(lines_c);
    // debug(lines_r);

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            max_lines = max(max_lines, lines_c[i][j] + lines_r[j][i]);
        }
    }

    cout << lines + max_lines << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read_input();
    solve();
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <sstream>
#include <climits>
#include <fstream>
#include <cmath>

using namespace std;

/// ********* debug template by Bidhan Roy *********

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const unordered_set< T > &v ) {
    os << "[";
    typename unordered_set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const unordered_map< F, S > &v ) {
    os << "[";
    typename unordered_map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << ": " << it -> second ;
    }
    return os << "]";
}

#define debug(x) cerr << #x << " = " << x << endl;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VC> VVC;
typedef vector<VLL> VVLL;
typedef vector<VD> VVD;
typedef vector<VS> VVS;

// TEMPLATE DEFINITIONS END

int n, k;
VS a;
vector<PII> cb, rb;

void read_input(){

    cin >> n >> k;
    a.resize(n);
    cb.resize(n, {INT_MAX, INT_MIN});
    rb.resize(n, {INT_MAX, INT_MIN});

    for(int i=0;i<n;i++){
        cin >> a[i];
        for(int j=0;j<n;j++){
            if(a[i][j] == 'B'){
                cb[j].first = min(cb[j].first, i);
                cb[j].second = max(cb[j].second, i);

                rb[i].first = min(rb[i].first, j);
                rb[i].second = max(rb[i].second, j);
            }
        }
    }
}
int white_lines(){
    LL lines = 0;
    for(int i=0;i<n;i++){
        if(cb[i].first == INT_MAX && cb[i].second == INT_MIN)
            lines++;

        if(rb[i].first == INT_MAX && rb[i].second == INT_MIN)
            lines++;
    }

    return lines;
}
void solve(){

    int lines = white_lines();
    int or_lines = lines;
    int max_lines = 0;

    // debug(cb);
    // debug(rb);
    for(int i=0;i<=n-k;i++){

        lines = or_lines;

        for(int t=0;t<k;t++){
            if(cb[t].first != INT_MAX && cb[t].first >= i && cb[t].second < i + k) lines++;
            if(rb[i+t].second != INT_MIN && rb[i+t].first >= 0 && rb[i+t].second < k) lines++;
        }
        max_lines = max(max_lines, lines);

        for(int j=1;j<=n-k;j++){
            // debug(make_pair(i, j));

            if(cb[j-1].first != INT_MAX && cb[j-1].first >= i && cb[j-1].second < i + k)
                lines--;

            if(cb[j+k-1].first != INT_MAX && cb[j+k-1].first >= i && cb[j+k-1].second < i + k)
                lines++;

            for(int t=i;t<i+k;t++){
                if(rb[t].first != INT_MAX && rb[t].first >= j-1 && rb[t].second < j + k - 1)
                    lines--;

                // debug(t);
                // debug(lines);

                if(rb[t].first != INT_MAX && rb[t].first >= j && rb[t].second < j + k)
                    lines++;

                // debug(lines);
            }

            // debug(max_lines);
            max_lines = max(max_lines, lines);
        }
        // cout << "\n\n#########################\n";
    }

    cout << max_lines << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read_input();
    solve();
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	bool finish = false;
	for (int i = 1; i + k - 1 <= n&&!finish; ++i)
	{
		for (int j = 1; j + k - 1 <= n&&!finish; ++j)
		{
			ll ret = 0;
			for (int p = j; p <= j + k - 1; ++p)
				if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++ret;
			for (int p = i; p <= i + k - 1; ++p)
				if (le[p][j - 1] + righ[p][j] != n && le[p][j - 1] + righ[p][j + k] == n - k)++ret;
			ans = max(ans, res + ret);
			if (ret == 2 * k)finish = 1;
		}
	}
	printf("%lld\n", ans);
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	for (int i = 1; i + k - 1 <= n; ++i)
	{
		for (int j = 1; j + k - 1 <= n; ++j)
		{
			ll ret = 0;
			for (int p = j; p <= j + k - 1; ++p)
				if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++ret;
			for (int p = i; p <= i + k - 1; ++p)
				if (le[p][j - 1] + righ[p][j] != n && le[p][j - 1] + righ[p][j + k] == n - k)++ret;
			ans = max(ans, res + ret);
		}
	}
	printf("%lld\n", ans);
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
int dp[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	bool finish = false;
	for (int p = 1; p < k; ++p)
	{
		if (le[p][0] + righ[p][1] != n && le[p][0] + righ[p][k] == n - k)++dp[1][1];
	}

	for (int i = 1; i + k - 1 <= n&&!finish; ++i)
	{
		ll tmp = 0;
		for (int p = 1; p < k; ++p)
			if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++tmp;
		for (int j = 1; j + k - 1 <= n&&!finish; ++j)
		{
			if (j > 1 && up[i - 1][j - 1] + down[i][j - 1] != n && up[i - 1][j - 1] + down[i + k][j - 1] == n - k)--tmp;
			if (up[i - 1][j + k - 1] + down[i][j + k - 1] != n && up[i - 1][j + k - 1] + down[i + k][j + k - 1] == n - k)++tmp;
			ll ret = tmp;
			if (i > 1)
				dp[i][j] = dp[i - 1][j];
			if (i > 1 && le[i - 1][j - 1] + righ[i - 1][j] != n && le[i - 1][j - 1] + righ[i - 1][j + k] == n - k)--dp[i][j];
			if (le[i + k - 1][j - 1] + righ[i + k - 1][j] != n && le[i + k - 1][j - 1] + righ[i + k - 1][j + k] == n - k)++dp[i][j];
			ret += dp[i][j];
			ans = max(ans, res + ret);
		}
	}
	printf("%lld\n", ans);
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	bool finish = false;
	for (int i = 1; i + k - 1 <= n&&!finish; ++i)
	{
		ll tmp = 0;
		for (int p = 1; p < k; ++p)
			if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++tmp;
		for (int j = 1; j + k - 1 <= n&&!finish; ++j)
		{
			if (j > 1 && up[i - 1][j - 1] + down[i][j - 1] != n && up[i - 1][j - 1] + down[i + k][j - 1] == n - k)--tmp;
			if (up[i - 1][j + k - 1] + down[i][j + k - 1] != n && up[i - 1][j + k - 1] + down[i + k][j + k - 1] == n - k)++tmp;
			ll ret = tmp;
			for (int p = i; p <= i + k - 1; ++p)
				if (le[p][j - 1] + righ[p][j] != n && le[p][j - 1] + righ[p][j + k] == n - k)++ret;
			ans = max(ans, res + ret);
			if (ret == 2 * k)finish = 1;
		}
	}
	printf("%lld\n", ans);
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
int dp[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	bool finish = false;
	for (int j = 1; j + k - 1 <= n; ++j)
	{
		for (int p = 1; p < k; ++p)
		{
			if (le[p][j - 1] + righ[p][j] != n && le[p][j - 1] + righ[p][j + k] == n - k)++dp[1][j];
		}
	}

	for (int i = 1; i + k - 1 <= n && !finish; ++i)
	{
		ll tmp = 0;
		for (int p = 1; p < k; ++p)
			if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++tmp;
		for (int j = 1; j + k - 1 <= n && !finish; ++j)
		{
			if (j > 1 && up[i - 1][j - 1] + down[i][j - 1] != n && up[i - 1][j - 1] + down[i + k][j - 1] == n - k)--tmp;
			if (up[i - 1][j + k - 1] + down[i][j + k - 1] != n && up[i - 1][j + k - 1] + down[i + k][j + k - 1] == n - k)++tmp;
			ll ret = tmp;
			if (i > 1)
				dp[i][j] = dp[i - 1][j];
			if (i > 1 && le[i - 1][j - 1] + righ[i - 1][j] != n && le[i - 1][j - 1] + righ[i - 1][j + k] == n - k)--dp[i][j];
			if (le[i + k - 1][j - 1] + righ[i + k - 1][j] != n && le[i + k - 1][j - 1] + righ[i + k - 1][j + k] == n - k)++dp[i][j];
			ret += dp[i][j];
			ans = max(ans, res + ret);
		}
	}
	printf("%lld\n", ans);
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], * p1 = buf, * p2 = buf;
//#define int long long
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int mod = 998244853;
template<class T>inline T read(T& x) {
	int sign = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') { if (c == '-')sign = -1; c = getchar(); }
	while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x *= sign;
}
using namespace std;
const int maxn = 2e3 + 10;
int le[maxn][maxn], up[maxn][maxn], down[maxn][maxn], righ[maxn][maxn];
char mp[maxn][maxn];
int main()
{
	int n, k;
	read(n), read(k);
	ll res = 0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= n; ++j)
		{
			if (mp[i][j] == 'W')
			{
				le[i][j] = up[i][j] = 1;
				le[i][j] += le[i][j - 1];
				up[i][j] += up[i - 1][j];
			}
			if (j == n && le[i][j] == n)++res;
			if (i == n && up[i][j] == n)++res;
		}
	}
	for (int i = n; i >= 1; --i)
	{
		for (int j = n; j >= 1; --j)
		{
			if (mp[i][j] == 'W')
			{
				righ[i][j] = down[i][j] = 1;
				righ[i][j] += righ[i][j + 1];
				down[i][j] += down[i + 1][j];
			}
		}
	}
	ll ans = 0;
	bool finish = false;
	for (int i = 1; i + k - 1 <= n&&!finish; ++i)
	{
		ll tmp = 0;
		for (int p = 1; p < k; ++p)
			if (up[i - 1][p] + down[i][p] != n && up[i - 1][p] + down[i + k][p] == n - k)++tmp;
		for (int j = 1; j + k - 1 <= n&&!finish; ++j)
		{
			if (up[i - 1][j - 1] + down[i][j - 1] != n && up[i - 1][j - 1] + down[i + k][j - 1] == n - k)--tmp;
			if (up[i - 1][j + k - 1] + down[i][j + k - 1] != n && up[i - 1][j + k - 1] + down[i + k][j + k - 1] == n - k)++tmp;
			ll ret = tmp;
			for (int p = i; p <= i + k - 1; ++p)
				if (le[p][j - 1] + righ[p][j] != n && le[p][j - 1] + righ[p][j + k] == n - k)++ret;
			ans = max(ans, res + ret);
			if (ret == 2 * k)finish = 1;
		}
	}
	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
using namespace std;
const int maxm=2005;
int sumr[2005][2005];
int sumc[maxm][maxm];
int r[maxm][maxm];
int c[maxm][maxm];
int ansr[maxm][maxm];
int ansc[maxm][maxm];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        string s;
        cin>>s;
        for(int j=1; j<=n; j++)
        {
            if(s[j-1]=='B')
            {
                sumr[i][j]=sumr[i][j-1]+1;
                sumc[i][j]=sumc[i-1][j]+1;
            }
            else
            {
                 sumr[i][j]=sumr[i][j-1];
                sumc[i][j]=sumc[i-1][j];
            }

        }
    }
    int presum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumr[i][n]==0)
        {
            presum++;
           // cout<<i<<"  1"<<endl;
        }

        if(sumc[n][i]==0)
        {
            presum++;
            //cout<<i<<"  2"<<endl;
        }

    }
    for(int j=1; j<=n; j++)
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumr[j][i+k-1]-sumr[j][i-1]==sumr[j][n]&&sumr[j][n])
            {
                r[j][i]=1;
            }
        }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumc[j+k-1][i]-sumc[j-1][i]==sumc[n][i]&&sumc[n][i])
            {
                c[j][i]=1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansr[i][j]=ansr[i][j-1]+c[i][j];
            ansc[i][j]=ansc[i-1][j]+r[i][j];
        }
    }
    //cout<<presum<<endl;
    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

            ans=max(ans,ansr[i][j+k-1]-ansr[i][j-1]+ansc[i+k-1][j]-ansc[i-1][j]) ;
        }

    printf("%d",ans+presum);

}


#include<bits/stdc++.h>
using namespace std;
const int maxm=1005;
int sumr[2005][2005];
int sumc[maxm][maxm];
int r[maxm][maxm];
int c[maxm][maxm];
int ansr[maxm][maxm];
int ansc[maxm][maxm];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        string s;
        cin>>s;
        for(int j=1; j<=n; j++)
        {
            if(s[j-1]=='B')
            {
                sumr[i][j]=sumr[i][j-1]+1;
                sumc[i][j]=sumc[i-1][j]+1;
            }
            else
            {
                 sumr[i][j]=sumr[i][j-1];
                sumc[i][j]=sumc[i-1][j];
            }

        }
    }
    int presum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumr[i][n]==0)
        {
            presum++;
           // cout<<i<<"  1"<<endl;
        }

        if(sumc[n][i]==0)
        {
            presum++;
            //cout<<i<<"  2"<<endl;
        }

    }
    for(int j=1; j<=n; j++)
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumr[j][i+k-1]-sumr[j][i-1]==sumr[j][n]&&sumr[j][n])
            {
                r[j][i]=1;
            }
        }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumc[j+k-1][i]-sumc[j-1][i]==sumc[n][i]&&sumc[n][i])
            {
                c[j][i]=1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansr[i][j]=ansr[i][j-1]+c[i][j];
            ansc[i][j]=ansc[i-1][j]+r[i][j];
        }
    }
    //cout<<presum<<endl;
    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

            ans=max(ans,ansr[i][j+k-1]-ansr[i][j-1]+ansc[i+k-1][j]-ansc[i-1][j]) ;
        }

    printf("%d",ans+presum);

}


#include <bits/stdc++.h>
using namespace std;
const int N=2005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
int R[N][N],C[N][N];
char ch[N];int n,k;
void out(int a[][N])
{

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int main()
{

    scanf("%d%d",&n,&k);

    for(int i=1; i<=n; i++)
    {
    	scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
            sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
        }
    }

    int pre_sum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumR[i][n]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        if(sumC[n][i]==0)
            pre_sum++;
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumR[i][n]!=0)
                if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
                {
                    R[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumC[n][j]!=0)
                if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
                {
                    C[i][j]=1;
                }
        }
    }
   /* out(ansR);

    out(ansC); */

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
           ansR[i][j]=ansR[i][j-1]+C[i][j];
           ansC[i][j]=ansC[i-1][j]+R[i][j];
        }
    }
    /*out(ansR);
    out(ansC); */

    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

             ans=max(ans,ansR[i][j+k-1]-ansR[i][j-1]+ansC[i+k-1][j]-ansC[i-1][j]) ;
        }

    printf("%d",ans+pre_sum);

}


#include<bits/stdc++.h>
using namespace std;
const int maxm=1005;
int sumr[2005][2005];
int sumc[maxm][maxm];
int r[maxm][maxm];
int c[maxm][maxm];
int ansr[maxm][maxm];
int ansc[maxm][maxm];
char ch[maxm];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {

        scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumr[i][j]=sumr[i][j-1]+(ch[j]=='B');
            sumc[i][j]=sumc[i-1][j]+(ch[j]=='B');

        }
    }
    int presum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumr[i][n]==0)
        {
            presum++;
           // cout<<i<<"  1"<<endl;
        }

        if(sumc[n][i]==0)
        {
            presum++;
            //cout<<i<<"  2"<<endl;
        }

    }
    for(int j=1; j<=n; j++)
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumr[j][i+k-1]-sumr[j][i-1]==sumr[j][n]&&sumr[j][n])
            {
                r[j][i]=1;
            }
        }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumc[j+k-1][i]-sumc[j-1][i]==sumc[n][i]&&sumc[n][i])
            {
                c[j][i]=1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansr[i][j]=ansr[i][j-1]+c[i][j];
            ansc[i][j]=ansc[i-1][j]+r[i][j];
        }
    }
    //cout<<presum<<endl;
    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

            ans=max(ans,ansr[i][j+k-1]-ansr[i][j-1]+ansc[i+k-1][j]-ansc[i-1][j]) ;
        }

    printf("%d",ans+presum);

}


#include<bits/stdc++.h>
using namespace std;
const int maxm=1005;
int sumr[2005][2005];
int sumc[maxm][maxm];
int r[maxm][maxm];
int c[maxm][maxm];
int ansr[maxm][maxm];
int ansc[maxm][maxm];
char ch[maxm];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {

        scanf("%s",ch+1);
        for(int j=1; j<=n; j++)
        {
            sumr[i][j]=sumr[i][j-1]+(ch[j]=='B');
            sumc[i][j]=sumc[i-1][j]+(ch[j]=='B');

        }
    }
    int presum=0;
    for(int i=1; i<=n; i++)
    {
        if(sumr[i][n]==0)
        {
            presum++;
           // cout<<i<<"  1"<<endl;
        }

        if(sumc[n][i]==0)
        {
            presum++;
            //cout<<i<<"  2"<<endl;
        }

    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n-k+1; j++)
        {
            if(sumr[i][n]!=0)
                if(sumr[i][n]==sumr[i][j+k-1]-sumr[i][j-1])
                {
                    r[i][j]=1;
                }

        }

    }

    for(int j=1; j<=n; j++)
    {
        for(int i=1; i<=n-k+1; i++)
        {
            if(sumc[n][j]!=0)
                if(sumc[n][j]==sumc[i+k-1][j]-sumc[i-1][j])
                {
                    c[i][j]=1;
                }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            ansr[i][j]=ansr[i][j-1]+c[i][j];
            ansc[i][j]=ansc[i-1][j]+r[i][j];
        }
    }
    //cout<<presum<<endl;
    int ans=0;
    for(int i=1; i<=n-k+1; i++)
        for(int j=1; j<=n-k+1; j++)
        {

            ans=max(ans,ansr[i][j+k-1]-ansr[i][j-1]+ansc[i+k-1][j]-ansc[i-1][j]) ;
        }

    printf("%d",ans+presum);

}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << min(res, 2 * n) << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	l = max(0, l - 1);
	return Gx[i][r] - Gx[i][l];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		dy += del[n + 1];
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	l = max(0, l - 1);
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		dy += del[n + 1];
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		dy += del[n + 1];
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	l = max(0, l - 1);
	return Gx[i][r] - Gx[i][l];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = min(n, i + k - 1);
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		dy += del[n + 1];
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	assert(i >= 1 && i <= n);
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		dy += del[n + 1];
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	int res = 0;
	for (int j = 1; j <= n - k + 1; ++j) {
		int dy = 0;
		dx[0] = 0;
		memset(add, 0, sizeof add);
		memset(del, 0, sizeof del);
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		for (int o = j; o <= j + k - 1; ++o) {
			int Max = 0, Min = 1e9;
			for (int i = 1; i <= n; ++i) {
				if (s[i][o] == 'B') {
					Max = max(Max, i);
					Min = min(Min, i);
				}
			}
			if (Max == 0) ++dy;
			else if (Max - Min <= k) {
				++add[max(1, Max - k + 1)];
				++del[Min + 1];
			}
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
const int N = 2e3 + 10;
int n, k;
char s[N][N];
int x[N], y[N], dx[N];
int Gx[N][N];
int add[N], del[N];
int get(int i, int l, int r) {
	if (l > r) return 0;
	return Gx[i][r] - Gx[i][l - 1];
}
void run() {
	for (int i = 1; i <= n; ++i) {
		cin >> (s[i] + 1);
	}
	x[0] = 0; y[0] = 0;
	for (int i = 1; i <= n; ++i) {
		bool F = 1;
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		x[i] = x[i - 1] + F;
	}
	for (int j = 1; j <= n; ++j) {
		bool F = 1;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				F = 0;
				break;
			}
		}
		y[j] = y[j - 1] + F;
	}
	for (int i = 1; i <= n; ++i) {
		Gx[i][0] = 0;
		for (int j = 1; j <= n; ++j) {
			Gx[i][j] = Gx[i][j - 1] + (s[i][j] == 'B');
		}
	}
	memset(add, 0, sizeof add);
	memset(del, 0, sizeof del);
	int res = 0, dy = 0;
	for (int j = 1; j <= k - 1; ++j) {
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
	}
	for (int j = 1; j <= n - k + 1; ++j) {
		dx[0] = 0;
		for (int i = 1; i <= n; ++i) {
			dx[i] = dx[i - 1];
			if (get(i, 0, j - 1) + get(i, j + k, n) == 0) {
				++dx[i];
			}
		}
		int Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j + k - 1] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) ++dy;
		else if (Max - Min <= k) {
			++add[max(1, Max - k + 1)];
			++del[Min + 1];
		}
		int l = j, r = j + k - 1;
		for (int i = 1; i <= n; ++i) {
			int up = i, down = i + k - 1;
			dy += add[i] - del[i];
			res = max(res, y[l - 1] + y[n] - y[r] + x[up - 1] + x[n] - x[down] + dy + dx[down] - dx[up - 1]);
		}
		Max = 0, Min = 1e9;
		for (int i = 1; i <= n; ++i) {
			if (s[i][j] == 'B') {
				Max = max(Max, i);
				Min = min(Min, i);
			}
		}
		if (Max == 0) --dy;
		else if (Max - Min <= k) {
			--add[max(1, Max - k + 1)];
			--del[Min + 1];
		}
	}
	cout << res << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	while (cin >> n >> k) run();
	return 0;
}


#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n, k;
char c[2010][2010];
int sum[2010][2010];

void f(){
    rep(i, n){
        int l, r;
        l = r = -1;
        int u = max(0, i-k+1);
        int d = i;
        rep(j, n)if(c[i][j] == 'B'){
            if(l < 0) l = j;
            r = j;
        }
        if(r-l+1 > k) continue;
        if(l < 0) l = 0, r = n-1;
        else{
            int t = r;
            r = l;
            l = max(0, t-k+1);
        }
        ++sum[u][l];
        --sum[d+1][r+1];
    }

}

int main(){
    scanf("%d%d", &n, &k);
    rep(i, n)rep(j, n) scanf(" %c", &c[i][j]);
    rep(i, n+1)rep(j, n+1) sum[i][j] = 0;
    f();
    rep(i, n)For(j, i+1, n) swap(c[i][j], c[j][i]);
    f();
    rep(i, n+1)rep(j, n) sum[i][j+1] += sum[i][j];
    rep(j, n+1)rep(i, n) sum[i+1][j] += sum[i][j];
    int ans = 0;
    rep(i, n+1)rep(j, n+1) ans = max(ans, sum[i][j]);
    printf("%d\n", ans);
}


#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n, k;
char c[2010][2010];
int sum[2010][2010];

void f(int com){
    rep(i, n){
        int l, r;
        l = r = -1;
        int u = max(0, i-k+1);
        int d = i;
        rep(j, n)if(c[i][j] == 'B'){
            if(l < 0) l = j;
            r = j;
        }
        if(r-l+1 > k) continue;
        if(l < 0) l = 0, r = n-1;
        else{
            int t = r;
            r = l;
            l = max(0, t-k+1);
        }
        if(com == 0) ++sum[u][l], --sum[d+1][r+1];
        else ++sum[l][u], --sum[r+1][d+1];
    }

}

int main(){
    scanf("%d%d", &n, &k);
    rep(i, n)rep(j, n) scanf(" %c", &c[i][j]);
    rep(i, n+1)rep(j, n+1) sum[i][j] = 0;
    f(0);
    rep(i, n)For(j, i+1, n) swap(c[i][j], c[j][i]);
    f(1);
    rep(i, n+1)rep(j, n) sum[i][j+1] += sum[i][j];
    rep(j, n+1)rep(i, n) sum[i+1][j] += sum[i][j];
    int ans = 0;
    rep(i, n+1)rep(j, n+1) ans = max(ans, sum[i][j]);
    printf("%d\n", ans);
}


#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n, k;
char c[2010][2010];
int sum[2010][2010];

void f(int com){
    rep(i, n){
        int l, r;
        l = r = -1;
        int u = max(0, i-k+1);
        int d = i;
        rep(j, n)if(c[i][j] == 'B'){
            if(l < 0) l = j;
            r = j;
        }
        if(r-l+1 > k) continue;
        if(l < 0) l = 0, r = n-1;
        else{
            int t = r;
            r = l;
            l = max(0, t-k+1);
        }
        if(com) swap(l, u), swap(r, d);
        ++sum[u][l];
        --sum[u][r+1];
        --sum[d+1][l];
        --sum[d+1][r+1];
    }
}

int main(){
    scanf("%d%d", &n, &k);
    rep(i, n)rep(j, n) scanf(" %c", &c[i][j]);
    rep(i, n+1)rep(j, n+1) sum[i][j] = 0;
    f(0);
    rep(i, n)For(j, i+1, n) swap(c[i][j], c[j][i]);
    f(1);
    rep(i, n+1)rep(j, n) sum[i][j+1] += sum[i][j];
    rep(j, n+1)rep(i, n) sum[i+1][j] += sum[i][j];
    int ans = 0;
    rep(i, n+1)rep(j, n+1) ans = max(ans, sum[i][j]);
    printf("%d\n", ans);
}


#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n, k;
char c[2010][2010];
int sum[2010][2010];

void f(int com){
    rep(i, n){
        int l, r;
        l = r = -1;
        int u = max(0, i-k+1);
        int d = i;
        rep(j, n)if(c[i][j] == 'B'){
            if(l < 0) l = j;
            r = j;
        }
        if(r-l+1 > k) continue;
        if(l < 0){
            l = u = 0;
            r = d = n-1;
        }
        else{
            int t = r;
            r = l;
            l = max(0, t-k+1);
        }
        if(com) swap(l, u), swap(r, d);
        ++sum[u][l];
        --sum[u][r+1];
        --sum[d+1][l];
        ++sum[d+1][r+1];
    }
}

int main(){
    scanf("%d%d", &n, &k);
    rep(i, n)rep(j, n) scanf(" %c", &c[i][j]);
    rep(i, n+1)rep(j, n+1) sum[i][j] = 0;
    f(0);
    rep(i, n)For(j, i+1, n) swap(c[i][j], c[j][i]);
    f(1);
    rep(i, n+1)rep(j, n) sum[i][j+1] += sum[i][j];
    rep(j, n+1)rep(i, n) sum[i+1][j] += sum[i][j];
    int ans = 0;
    rep(i, n+1)rep(j, n+1) ans = max(ans, sum[i][j]);
    printf("%d\n", ans);
}


#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n, k;
char c[2010][2010];
int sum[2010][2010];

void f(int com){
    rep(i, n){
        int l, r;
        l = r = -1;
        int u = max(0, i-k+1);
        int d = i;
        rep(j, n)if(c[i][j] == 'B'){
            if(l < 0) l = j;
            r = j;
        }
        if(r-l+1 > k) continue;
        if(l < 0) l = 0, r = n-1;
        else{
            int t = r;
            r = l;
            l = max(0, t-k+1);
        }
        if(com) swap(l, u), swap(r, d);
        ++sum[u][l];
        --sum[u][r+1];
        --sum[d+1][l];
        ++sum[d+1][r+1];
    }
}

int main(){
    scanf("%d%d", &n, &k);
    rep(i, n)rep(j, n) scanf(" %c", &c[i][j]);
    rep(i, n+1)rep(j, n+1) sum[i][j] = 0;
    f(0);
    rep(i, n)For(j, i+1, n) swap(c[i][j], c[j][i]);
    f(1);
    rep(i, n+1)rep(j, n) sum[i][j+1] += sum[i][j];
    rep(j, n+1)rep(i, n) sum[i+1][j] += sum[i][j];
    int ans = 0;
    rep(i, n+1)rep(j, n+1) ans = max(ans, sum[i][j]);
    printf("%d\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define pb push_back
#define kek pop_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

struct SegTree {
	int n;
	vector<int> tree;
	vector<int> add;

	SegTree(int sz) : n(sz) {
		while (n & (n - 1)) {
			++n;
		}
		tree.resize(2 * n - 1, 0);
		add.resize(2 * n - 1, 0);
	}

	void plus(int v, int tl, int tr, int l, int r, int val) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return;
		}
		if (tl >= l && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return;
		}
		int mid = (tl + tr) >> 1;
		plus(2 * v + 1, tl, mid, l, r, val);
		plus(2 * v + 2, mid, tr, l, r, val);
		recalc(v, tl, tr);
	}

	int query(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return -INF;
		}
		if (tl >= l && tr <= r) {
			return tree[v];
		}
		int mid = (tl + tr) >> 1;
		int q1 = query(2 * v + 1, tl, mid, l, r);
		int q2 = query(2 * v + 2, mid, tr, l, r);
		return max(q1, q2);
	}

	void plus(int l, int r, int val) {
		plus(0, 0, n, l, r, val);
	}

	int query(int l, int r) {
		return query(0, 0, n, l, r);
	}

	void push(int v, int l, int r) {
		if (!add[v]) {
			return;
		}
		int a = add[v];
		add[v] = 0;
		tree[v] += a;
		if (r - l > 1) {
			add[2 * v + 1] += a;
			add[2 * v + 2] += a;
		}
	}

	void recalc(int v, int l, int r) {
		tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
	}
};

void run() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> fld(n, vector<int>(n));
	for (auto &v : fld) {
		for (auto &x : v) {
			char c;
			cin >> c;
			if (c == 'W') {
				x = 0;
			} else {
				x = 1;
			}
		}
	}
	pii kek_val = mp(INF, -INF);
	vector<pii> col(n, kek_val), row(n, kek_val);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (fld[i][j]) {
				cmin(row[i].first, j);
				cmax(row[i].second, j);
				cmin(col[j].first, i);
				cmax(col[j].second, i);
			}
		}
	}
	vector<vector<vector<int>>> add(n + 1);
	for (int i = 0; i < n; ++i) {
		if (col[i] == kek_val) {
			add[0].pb({0, n, 1});
			add[n].pb({0, n, -1});
		} else if (col[i].second - col[i].first <= k - 1) {
			int cl = i - k + 1, cr = i + 1;
			cmax(cl, 0);
			cmin(cr, n);
			int r = col[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, n);
			add[cl].pb({l, r, 1});
			add[cr].pb({l, r, -1});
		}
		if (row[i] == kek_val) {
			add[0].pb({0, n, 1});
			add[n].pb({0, n, -1});
		} else if (row[i].second - row[i].first <= k - 1) {
			int rl = i - k + 1, rr = i + 1;
			cmax(rl, 0);
			cmin(rr, n);
			int r = row[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, n);
			add[l].pb({rl, rr, 1});
			add[r].pb({rl, rr, -1});
		}
	}
	SegTree t(n);
	int ans = 0;
	for (auto &x : add) {
		for (auto &v : x) {
			t.plus(v[0], v[1], v[2]);
		}
		cmax(ans, t.query(0, n));
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define pb push_back
#define kek pop_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

struct SegTree {
	int n;
	vector<int> tree;
	vector<int> add;

	SegTree(int sz) : n(sz) {
		while (n & (n - 1)) {
			++n;
		}
		tree.resize(2 * n - 1, 0);
		add.resize(2 * n - 1, 0);
	}

	void plus(int v, int tl, int tr, int l, int r, int val) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return;
		}
		if (tl >= l && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return;
		}
		int mid = (tl + tr) >> 1;
		plus(2 * v + 1, tl, mid, l, r, val);
		plus(2 * v + 2, mid, tr, l, r, val);
		recalc(v, tl, tr);
	}

	int query(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return -INF;
		}
		if (tl >= l && tr <= r) {
			return tree[v];
		}
		int mid = (tl + tr) >> 1;
		int q1 = query(2 * v + 1, tl, mid, l, r);
		int q2 = query(2 * v + 2, mid, tr, l, r);
		return max(q1, q2);
	}

	void plus(int l, int r, int val) {
		plus(0, 0, n, l, r, val);
	}

	int query(int l, int r) {
		return query(0, 0, n, l, r);
	}

	void push(int v, int l, int r) {
		if (!add[v]) {
			return;
		}
		int a = add[v];
		add[v] = 0;
		tree[v] += a;
		if (r - l > 1) {
			add[2 * v + 1] += a;
			add[2 * v + 2] += a;
		}
	}

	void recalc(int v, int l, int r) {
		tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
	}
};

void run() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> fld(n, vector<int>(n));
	for (auto &v : fld) {
		for (auto &x : v) {
			char c;
			cin >> c;
			if (c == 'W') {
				x = 0;
			} else {
				x = 1;
			}
		}
	}
	pii kek_val = mp(INF, -INF);
	vector<pii> col(n, kek_val), row(n, kek_val);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (fld[i][j]) {
				cmin(row[i].first, j);
				cmax(row[i].second, j);
				cmin(col[j].first, i);
				cmax(col[j].second, i);
			}
		}
	}
	int N = n - k + 1;
	vector<vector<int>> add(N + 1, vector<int>(N + 1));
	for (int i = 0; i < n; ++i) {
		if (col[i] == kek_val) {
			++add[0][0];
			--add[0][N];
			--add[N][0];
			++add[N][N];
		} else if (col[i].second - col[i].first <= k - 1) {
			int cl = i - k + 1, cr = i + 1;
			cmax(cl, 0);
			cmin(cr, N);
			int r = col[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			++add[cl][l];
			--add[cl][r];
			--add[cr][l];
			++add[cr][r];
		}
		if (row[i] == kek_val) {
			++add[0][0];
			--add[0][N];
			--add[N][0];
			++add[N][N];
		} else if (row[i].second - row[i].first <= k - 1) {
			int rl = i - k + 1, rr = i + 1;
			cmax(rl, 0);
			cmin(rr, N);
			int r = row[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			++add[l][rl];
			--add[l][rr];
			--add[r][rl];
			++add[r][rr];
		}
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 1; j < N; ++j) {
			add[i][j] += add[i][j - 1];
		}
	}
	for (int i = 1; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			add[i][j] += add[i - 1][j];
		}
	}
	int ans = 0;
	for (auto &l : add) {
		for (auto &x : l) {
			cmax(ans, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define pb push_back
#define kek pop_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

struct SegTree {
	int n;
	vector<int> tree;
	vector<int> add;

	SegTree(int sz) : n(sz) {
		while (n & (n - 1)) {
			++n;
		}
		tree.resize(2 * n - 1, 0);
		add.resize(2 * n - 1, 0);
	}

	void plus(int v, int tl, int tr, int l, int r, int val) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return;
		}
		if (tl >= l && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return;
		}
		int mid = (tl + tr) >> 1;
		plus(2 * v + 1, tl, mid, l, r, val);
		plus(2 * v + 2, mid, tr, l, r, val);
		recalc(v, tl, tr);
	}

	int query(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return -INF;
		}
		if (tl >= l && tr <= r) {
			return tree[v];
		}
		int mid = (tl + tr) >> 1;
		int q1 = query(2 * v + 1, tl, mid, l, r);
		int q2 = query(2 * v + 2, mid, tr, l, r);
		return max(q1, q2);
	}

	void plus(int l, int r, int val) {
		plus(0, 0, n, l, r, val);
	}

	int query(int l, int r) {
		return query(0, 0, n, l, r);
	}

	void push(int v, int l, int r) {
		if (!add[v]) {
			return;
		}
		int a = add[v];
		add[v] = 0;
		tree[v] += a;
		if (r - l > 1) {
			add[2 * v + 1] += a;
			add[2 * v + 2] += a;
		}
	}

	void recalc(int v, int l, int r) {
		tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
	}
};

void run() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> fld(n, vector<int>(n));
	for (auto &v : fld) {
		for (auto &x : v) {
			char c;
			cin >> c;
			if (c == 'W') {
				x = 0;
			} else {
				x = 1;
			}
		}
	}
	pii kek_val = mp(INF, -INF);
	vector<pii> col(n, kek_val), row(n, kek_val);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (fld[i][j]) {
				cmin(row[i].first, j);
				cmax(row[i].second, j);
				cmin(col[j].first, i);
				cmax(col[j].second, i);
			}
		}
	}
	int N = n - k + 1;
	vector<vector<vector<int>>> add(N + 1);
	for (int i = 0; i < n; ++i) {
		if (col[i] == kek_val) {
			add[0].pb({0, N, 1});
			add[N].pb({0, N, -1});
		} else if (col[i].second - col[i].first <= k - 1) {
			int cl = i - k + 1, cr = i + 1;
			cmax(cl, 0);
			cmin(cr, N);
			int r = col[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			add[cl].pb({l, r, 1});
			add[cr].pb({l, r, -1});
		}
		if (row[i] == kek_val) {
			add[0].pb({0, N, 1});
			add[N].pb({0, N, -1});
		} else if (row[i].second - row[i].first <= k - 1) {
			int rl = i - k + 1, rr = i + 1;
			cmax(rl, 0);
			cmin(rr, N);
			int r = row[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			add[l].pb({rl, rr, 1});
			add[r].pb({rl, rr, -1});
		}
	}
	SegTree t(N);
	int ans = 0;
	for (auto &x : add) {
		for (auto &v : x) {
			t.plus(v[0], v[1], v[2]);
		}
		cmax(ans, t.query(0, N));
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define pb push_back
#define kek pop_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

struct SegTree {
	int n;
	vector<int> tree;
	vector<int> add;

	SegTree(int sz) : n(sz) {
		while (n & (n - 1)) {
			++n;
		}
		tree.resize(2 * n - 1, 0);
		add.resize(2 * n - 1, 0);
	}

	void plus(int v, int tl, int tr, int l, int r, int val) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return;
		}
		if (tl >= l && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return;
		}
		int mid = (tl + tr) >> 1;
		plus(2 * v + 1, tl, mid, l, r, val);
		plus(2 * v + 2, mid, tr, l, r, val);
		recalc(v, tl, tr);
	}

	int query(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return -INF;
		}
		if (tl >= l && tr <= r) {
			return tree[v];
		}
		int mid = (tl + tr) >> 1;
		int q1 = query(2 * v + 1, tl, mid, l, r);
		int q2 = query(2 * v + 2, mid, tr, l, r);
		return max(q1, q2);
	}

	void plus(int l, int r, int val) {
		plus(0, 0, n, l, r, val);
	}

	int query(int l, int r) {
		return query(0, 0, n, l, r);
	}

	void push(int v, int l, int r) {
		if (!add[v]) {
			return;
		}
		int a = add[v];
		add[v] = 0;
		tree[v] += a;
		if (r - l > 1) {
			add[2 * v + 1] += a;
			add[2 * v + 2] += a;
		}
	}

	void recalc(int v, int l, int r) {
		tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
	}
};

void run() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> fld(n, vector<int>(n));
	for (auto &v : fld) {
		for (auto &x : v) {
			char c;
			cin >> c;
			if (c == 'W') {
				x = 0;
			} else {
				x = 1;
			}
		}
	}
	pii kek_val = mp(INF, -INF);
	vector<pii> col(n, kek_val), row(n, kek_val);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (fld[i][j]) {
				cmin(row[i].first, j);
				cmax(row[i].second, j);
				cmin(col[j].first, i);
				cmax(col[j].second, i);
			}
		}
	}
	vector<vector<vector<int>>> add(n - k + 2);
	for (int i = 0; i < n; ++i) {
		if (col[i] == kek_val) {
			add[0].pb({0, n, 1});
			add[n].pb({0, n, -1});
		} else if (col[i].second - col[i].first <= k - 1) {
			int cl = i - k + 1, cr = i + 1;
			cmax(cl, 0);
			cmin(cr, n - k + 1);
			int r = col[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, n - k + 1);
			add[cl].pb({l, r, 1});
			add[cr].pb({l, r, -1});
		}
		if (row[i] == kek_val) {
			add[0].pb({0, n, 1});
			add[n].pb({0, n, -1});
		} else if (row[i].second - row[i].first <= k - 1) {
			int rl = i - k + 1, rr = i + 1;
			cmax(rl, 0);
			cmin(rr, n - k + 1);
			int r = row[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, n - k + 1);
			add[l].pb({rl, rr, 1});
			add[r].pb({rl, rr, -1});
		}
	}
	SegTree t(n - k + 2);
	int ans = 0;
	for (auto &x : add) {
		for (auto &v : x) {
			t.plus(v[0], v[1], v[2]);
		}
		cmax(ans, t.query(0, n));
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define pb push_back
#define kek pop_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

struct SegTree {
	int n;
	vector<int> tree;
	vector<int> add;

	SegTree(int sz) : n(sz) {
		while (n & (n - 1)) {
			++n;
		}
		tree.resize(2 * n - 1, 0);
		add.resize(2 * n - 1, 0);
	}

	void plus(int v, int tl, int tr, int l, int r, int val) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return;
		}
		if (tl >= l && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return;
		}
		int mid = (tl + tr) >> 1;
		plus(2 * v + 1, tl, mid, l, r, val);
		plus(2 * v + 2, mid, tr, l, r, val);
		recalc(v, tl, tr);
	}

	int query(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl >= r || tr <= l) {
			return -INF;
		}
		if (tl >= l && tr <= r) {
			return tree[v];
		}
		int mid = (tl + tr) >> 1;
		int q1 = query(2 * v + 1, tl, mid, l, r);
		int q2 = query(2 * v + 2, mid, tr, l, r);
		return max(q1, q2);
	}

	void plus(int l, int r, int val) {
		plus(0, 0, n, l, r, val);
	}

	int query(int l, int r) {
		return query(0, 0, n, l, r);
	}

	void push(int v, int l, int r) {
		if (!add[v]) {
			return;
		}
		int a = add[v];
		add[v] = 0;
		tree[v] += a;
		if (r - l > 1) {
			add[2 * v + 1] += a;
			add[2 * v + 2] += a;
		}
	}

	void recalc(int v, int l, int r) {
		tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
	}
};

void run() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> fld(n, vector<int>(n));
	for (auto &v : fld) {
		for (auto &x : v) {
			char c;
			cin >> c;
			if (c == 'W') {
				x = 0;
			} else {
				x = 1;
			}
		}
	}
	pii kek_val = mp(INF, -INF);
	vector<pii> col(n, kek_val), row(n, kek_val);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (fld[i][j]) {
				cmin(row[i].first, j);
				cmax(row[i].second, j);
				cmin(col[j].first, i);
				cmax(col[j].second, i);
			}
		}
	}
	int N = n - k + 1;
	vector<vector<int>> add(N + 1, vector<int>(N + 1));
	for (int i = 0; i < n; ++i) {
		if (col[i] == kek_val) {
			++add[0][0];
			--add[0][N];
			--add[N][0];
			++add[N][N];
		} else if (col[i].second - col[i].first <= k - 1) {
			int cl = i - k + 1, cr = i + 1;
			cmax(cl, 0);
			cmin(cr, N);
			int r = col[i].first + 1, l = col[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			++add[cl][l];
			--add[cl][r];
			--add[cr][l];
			++add[cr][r];
		}
		if (row[i] == kek_val) {
			++add[0][0];
			--add[0][N];
			--add[N][0];
			++add[N][N];
		} else if (row[i].second - row[i].first <= k - 1) {
			int rl = i - k + 1, rr = i + 1;
			cmax(rl, 0);
			cmin(rr, N);
			int r = row[i].first + 1, l = row[i].second - k + 1;
			cmax(l, 0);
			cmin(r, N);
			++add[l][rl];
			--add[l][rr];
			--add[r][rl];
			++add[r][rr];
		}
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 1; j < N; ++j) {
			add[i][j] += add[i][j - 1];
		}
	}
	for (int i = 1; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			add[i][j] += add[i - 1][j];
		}
	}
	int ans = 0;
	for (auto &l : add) {
		for (auto &x : l) {
			cmax(ans, x);
		}
	}
	cout << ans << endl;
}


/*input
2 1
WW
WW

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	int ar[n*2][n*2];
	REP(i,2*n){
		REP(j,2*n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) ar[max(i-k+n+1,0)][max(0,mx-k+n+1)]++;
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn) ar[max(0,mx-k+1+n)][max(i-k+n+1,0)]++;
		}
	}
	n*=2;

	if (k > n){
		cout<<n;
		return 0;
	}
	int stripSum[n][n];
	for (int j=0; j<n; j++)
	{
		int sum = 0;
		for (int i=0; i<k; i++)
			sum += ar[i][j];
		stripSum[0][j] = sum;
		for (int i=1; i<n-k+1; i++)
		{
			sum += (ar[i+k-1][j] - ar[i-1][j]);
			stripSum[i][j] = sum;
		}
	}
	int max_sum = INT_MIN, *pos = NULL;
	for (int i=0; i<n-k+1; i++)
	{
		int sum = 0;
		for (int j = 0; j<k; j++)
			sum += stripSum[i][j];

		if (sum > max_sum)
		{
			max_sum = sum;
			pos = &(ar[i][0]);
		}
		for (int j=1; j<n-k+1; j++)
		{
			sum += (stripSum[i][j+k-1] - stripSum[i][j-1]);
			if (sum > max_sum)
			{
				max_sum = sum;
				pos = &(ar[i][j]);
			}
		}
	}
	ll sum=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<k; j++)
			sum+= *(pos + i*n + j);

	}
	cout<<ans+sum;
}


/*input
4 3
BBBB
BBBB
BBBB
BBBW

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	int ar[n*2][n*2];
	REP(i,2*n){
		REP(j,2*n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) ar[max(i+n+1,0)][max(0,mx-k+n+1)]++;
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn) ar[max(0,mx-k+1+n)][max(i+n,0)]++;
		}
	}
	n*=2;
	int stripSum[n][n];
	for (int j=0; j<n; j++)
	{
		int sum = 0;
		for (int i=0; i<k; i++)
			sum += ar[i][j];
		stripSum[0][j] = sum;
		for (int i=1; i<n-k+1; i++)
		{
			sum += (ar[i+k-1][j] - ar[i-1][j]);
			stripSum[i][j] = sum;
		}
	}
	int max_sum = INT_MIN, *pos = NULL;
	for (int i=0; i<n-k+1; i++)
	{
		int sum = 0;
		for (int j = 0; j<k; j++)
			sum += stripSum[i][j];

		if (sum > max_sum)
		{
			max_sum = sum;
			pos = &(ar[i][0]);
		}
		for (int j=1; j<n-k+1; j++)
		{
			sum += (stripSum[i][j+k-1] - stripSum[i][j-1]);
			if (sum > max_sum)
			{
				max_sum = sum;
				pos = &(ar[i][j]);
			}
		}
	}
	ll sum=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<k; j++)
			sum+= *(pos + i*n + j);

	}
	cout<<ans+sum;
}


/*input
4 2
BWWW
WBBW
WBBW
WWWB

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	int ar[n*2][n*2];
	REP(i,2*n){
		REP(j,2*n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) ar[max(i+n,0)][max(0,mx-k+n+1)]++;
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn) ar[max(0,mx-k+1+n)][max(i+n,0)]++;
		}
	}
	n*=2;
	int stripSum[n][n];
	for (int j=0; j<n; j++)
	{
		int sum = 0;
		for (int i=0; i<k; i++)
			sum += ar[i][j];
		stripSum[0][j] = sum;
		for (int i=1; i<n-k+1; i++)
		{
			sum += (ar[i+k-1][j] - ar[i-1][j]);
			stripSum[i][j] = sum;
		}
	}
	int max_sum = INT_MIN, *pos = NULL;
	for (int i=0; i<n-k+1; i++)
	{
		int sum = 0;
		for (int j = 0; j<k; j++)
			sum += stripSum[i][j];

		if (sum > max_sum)
		{
			max_sum = sum;
			pos = &(ar[i][0]);
		}
		for (int j=1; j<n-k+1; j++)
		{
			sum += (stripSum[i][j+k-1] - stripSum[i][j-1]);
			if (sum > max_sum)
			{
				max_sum = sum;
				pos = &(ar[i][j]);
			}
		}
	}
	ll sum=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<k; j++)
			sum+= *(pos + i*n + j);

	}
	cout<<ans+sum;
}


/*input
4 2
BWWW
WBBW
WBBW
WWWB

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=2005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int ar[maxn][maxn];
void add(int x1,int y1,int x2,int y2)
{
	ar[x1][y1]++,ar[x2+1][y2+1]++;
	ar[x1][y2+1]--,ar[x2+1][y1]--;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	REP(i,2*n){
		REP(j,2*n) ar[i][j]=0;
	}
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) 	add(max(i-k+2,1),max(mx-k+2,1),i+1,mn+1);
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn){
			add(max(mx-k+2,1),max(i-k+2,1),mn+1,i+1);
		}
		}
	}
	int sum=0;
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	 {
	 	ar[i][j]+=ar[i-1][j]+ar[i][j-1]-ar[i-1][j-1];
	 	sum=max(sum,ar[i][j]);
	 }

	cout<<ans+sum;
}


/*input
4 2
BWWW
WBBW
WBBW
WWWB

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,0,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=2005;
const ll maxlg=__lg(maxn)+2;
const ll InF64=8000000000000000000LL;
const int InF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int ar[maxn][maxn];
void add(int x1,int y1,int x2,int y2)
{
	ar[x1][y1]++,ar[x2+1][y2+1]++;
	ar[x1][y2+1]--,ar[x2+1][y1]--;
}

int main() {
	int n,k;
	cin>>n>>k;
	char arr[n][n];
	FILL(ar)
	int ans=0;
	REP(i,n){
		int mx=-1;
		int mn=-1;
		REP(j,n){
			cin>>arr[i][j];
			if(arr[i][j]=='B' and mn==-1) mn=j;
			if(arr[i][j]=='B') mx=j;

		}
		if(mn==-1) ans++;
		else{
		if(mx-k+1<=mn) 	add(max(i-k+2,1),max(mx-k+2,1),i+1,mn+1);
		}
	}
	REP(i,n){
		int mn=-1,mx=-1;
		REP(j,n){
			if(arr[j][i]=='B' and mn==-1) mn=j;
			if(arr[j][i]=='B') mx=j;
		}

		if(mn==-1){
			ans++;

		}
		else{
		if(mx-k+1<=mn){
			add(max(mx-k+2,1),max(i-k+2,1),mn+1,i+1);
		}
		}
	}
	int sum=0;
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	 {
	 	ar[i][j]+=ar[i-1][j]+ar[i][j-1]-ar[i-1][j-1];
	 	sum=max(sum,ar[i][j]);
	 }

	cout<<ans+sum;
}