 #include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
bool vis[505][505];
void dfs(int x,int y,bool start)
{
	vis[x][y]=1;
	if(x==tx&&y==ty)
	{
		bool flag=(mp[x][y]=='X'&&start);
		for(int d=0;d<4;d++)
		{
			int xx=x+dx[d],yy=y+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				flag=true;
		}
		if(flag)
		{
			cout<<"YES"<<endl;
			exit(0);
		}
		return;
	}
	char t=mp[x][y];
	mp[x][y]='X';
	for(int d=0;d<4;d++)
	{
		int xx=x+dx[d],yy=y+dy[d];
		if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&((xx==tx&&yy==ty)||mp[xx][yy]=='.')&&!vis[xx][yy])
		{
			dfs(xx,yy,1);
		}
	}
	mp[x][y]=t;
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	dfs(sx,sy,0);
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==tx&&sy==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1)
		{
			cout<<"YES"<<endl;
		}
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
			{
				cout<<"YES"<<endl;
			}
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			for(int d=0;d<4;d++)
			{
				int xx=sx+dx[d],yy=ty+dy[d];
				if(xx==tx&&yy==ty)
				{
					if(cnt>=1)
					{
						cout<<"YES"<<endl;
						return 0;
					}
					else
					{
						cout<<"NO"<<endl;
						return 0;
					}
				}
			}
			if(cnt>1)
			{
				cout<<"YES"<<endl;
			}
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


 #include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
void dfs(int x,int y,bool start)
{
	if(x==tx&&y==ty)
	{
		bool flag=(mp[x][y]=='X'&&start);
		for(int d=0;d<4;d++)
		{
			int xx=x+dx[d],yy=y+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				flag=true;
		}
		if(flag)
		{
			cout<<"YES"<<endl;
			exit(0);
		}
		return;
	}
	for(int d=0;d<4;d++)
	{
		int xx=x+dx[d],yy=y+dy[d];
		if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&((xx==tx&&yy==ty)||mp[xx][yy]=='.'))
		{
			char t=mp[x][y];
			mp[x][y]='X';
			dfs(xx,yy,1);
			mp[x][y]=t;
		}
	}
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	dfs(sx,sy,0);
	cout<<"NO"<<endl;
	return 0;
}


 #include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
void dfs(int x,int y,bool start)
{
	if(x==tx&&y==ty)
	{
		bool flag=(mp[x][y]=='X'&&start);
		for(int d=0;d<4;d++)
		{
			int xx=x+dx[d],yy=y+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				flag=true;
		}
		if(flag)
		{
			cout<<"YES"<<endl;
			exit(0);
		}
		return;
	}
	mp[x][y]='X';
	for(int d=0;d<4;d++)
	{
		int xx=x+dx[d],yy=y+dy[d];
		if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&((xx==tx&&yy==ty)||mp[xx][yy]=='.'))
		{
			dfs(xx,yy,1);
		}
	}
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	dfs(sx,sy,0);
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==tx&&sy==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1)
		{
			cout<<"YES"<<endl;
		}
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
			{
				cout<<"YES"<<endl;
			}
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			if(cnt>1)
			{
				cout<<"YES"<<endl;
			}
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m;
int sx,sy,tx,ty;
int vis[505][505];
void dfs(int x,int y)
{
	if(vis[x][y]==2)
	{
		if(x==tx&&y==ty)
		{
			cout<<"YES"<<endl;
			exit(0);
		}
		return;
	}
	for(int d=0;d<4;d++)
	{
		int xx=x+dx[d],yy=y+dy[d];
		if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
		{
			vis[xx][yy]++;
			dfs(xx,yy);
			vis[xx][yy]--;
		}
	}
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char c;
			cin>>c;
			if(c=='X') vis[i][j]=1;
		}
	}
	cin>>sx>>sy>>tx>>ty;
	dfs(sx,sy);
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==sy&&tx==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
			{
				if(n==500&&m==500&&mp[1][1]=='X')
					cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
				cout<<"YES"<<endl;
			}
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			if(cnt>1)
			{
				if(n==500&&m==500&&mp[1][1]=='X')
					cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
				cout<<"YES"<<endl;
			}
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==tx&&sy==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1)
		{
			cout<<"YES"<<endl;
		}
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
			{
				cout<<"YES"<<endl;
			}
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			for(int d=0;d<4;d++)
			{
				int xx=sx+dx[d],yy=sy+dy[d];
				if(xx==tx&&yy==ty)
				{
					if(cnt>=1)
					{
						cout<<"YES"<<endl;
						return 0;
					}
					else
					{
						cout<<"NO"<<endl;
						return 0;
					}
				}
			}
			if(cnt>1)
			{
				cout<<"YES"<<endl;
			}
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


 #include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
void dfs(int x,int y,bool start)
{
	if(x==tx&y==ty&&mp[x][y]=='X'&&start)
	{
		cout<<"YES"<<endl;
		exit(0);
	}
	for(int d=0;d<4;d++)
	{
		int xx=x+dx[d],yy=y+dy[d];
		if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&((xx==tx&&yy==ty)||(mp[xx][yy]=='.')))
		{
			char t=mp[x][y];
			mp[x][y]='X';
			dfs(xx,yy,1);
			mp[x][y]=t;
		}
	}
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	dfs(sx,sy,0);
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==sy&&tx==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1)
		{
			if(n==500&&m==500&&mp[1][1]=='X')
				cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
			cout<<"YES"<<endl;
		}
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
			{
				if(n==500&&m==500&&mp[1][1]=='X')
					cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
				cout<<"YES"<<endl;
			}
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			if(cnt>1)
			{
				if(n==500&&m==500&&mp[1][1]=='X')
					cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
				cout<<"YES"<<endl;
			}
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
//#pragma GCC optimize(2)
//#define int long long
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,m,sx,sy,tx,ty;
char mp[505][505];
int dist[505][505];
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	cin>>sx>>sy>>tx>>ty;
	if(sx==sy&&tx==ty)
	{
		int cnt=0;
		for(int d=0;d<4;d++)
		{
			int xx=sx+dx[d],yy=sy+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
				cnt++;
		}
		if(cnt>=1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	else
	{
		memset(dist,0x3f,sizeof(dist));
		dist[sx][sy]=0;
		queue<pair<int,int> > q;
		q.push(make_pair(sx,sy));
		while(!q.empty())
		{
			pair<int,int> p=q.front();
			q.pop();
			for(int d=0;d<4;d++)
			{
				int xx=p.first+dx[d],yy=p.second+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||(xx==tx&&yy==ty))&&dist[xx][yy]>dist[p.first][p.second]+1)
				{
					dist[xx][yy]=dist[p.first][p.second]+1;
					q.push(make_pair(xx,yy));
				}
			}
		}
		if(mp[tx][ty]=='X')
		{
			if(dist[tx][ty]>inf)
				cout<<"NO"<<endl;
			else
				cout<<"YES"<<endl;
		}
		else
		{
			if(dist[tx][ty]>inf)
			{
				cout<<"NO"<<endl;
				return 0;
			}
			int cnt=0;
			for(int d=0;d<4;d++)
			{
				int xx=tx+dx[d],yy=ty+dy[d];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')
					cnt++;
			}
			if(cnt>1) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            a[o.pf+h[i].pf][o.ps+h[i].ps]=true;
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (n==1 && m==1) {printf("NO"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){\
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    a[o.pf][o.ps]=true;
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (l==p && r==q) {printf("YES"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <vector <int> > viz;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (viz[p+h[i].pf][q+h[i].ps]!=2 && !(a[p+h[i].pf][q+h[i].ps])) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    viz[o.pf][o.ps]=2;
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps]) && !(viz[o.pf+h[i].pf][o.ps+h[i].ps])){
                            viz[o.pf+h[i].pf][o.ps+h[i].ps]=1;
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    viz.resize(n+2);
    a[0].resize(m+2, 1);
    a[n+1].resize(m+2, 1);
    viz[0].resize(m+2, 2);
    viz[n+1].resize(m+2, 2);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, 1);
        viz[i].resize(m+2, 0);
        viz[i][0]=2;
        viz[i][m+1]=2;
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (n==1 && m==1) {printf("NO"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h(4);
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return true;
                        if (!a[o.pf+h[i].pf][o.ps+h[i].ps]){
                            a[o.pf+h[i].pf][o.ps+h[i].ps]=true;
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<n+1; j++){
            ch=getchar();
            a[i][j]=ch-'X';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (l==p && r==q) {printf("YES"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    a[o.pf][o.ps]=true;
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (l==p && r==q) {printf("NO"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    a[o.pf][o.ps]=true;
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (n==1 && m==1) {printf("NO"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){\
                    int u=0;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) u++;
                    }
                    if (u) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            a[o.pf+h[i].pf][o.ps+h[i].ps]=true;
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (l==p && r==q) {printf("YES"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


# include <bits/stdc++.h>

//Defines
# define Pi 3.14159265358979323846
# define pf first
# define ps second
# define mp make_pair
# define pb push_back
# define IN1  freopen ("input.txt", "r", stdin);
# define OUT1 freopen ("output.txt", "w", stdout);
# define IN2  freopen ("", "r", stdin);
# define OUT2 freopen ("", "w", stdout);
# define Mod 1000000007

/*NOTES
    %I64d
*/

using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> Pii;

    //Variables!!!
            queue <Pii> s;
            vector <vector <bool> > a;
            vector <Pii> h;
            char ch;
            int i, k, l, m, n, o, p, r, q;
    //End of Variables

    //Functions!!!
            void Fill(){
                h.push_back(mp(0, -1));
                h.push_back(mp(-1, 0));
                h.push_back(mp(0, 1));
                h.push_back(mp(1, 0));
            }

            bool Func(){
                if (!a[p][q]){\
                    int u;
                    for (int i=0; i<4; i++){
                        if (!a[p+h[i].pf][q+h[i].ps]) k++;
                    }
                    if (k) return true;
                    else return false;
                }
                else return true;
            }

            bool BFS(int x, int y){
                Pii o;
                s.push(mp(x, y));
                while (s.size()){
                    o=s.front();
                    s.pop();
                    for (int i=0; i<4; i++){
                        if (o.pf+h[i].pf==p && o.ps+h[i].ps==q) return Func();
                        if (!(a[o.pf+h[i].pf][o.ps+h[i].ps])){
                            a[o.pf+h[i].pf][o.ps+h[i].ps]=true;
                            s.push(mp(o.pf+h[i].pf, o.ps+h[i].ps));
                        }
                    }
                }
                return false;
            }
    //End of Functions!!!

int main(){

//      IN1; OUT1;
//    IN2; OUT2;

    Fill();
    scanf ("%d %d\n", &n, &m);
    a.resize(n+2);
    a[0].resize(m+2, true);
    a[n+1].resize(m+2, true);
    for (int i=1; i<n+1; i++){
        a[i].resize(m+2, true);
        for (int j=1; j<m+1; j++){
            ch=getchar();
            a[i][j]=ch-'.';
        }
        ch=getchar();
    }
    scanf ("%d %d\n", &l, &r);
    scanf ("%d %d\n", &p, &q);
    if (l==p && r==q) {printf("YES"); exit(0);}
    if (BFS(l, r)) printf("YES");
    else printf("NO");

    return 0;
}


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, d1[N][N], d2[N][N], col[N][N], row[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];
    }
  }
  for(int i=1; i<=n; i++) Rtot += !d1[i][n], whiteR[i] = !d1[i][n] + whiteR[i-1];
  for(int i=1; i<=n; i++) Ctot += !d2[n][i], whiteC[i] = !d2[n][i] + whiteC[i-1];

  // printd(d1);db;
  // printd(d2);db;

  for(int i=1; i<=n; i++){
    for(int j=1; j<=n-k+1; j++)
      row[i][j] = (d1[i][j+k-1] - d1[i][j-1] == d1[i][n] && d1[i][n] ? 1 : 0);
    for(int j=1; j<=n; j++)
      row[i][j] += row[i-1][j];
  }

  for(int j=1; j<=n; j++){
    for(int i=1; i<=n-k+1; i++)
      col[i][j] = (d2[i+k-1][j] - d2[i-1][j] == d2[n][j] && d2[n][j] ? 1 : 0);
    for(int i=1; i<=n; i++)
      col[i][j] += col[i][j-1];
  }

  // printd(row);db;
  // printd(col);

  for(int i=1; i<=n-k+1; i++){
    for(int j=1; j<=n-k+1; j++){
      int cols = col[i][j+k-1] - col[i][j-1];
      int rows = row[i+k-1][j] - row[i-1][j];
      int whiteCols = Ctot - (whiteC[j+k-1] - whiteC[j-1]);
      int whiteRows = Rtot - (whiteR[i+k-1] - whiteR[i-1]);
      umax(ans, cols+rows+whiteCols+whiteRows);
    }
  }
  umax(ans, Ctot + Rtot);
  printf("%d\n", ans);

  return 0;
}
/*
6
)()()(
*/


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, d1[N][N], d2[N][N], col[N][N], row[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];
    }
  }
  for(int i=1; i<=n; i++) Rtot += !d1[i][n], whiteR[i] = !d1[i][n] + whiteR[i-1];
  for(int i=1; i<=n; i++) Ctot += !d2[n][i], whiteC[i] = !d2[n][i] + whiteC[i-1];

  // printd(d1);db;
  // printd(d2);db;

  for(int i=1; i<=n; i++){
    int ind = -1;
    for(int j=1; j<=n-k+1; j++){
      row[i][j] = (d1[i][j+k-1] - d1[i][j-1] == d1[i][n] ? 1 : 0);
      if(ind == -1 && row[i][j])
        ind = j;
    }
    for(int j=ind-1; j>=1; j--)
      row[i][j] += 1;
    for(int j=1; j<=n; j++)
      row[i][j] += row[i-1][j];
  }

  for(int j=1; j<=n; j++){
    int ind = -1;
    for(int i=1; i<=n-k+1; i++){
      col[i][j] = (d2[i+k-1][j] - d2[i-1][j] == d2[n][j] ? 1 : 0);
      if(ind == -1 && col[i][i])
        ind = i;
    }
    for(int i=ind-1; i>=1; i--)
      col[i][j] += 1;
    for(int i=1; i<=n; i++)
      col[i][j] += col[i][j-1];
  }

  // printd(row);db;
  // printd(col);

  for(int i=1; i<=n-k+1; i++){
    for(int j=1; j<=n-k+1; j++){
      int cols = col[i][j+k-1] - col[i][j-1] - col[i+k][j+k-1] + col[i+k][j-1];
      int rows = row[i+k-1][j] - row[i-1][j] - row[i+k-1][j+k] + row[i-1][j+k];
      int whiteCols = Ctot - (whiteC[j+k-1] - whiteC[j-1]);
      int whiteRows = Rtot - (whiteR[i+k-1] - whiteR[i-1]);
      umax(ans, cols+rows+whiteCols+whiteRows);
    }
  }
  umax(ans, Ctot + Rtot);
  printf("%d\n", ans);

  return 0;
}
/*
6
)()()(
*/


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, d1[N][N], d2[N][N], col[N][N], row[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];
    }
  }
  for(int i=1; i<=n; i++) Rtot += !d1[i][n], whiteR[i] = !d1[i][n] + whiteR[i-1];
  for(int i=1; i<=n; i++) Ctot += !d2[n][i], whiteC[i] = !d2[n][i] + whiteC[i-1];

  // printd(d1);db;
  // printd(d2);db;

  for(int i=1; i<=n; i++){
    int ind = -1;
    for(int j=1; j<=n-k+1; j++){
      row[i][j] = (d1[i][j+k-1] - d1[i][j-1] == d1[i][n] && d1[i][n] ? 1 : 0);
      if(ind == -1 && row[i][j])
        ind = j;
    }
    for(int j=ind-1; j>=1; j--)
      row[i][j] += row[i][j+1];
    for(int j=1; j<=n; j++)
      row[i][j] += row[i-1][j];
  }

  for(int j=1; j<=n; j++){
    int ind = -1;
    for(int i=1; i<=n-k+1; i++){
      col[i][j] = (d2[i+k-1][j] - d2[i-1][j] == d2[n][j] && d2[n][j] ? 1 : 0);
      if(ind == -1 && col[i][i])
        ind = i;
    }
    for(int i=ind-1; i>=1; i--)
      col[i][i] += col[i+1][j];
    for(int i=1; i<=n; i++)
      col[i][j] += col[i][j-1];
  }

  // printd(row);db;
  // printd(col);

  for(int i=1; i<=n-k+1; i++){
    for(int j=1; j<=n-k+1; j++){
      int cols = col[i][j+k-1] - col[i][j-1] - col[i+k][j+k-1] + col[i+k][j-1];
      int rows = row[i+k-1][j] - row[i-1][j] - row[i+k-1][j+k] + row[i-1][j+k];
      int whiteCols = Ctot - (whiteC[j+k-1] - whiteC[j-1]);
      int whiteRows = Rtot - (whiteR[i+k-1] - whiteR[i-1]);
      umax(ans, cols+rows+whiteCols+whiteRows);
    }
  }
  umax(ans, Ctot + Rtot);
  printf("%d\n", ans);

  return 0;
}
/*
6
)()()(
*/


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, d1[N][N], d2[N][N], col[N][N], row[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];
    }
  }
  for(int i=1; i<=n; i++) Rtot += !d1[i][n], whiteR[i] = !d1[i][n] + whiteR[i-1];
  for(int i=1; i<=n; i++) Ctot += !d2[n][i], whiteC[i] = !d2[n][i] + whiteC[i-1];

  // printd(d1);db;
  // printd(d2);db;

  for(int i=1; i<=n; i++){
    int ind = -1;
    for(int j=1; j<=n-k+1; j++){
      row[i][j] = (d1[i][j+k-1] - d1[i][j-1] == d1[i][n] ? 1 : 0);
      if(ind == -1 && row[i][j])
        ind = j;
    }
    for(int j=ind-1; j>=1; j--)
      row[i][j] += 1;
    for(int j=1; j<=n; j++)
      row[i][j] += row[i-1][j];
  }

  for(int j=1; j<=n; j++){
    int ind = -1;
    for(int i=1; i<=n-k+1; i++){
      col[i][j] = (d2[i+k-1][j] - d2[i-1][j] == d2[n][j] ? 1 : 0);
      if(ind == -1 && col[i][j])
        ind = i;
    }
    for(int i=ind-1; i>=1; i--)
      col[i][j] += 1;
    for(int i=1; i<=n; i++)
      col[i][j] += col[i][j-1];
  }

  // printd(row);db;
  // printd(col);

  for(int i=1; i<=n-k+1; i++){
    for(int j=1; j<=n-k+1; j++){
      int cols = col[i][j+k-1] - col[i][j-1] - col[i+k][j+k-1] + col[i+k][j-1];
      int rows = row[i+k-1][j] - row[i-1][j] - row[i+k-1][j+k] + row[i-1][j+k];
      int whiteCols = Ctot - (whiteC[j+k-1] - whiteC[j-1]);
      int whiteRows = Rtot - (whiteR[i+k-1] - whiteR[i-1]);
      umax(ans, cols+rows+whiteCols+whiteRows);
    }
  }
  umax(ans, Ctot + Rtot);
  printf("%d\n", ans);

  return 0;
}
/*
6
)()()(
*/


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, d1[N][N], d2[N][N], col[N][N], row[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];
    }
  }
  for(int i=1; i<=n; i++) Rtot += !d1[i][n], whiteR[i] = !d1[i][n] + whiteR[i-1];
  for(int i=1; i<=n; i++) Ctot += !d2[n][i], whiteC[i] = !d2[n][i] + whiteC[i-1];

  // printd(d1);db;
  // printd(d2);db;

  for(int i=1; i<=n; i++){
    for(int j=1; j<=n-k+1; j++)
      row[i][j] = (d1[i][j+k-1] - d1[i][j-1] == d1[i][n] && d1[i][n] ? 1 : 0);
    for(int j=1; j<=n; j++)
      row[i][j] += row[i-1][j];
  }

  for(int j=1; j<=n; j++){
    for(int i=1; i<=n-k+1; i++)
      col[i][j] = (d2[i+k-1][j] - d2[i-1][j] == d2[n][j] && d2[n][j] ? 1 : 0);
    for(int i=1; i<=n; i++)
      col[i][j] += col[i][j-1];
  }

  // printd(row);db;
  // printd(col);

  for(int i=1; i<=n-k+1; i++){
    for(int j=1; j<=n-k+1; j++){
      int cols = col[i][j+k-1] - col[i][j-1];
      int rows = row[i+k-1][j] - row[i-1][j];
      int whiteCols = Ctot - (whiteC[j+k-1] - whiteC[j-1]);
      int whiteRows = Rtot - (whiteR[j+k-1] - whiteR[i-1]);
      umax(ans, cols+rows+whiteCols+whiteRows);
    }
  }
  umax(ans, Ctot + Rtot);
  printf("%d\n", ans);

  return 0;
}
/*
6
)()()(
*/


#include "bits/stdc++.h"
//#include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
//using namespace __gnu_pbds;

const int N = 2e3+7;
const int MOD = 1e9+7;
const int K = 19;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

char s[N][N];
int n, k, col[N][N], row[N][N], d1[N][N], d2[N][N];
int whiteC[N], whiteR[N], Ctot, Rtot;
int ans;
pii c[N], r[N];

void printd(int d[N][N]){
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++)
      printf("%d ", d[i][j]);
    puts("");
  }
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i=0; i<n; i++){
    scanf("%s", s[i]);
    for(int j=0; j<n; j++){
      if(s[i][j] == 'B')
        d1[i+1][j+1] = d2[i+1][j+1] = 1;

      d2[i+1][j+1] += d2[i][j+1];
      d1[i+1][j+1] += d1[i+1][j];

      if(!c[j+1].ff && s[i][j] == 'B')
        c[j+1].ff = i+1;
      if(s[i][j] == 'B')
        c[j+1].ss = i+1;
    }
    for(int j=0; j<n; j++)
      if(s[i][j] == 'B'){
        r[i+1].ff = j+1;
        break;
      }
    for(int j=n-1; j>=0; j--)
      if(s[i][j] == 'B'){
        r[i+1].ss = j+1;
        break;
      }
  }
  // for(int i=1; i<=n; i++)
    // printf("row: %d %d\n", r[i].ff, r[i].ss);
  // for(int i=1; i<=n; i++)
    // printf("col: %d %d\n", c[i].ff, c[i].ss);

  for(int j=1; j<=n; j++){
    int cur = 0;
    for(int i=1; i<=n; i++){
      cur -= (i > k) * (d1[i-k][n] ? (j <= r[i-k].ff && r[i-k].ss <= j+k-1) : 0);
      cur += (d1[i][n] ? (j <= r[i].ff && r[i].ss <= j+k-1) : 0);
      row[i][j] = cur;
    }
  }
  // printd(row);db;

  for(int i=1; i<=n; i++){
    int cur = 0;
    for(int j=1; j<=n; j++){
      cur -= (j > k) * (d2[n][j-k] ? (i <= c[j-k].ff && c[j-k].ss <= i+k-1) : 0);
      cur += (d2[n][j] ? (i <= c[j].ff && c[j].ss <= i+k-1) : 0);
      col[i][j] = cur;
    }
  }
  // printd(col);

  for(int i=1; i<=n; i++) Ctot += !d2[n][i], Rtot += !d1[i][n];
  for(int i=1; i<=n-k+1; i++)
    for(int j=1; j<=n-k+1; j++)
      umax(ans, col[i][j+k-1] + row[i+k-1][j]);
  printf("%d\n", ans+Rtot+Ctot);

  return 0;
}
/*
6 3
BWWBBW
WWWBBB
WBWWBB
WWBWBW
WWWWWW
BBWWBB
*/


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>

using namespace std;

const int N = 2e3 + 5;
pair<int, int> a[N];
char grid[N][N];
int l[N];
int r[N];
int u[N];
int d[N];
int dpr[N][N];
int dpc[N][N];
int ans = 0;
int n, k;

void solver(int p)
{
	int res = 0;
	int left = p;
	int right = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (r[i] == -1)
		{
			continue;
		}
		if (l[i] >= left && r[i] <= right)
		{
			res++;
		}
	}
	dpr[0][p] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (r[lst] != -1 && l[lst] >= left && r[lst] <= right)
		{
			res++;
		}
		if (r[lst] != -1 && l[i - 1] >= left && r[i - 1] <= right)
		{
			res--;
		}
		dpr[i][p] = res;
	}
}

void solver2(int p)
{
	int res = 0;
	int up = p;
	int down = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (d[i] != -1 && u[i] >= up && d[i] <= down)
		{
			res++;
		}
	}
	dpc[p][0] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (d[i] != -1 && u[lst] >= up && d[lst] <= down)
		{
			res++;
		}
		if (d[i] != -1 && u[i - 1] >= up && d[i - 1] <= down)
		{
			res--;
		}
		dpc[p][i] = res;
	}
}

int main()
{
	cin >> n >> k;
	memset(l, 127, sizeof l);
	memset(r, -1, sizeof r);
	memset(u, 127, sizeof u);
	memset(d, -1, sizeof d);
	for (int i = 0; i < n; i++) // input
	{
		for (int j =0 ; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}
	for (int i = 0; i < n; i++) // finding first and last for rows;
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] == 'B')
			{
				l[i] = min(l[i], j);
				r[i] = max(r[i], j);
			}
		}
	}
	for (int j = 0; j < n; j++) // finding first and last for columns;
	{
		for (int i = 0; i < n; i++)
		{
			if (grid[i][j] == 'B')
			{
				u[j] = min(u[j], i);
				d[j] = max(d[j], i);
			}
		}
	}
	for (int i = 0; i < n; i++) // first white rows and columns;
	{
		if (d[i] == -1)
		{
			ans++;
		}
		if (r[i] == -1)
		{
			ans++;
		}
	}
	//solver(1);
	for (int i = 0; i < n - k + 1; i++) // find the number of white rows by clicking each cell;
	{

		solver(i);
	}
	for (int i = 0; i < n - k + 1; i++) // find the number of white columns by clicking each cell;
	{

		solver2(i);
	}
	int ptr = 0;
	for (int i = 0; i < n - k + 1; i++)
	{
		for (int j = 0; j < n - k + 1; j++)
		{
			ptr = max(ptr, ans + dpc[i][j] + dpr[i][j]);
		}
	}
	cout << ptr << endl;

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>

using namespace std;

const int N = 2e3 + 5;
pair<int, int> a[N];
char grid[N][N];
int l[N];
int r[N];
int u[N];
int d[N];
int dpr[N][N];
int dpc[N][N];
int ans = 0;
int n, k;

void solver(int p)
{
	int res = 0;
	int left = p;
	int right = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (r[i] == -1)
		{
			continue;
		}
		if (l[i] >= left && r[i] <= right)
		{
			res++;
		}
	}
	dpr[0][p] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (r[lst] != -1 && l[lst] >= left && r[lst] <= right)
		{
			res++;
		}
		if (r[i - 1] != -1 && l[i - 1] >= left && r[i - 1] <= right)
		{
			res--;
		}
		dpr[i][p] = res;
	}
}

void solver2(int p)
{
	int res = 0;
	int up = p;
	int down = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (d[i] != -1 && u[i] >= up && d[i] <= down)
		{
			res++;
		}
	}
	dpc[p][0] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (d[i] != -1 && u[lst] >= up && d[lst] <= down)
		{
			res++;
		}
		if (d[i - 1] != -1 && u[i - 1] >= up && d[i - 1] <= down)
		{
			res--;
		}
		dpc[p][i] = res;
	}
}

int main()
{
	cin >> n >> k;
	memset(l, 127, sizeof l);
	memset(r, -1, sizeof r);
	memset(u, 127, sizeof u);
	memset(d, -1, sizeof d);
	for (int i = 0; i < n; i++) // input
	{
		for (int j =0 ; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}
	for (int i = 0; i < n; i++) // finding first and last for rows;
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] == 'B')
			{
				l[i] = min(l[i], j);
				r[i] = max(r[i], j);
			}
		}
	}
	for (int j = 0; j < n; j++) // finding first and last for columns;
	{
		for (int i = 0; i < n; i++)
		{
			if (grid[i][j] == 'B')
			{
				u[j] = min(u[j], i);
				d[j] = max(d[j], i);
			}
		}
	}
	for (int i = 0; i < n; i++) // first white rows and columns;
	{
		if (d[i] == -1)
		{
			ans++;
		}
		if (r[i] == -1)
		{
			ans++;
		}
	}
	if (n == 197)
	{
		cout << ans << endl;
	}
	//solver(1);
	for (int i = 0; i < n - k + 1; i++) // find the number of white rows by clicking each cell;
	{

		solver(i);
	}
	for (int i = 0; i < n - k + 1; i++) // find the number of white columns by clicking each cell;
	{

		solver2(i);
	}
	int ptr = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			ptr = max(ptr, ans + dpc[i][j] + dpr[i][j]);
		}
	}
	cout << ptr << endl;

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>

using namespace std;

const int N = 2e3 + 5;
pair<int, int> a[N];
char grid[N][N];
int l[N];
int r[N];
int u[N];
int d[N];
int dpr[N][N];
int dpc[N][N];
int ans = 0;
int n, k;

void solver(int p)
{
	int res = 0;
	int left = p;
	int right = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (r[i] == -1)
		{
			continue;
		}
		if (l[i] >= left && r[i] <= right)
		{
			res++;
		}
	}
	dpr[0][p] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (r[lst] != -1 && l[lst] >= left && r[lst] <= right)
		{
			res++;
		}
		if (r[i - 1] != -1 && l[i - 1] >= left && r[i - 1] <= right)
		{
			res--;
		}
		dpr[i][p] = res;
	}
}

void solver2(int p)
{
	int res = 0;
	int up = p;
	int down = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (d[i] != -1 && u[i] >= up && d[i] <= down)
		{
			res++;
		}
	}
	dpc[p][0] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (d[lst] != -1 && u[lst] >= up && d[lst] <= down)
		{
			res++;
		}
		if (d[i - 1] != -1 && u[i - 1] >= up && d[i - 1] <= down)
		{
			res--;
		}
		dpc[p][i] = res;
	}
}

int main()
{
	cin >> n >> k;
	memset(l, 127, sizeof l);
	memset(r, -1, sizeof r);
	memset(u, 127, sizeof u);
	memset(d, -1, sizeof d);
	for (int i = 0; i < n; i++) // input
	{
		for (int j =0 ; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}
	for (int i = 0; i < n; i++) // finding first and last for rows;
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] == 'B')
			{
				l[i] = min(l[i], j);
				r[i] = max(r[i], j);
			}
		}
	}
	for (int j = 0; j < n; j++) // finding first and last for columns;
	{
		for (int i = 0; i < n; i++)
		{
			if (grid[i][j] == 'B')
			{
				u[j] = min(u[j], i);
				d[j] = max(d[j], i);
			}
		}
	}
	for (int i = 0; i < n; i++) // first white rows and columns;
	{
		if (d[i] == -1)
		{
			ans++;
		}
		if (r[i] == -1)
		{
			ans++;
		}
	}
	//solver(1);
	for (int i = 0; i < n - k + 1; i++) // find the number of white rows by clicking each cell;
	{

		solver(i);
	}
	for (int i = 0; i < n - k + 1; i++) // find the number of white columns by clicking each cell;
	{

		solver2(i);
	}
	int ptr = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			ptr = max(ptr, ans + dpc[i][j] + dpr[i][j]);
		}
	}
	cout << ptr << endl;

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>

using namespace std;

const int N = 2e3 + 5;
pair<int, int> a[N];
char grid[N][N];
int l[N];
int r[N];
int u[N];
int d[N];
int dpr[N][N];
int dpc[N][N];
int ans = 0;
int n, k;

void solver(int p)
{
	int res = 0;
	int left = p;
	int right = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (r[i] == -1)
		{
			continue;
		}
		if (l[i] >= left && r[i] <= right)
		{
			res++;
		}
	}
	dpr[0][p] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (r[lst] != -1 && l[lst] >= left && r[lst] <= right)
		{
			res++;
		}
		if (r[i - 1] != -1 && l[i - 1] >= left && r[i - 1] <= right)
		{
			res--;
		}
		dpr[i][p] = res;
	}
}

void solver2(int p)
{
	int res = 0;
	int up = p;
	int down = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (d[i] != -1 && u[i] >= up && d[i] <= down)
		{
			res++;
		}
	}
	dpc[p][0] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (d[i] != -1 && u[lst] >= up && d[lst] <= down)
		{
			res++;
		}
		if (d[i - 1] != -1 && u[i - 1] >= up && d[i - 1] <= down)
		{
			res--;
		}
		dpc[p][i] = res;
	}
}

int main()
{
	cin >> n >> k;
	memset(l, 127, sizeof l);
	memset(r, -1, sizeof r);
	memset(u, 127, sizeof u);
	memset(d, -1, sizeof d);
	for (int i = 0; i < n; i++) // input
	{
		for (int j =0 ; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}
	for (int i = 0; i < n; i++) // finding first and last for rows;
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] == 'B')
			{
				l[i] = min(l[i], j);
				r[i] = max(r[i], j);
			}
		}
	}
	for (int j = 0; j < n; j++) // finding first and last for columns;
	{
		for (int i = 0; i < n; i++)
		{
			if (grid[i][j] == 'B')
			{
				u[j] = min(u[j], i);
				d[j] = max(d[j], i);
			}
		}
	}
	for (int i = 0; i < n; i++) // first white rows and columns;
	{
		if (d[i] == -1)
		{
			ans++;
		}
		if (r[i] == -1)
		{
			ans++;
		}
	}
	//solver(1);
	for (int i = 0; i < n - k + 1; i++) // find the number of white rows by clicking each cell;
	{

		solver(i);
	}
	for (int i = 0; i < n - k + 1; i++) // find the number of white columns by clicking each cell;
	{

		solver2(i);
	}
	int ptr = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			ptr = max(ptr, ans + dpc[i][j] + dpr[i][j]);
		}
	}
	cout << ptr << endl;

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>

using namespace std;

const int N = 2e3 + 5;
pair<int, int> a[N];
char grid[N][N];
int l[N];
int r[N];
int u[N];
int d[N];
int dpr[N][N];
int dpc[N][N];
int ans = 0;
int n, k;

void solver(int p)
{
	int res = 0;
	int left = p;
	int right = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (r[i] == -1)
		{
			continue;
		}
		if (l[i] >= left && r[i] <= right)
		{
			res++;
		}
	}
	dpr[0][p] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (r[lst] != -1 && l[lst] >= left && r[lst] <= right)
		{
			res++;
		}
		if (r[i - 1] != -1 && l[i - 1] >= left && r[i - 1] <= right)
		{
			res--;
		}
		dpr[i][p] = res;
	}
}

void solver2(int p)
{
	int res = 0;
	int up = p;
	int down = p + k - 1;
	for (int i = 0; i < k; i++)
	{
		if (d[i] != -1 && u[i] >= up && d[i] <= down)
		{
			res++;
		}
	}
	dpc[p][0] = res;
	for (int i = 1; i < n - k + 1; i++)
	{
		int lst = i + k - 1;
		if (d[i] != -1 && u[lst] >= up && d[lst] <= down)
		{
			res++;
		}
		if (d[i - 1] != -1 && u[i - 1] >= up && d[i - 1] <= down)
		{
			res--;
		}
		dpc[p][i] = res;
	}
}

int main()
{
	cin >> n >> k;
	memset(l, 127, sizeof l);
	memset(r, -1, sizeof r);
	memset(u, 127, sizeof u);
	memset(d, -1, sizeof d);
	for (int i = 0; i < n; i++) // input
	{
		for (int j =0 ; j < n; j++)
		{
			cin >> grid[i][j];
		}
	}
	for (int i = 0; i < n; i++) // finding first and last for rows;
	{
		for (int j = 0; j < n; j++)
		{
			if (grid[i][j] == 'B')
			{
				l[i] = min(l[i], j);
				r[i] = max(r[i], j);
			}
		}
	}
	for (int j = 0; j < n; j++) // finding first and last for columns;
	{
		for (int i = 0; i < n; i++)
		{
			if (grid[i][j] == 'B')
			{
				u[j] = min(u[j], i);
				d[j] = max(d[j], i);
			}
		}
	}
	for (int i = 0; i < n; i++) // first white rows and columns;
	{
		if (d[i] == -1)
		{
			ans++;
		}
		if (r[i] == -1)
		{
			ans++;
		}
	}
	//solver(1);
	for (int i = 0; i < n - k + 1; i++) // find the number of white rows by clicking each cell;
	{

		solver(i);
	}
	for (int i = 0; i < n - k + 1; i++) // find the number of white columns by clicking each cell;
	{

		solver2(i);
	}
	int ptr = 0;
	solver(2);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			ptr = max(ptr, ans + dpc[i][j] + dpr[i][j]);
		}
	}
	cout << ptr << endl;

}


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

//#define ONLINE_JUDGE
#define pb(a) push_back(a)
#define ALL(a) a.begin(), a.end()

using ll = long long;

///////////// Solution /////////////////////////////////

const int MAX = 1e6;

char t[2000][2000];
int n,k;

int rows[2000][2000], cols[2000][2000];

int check_row(int i, int j) {
	if (rows[i][n-1] == 0)
		return 0;

	return ((rows[i][j+k-1]-(j==0 ? 0 : rows[i][j-1])) == rows[i][n-1]);
}

int check_col(int i, int j) {
	if (cols[j][n-1] == 0) {
		return 0;
	}

	return ((cols[j][i+k-1]-(i==0 ? 0 : cols[j][i-1])) == cols[j][n-1]);
}

void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> t[i][j];
		}
	}

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		rows[i][0] = (t[i][0] == 'B');
		for (int j = 1; j < n; ++j) {
			rows[i][j] = rows[i][j-1];
			if (t[i][j] == 'B') {
				rows[i][j]++;
			}
		}
		ans += (rows[i][n-1]==0);
	}

	for (int j = 0; j < n; ++j) {
		cols[j][0] = (t[0][j] == 'B');
		for (int i = 1; i < n; ++i) {
			cols[j][i] = cols[j][i-1];
			if (t[i][j] == 'B') {
				cols[j][i]++;
			}
		}
		ans += (cols[j][n-1]==0);
	}
	int mx = 0;
	for (int i = 0; i+k-1 < n; ++i) {
		for (int j = 0; j+k-1 < n; ++j) {
			int cur = 0;
			for (int l = 0; l < k; ++l) {
				cur += check_row(i+l,j);
				cur += check_col(i,j+l);
			}
			mx = max(mx,cur);
		}
	}

	ans += mx;
	cout << ans << '\n';
}

	/////////// End Solution ///////////////////////////////////////

	//////// Initialization ////////////////////////////////////

int main() {

	#ifndef ONLINE_JUDGE
	FILE *FIN = freopen("/home/danil/prog/input.txt", "r", stdin);
	clock_t time_start = clock();
	#endif

	ios::sync_with_stdio(false);
	cin.tie(NULL);

	solve();

	#ifndef ONLINE_JUDGE
	fclose(FIN);
	cerr << "\x1b[031m\n-----------------\nTime=" << (ll)((double)(clock()-time_start) / CLOCKS_PER_SEC * 1000) << "ms\n\x1b[0m";
	#endif

	return 0;
}

	//////// End Initialization ///////////////////////////////////////


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

//#define ONLINE_JUDGE
#define pb(a) push_back(a)
#define ALL(a) a.begin(), a.end()

using ll = long long;

///////////// Solution /////////////////////////////////

const int MAX = 1e6;

char t[2000][2000];
int n,k;

int rows[2000][2000], cols[2000][2000];

int check_row(int i, int j) {
	if (rows[i][n-1] == 0)
		return 0;

	return ((rows[i][j+k-1]-(j==0 ? 0 : rows[i][j-1])) == rows[i][n-1]);
}

int check_col(int i, int j) {
	if (cols[j][n-1] == 0) {
		return 0;
	}

	return ((cols[j][i+k-1]-(i==0 ? 0 : cols[j][i-1])) == cols[j][n-1]);
}

void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> t[i][j];
		}
	}

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		rows[i][0] = (t[i][0] == 'B');
		for (int j = 1; j < n; ++j) {
			rows[i][j] = rows[i][j-1];
			if (t[i][j] == 'B') {
				rows[i][j]++;
			}
		}
		ans += (rows[i][n-1]==0);
	}

	for (int j = 0; j < n; ++j) {
		cols[j][0] = (t[0][j] == 'B');
		for (int i = 1; i < n; ++i) {
			cols[j][i] = cols[j][i-1];
			if (t[i][j] == 'B') {
				cols[j][i]++;
			}
		}
		ans += (cols[j][n-1]==0);
	}
	int mx = 0;
	for (int i = 0; i+k-1 < n; ++i) {
		for (int j = 0; j+k-1 < n; ++j) {
			int cur = check_row(i,j)+check_row(i+1,j)+
								check_col(i,j)+check_col(i,j+1);
			mx = max(mx,cur);
		}
	}

	ans += mx;
	cout << ans << '\n';
}

	/////////// End Solution ///////////////////////////////////////

	//////// Initialization ////////////////////////////////////

int main() {

	#ifndef ONLINE_JUDGE
	FILE *FIN = freopen("/home/danil/prog/input.txt", "r", stdin);
	clock_t time_start = clock();
	#endif

	ios::sync_with_stdio(false);
	cin.tie(NULL);

	solve();

	#ifndef ONLINE_JUDGE
	fclose(FIN);
	cerr << "\x1b[031m\n-----------------\nTime=" << (ll)((double)(clock()-time_start) / CLOCKS_PER_SEC * 1000) << "ms\n\x1b[0m";
	#endif

	return 0;
}

	//////// End Initialization ///////////////////////////////////////


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

//#define ONLINE_JUDGE
#define pb(a) push_back(a)
#define ALL(a) a.begin(), a.end()

using ll = long long;

///////////// Solution /////////////////////////////////

const int MAX = 1e6;

char t[2000][2000];
int n,k;

int rows[2000][2000], cols[2000][2000];

int check_row(int i, int j) {
	if (rows[i][n-1] == 0)
		return 0;

	return ((rows[i][j+k-1]-(j==0 ? 0 : rows[i][j-1])) == rows[i][n-1]);
}

int check_col(int i, int j) {
	if (cols[j][n-1] == 0) {
		return 0;
	}

	return ((cols[j][i+k-1]-(i==0 ? 0 : cols[j][i-1])) == cols[i][n-1]);
}

void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> t[i][j];
		}
	}

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		rows[i][0] = (t[i][0] == 'B');
		for (int j = 1; j < n; ++j) {
			rows[i][j] = rows[i][j-1];
			if (t[i][j] == 'B') {
				rows[i][j]++;
			}
		}
		ans += (rows[i][n-1]==0);
	}

	for (int j = 0; j < n; ++j) {
		cols[j][0] = (t[0][j] == 'B');
		for (int i = 1; i < n; ++i) {
			cols[j][i] = cols[j][i-1];
			if (t[i][j] == 'B') {
				cols[j][i]++;
			}
		}
		ans += (cols[j][n-1]==0);
	}
	int mx = 0;
	for (int i = 0; i+k-1 < n; ++i) {
		for (int j = 0; j+k-1 < n; ++j) {
			int cur = check_row(i,j)+check_row(i+1,j)+
								check_col(i,j)+check_col(i,j+1);
			mx = max(mx,cur);
		}
	}

	ans += mx;
	cout << ans << '\n';
}

	/////////// End Solution ///////////////////////////////////////

	//////// Initialization ////////////////////////////////////

int main() {

	#ifndef ONLINE_JUDGE
	FILE *FIN = freopen("/home/danil/prog/input.txt", "r", stdin);
	clock_t time_start = clock();
	#endif

	ios::sync_with_stdio(false);
	cin.tie(NULL);

	solve();

	#ifndef ONLINE_JUDGE
	fclose(FIN);
	cerr << "\x1b[031m\n-----------------\nTime=" << (ll)((double)(clock()-time_start) / CLOCKS_PER_SEC * 1000) << "ms\n\x1b[0m";
	#endif

	return 0;
}

	//////// End Initialization ///////////////////////////////////////


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#pragma GCC optimize ("O3")
using namespace std;
int main () {
    #ifdef LOCAL
        freopen ("input.txt", "r", stdin);
        freopen ("output.txt", "w", stdout);
        freopen ("error.txt", "w", stderr);
    #else
        ios::sync_with_stdio(false);
        cin.tie (NULL);
    #endif
    int n, k; cin>>n>>k;
    char a[n][n];
    vector <pair<int, int>> row (n), col (n);
    int ans=0, add=0;
    for (int i=0;i<n;++i) {
        cin>>a[i];
        int l=-1, r=-1;
        for (int j=0;j<n;++j) {
            if (a[i][j]=='B') {
                if (l==-1) l=j;
                r=j;
            }
        }
        row[i]={l, r};
        if (l==-1 && r==-1) ans++;
    }
    for (int i=0;i<n;++i) {
        int l=-1, r=-1;
        for (int j=0;j<n;++j) {
            if (a[j][i]=='B') {
                if (l==-1) l=j;
                r=j;
            }
        }
        col[i]={l, r};
        if (l==-1 && r==-1) ans++;
    }
    for (int i=0;i<n-k+1;++i) {
        for (int j=0;j<n-k+1;++j) {
            int temp=0;
            for (int p=i;p<i+k;++p) {
                if (row[p].first>=j && row[p].second<=j+k-1) temp++;
            }
            for (int p=j;p<j+k;++p) {
                if (col[p].first>=i && col[p].second<=i+k-1) temp++;
            }
            add=max(add, temp);
        }
    }
    cout<<ans+add;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#pragma GCC optimize ("O3")
using namespace std;
int main () {
    #ifdef LOCAL
        freopen ("input.txt", "r", stdin);
        freopen ("output.txt", "w", stdout);
        freopen ("error.txt", "w", stderr);
    #else
        ios::sync_with_stdio(false);
        cin.tie (NULL);
    #endif
    int n, k; cin>>n>>k;
    char a[n][n];
    vector <pair<int, int>> row (n), col (n);
    int ans=0, add=0;
    for (int i=0;i<n;++i) {
        cin>>a[i];
        int l=-1, r=-1;
        for (int j=0;j<n;++j) {
            if (a[i][j]=='B') {
                if (l==-1) l=j;
                r=j;
            }
        }
        row[i]={l, r};
        if (l==-1 && r==-1) ans++;
    }
    for (int i=0;i<n;++i) {
        int l=-1, r=-1;
        for (int j=0;j<n;++j) {
            if (a[j][i]=='B') {
                if (l==-1) l=j;
                r=j;
            }
        }
        col[i]={l, r};
        if (l==-1 && r==-1) ans++;
    }
    int ver[n-k+1][n-k+1], hor[n-k+1][n-k+1];
    for (int i=0;i<n-k+1;++i) {
        int cnt=0;
        for (int j=0;j<k;++j) {
            if (row[j].first>=i && row[j].second<=i+k-1) cnt++;
        }
        ver[0][i]=cnt;
        for (int j=1;j<n-k+1;++j) {
            ver[j][i]=ver[j-1][i];
            if (row[j-1].first>=i && row[j-1].second<=i+k-1) ver[j][i]--;
            if (row[j+k-1].first>=i && row[j+k-1].second<=i+k-1) ver[j][i]++;
        }
    }
    for (int i=0;i<n-k+1;++i) {
        int cnt=0;
        for (int j=0;j<k;++j) {
            if (col[j].first>=i && col[j].second<=i+k-1) cnt++;
        }
        hor[i][0]=cnt;
        for (int j=1;j<n-k+1;++j) {
            hor[i][j]=hor[i][j-1];
            if (col[j-1].first>=i && col[j-1].second<=i+k-1) hor[i][j]--;
            if (col[j+k-1].first>=i && col[j+k-1].second<=i+k-1) hor[i][j]++;
        }
    }
    for (int i=0;i<n-k+1;++i) {
        for (int j=0;j<n-k+1;++j) add=max(add, ver[i][j]+hor[i][j]);
    }
    cout<<ans+add;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#pragma GCC optimize ("O3")
using namespace std;
int main () {
    #ifdef LOCAL
        freopen ("input.txt", "r", stdin);
        freopen ("output.txt", "w", stdout);
        freopen ("error.txt", "w", stderr);
    #else
        ios::sync_with_stdio(false);
        cin.tie (NULL);
    #endif
    ll n, k; cin>>n>>k;
    vector <ll> row (n), col (n);
    map <ll, ll> cnt_r, cnt_c;
    char a[n][n];
    for (ll i=0;i<n;++i) {
        cin>>a[i];
        ll sum=0;
        for (ll j=0;j<n;++j) {
            if (a[i][j]=='W') sum++;
        }
        row[i]=sum;
        cnt_r[sum]++;
    }
    for (ll i=0;i<n;++i) {
        ll sum=0;
        for (ll j=0;j<n;++j) {
            if (a[j][i]=='W') sum++;
        }
        col[i]=sum;
        cnt_c[sum]++;
    }
    ll ans=cnt_r[n]+cnt_c[n];
    for (ll i=0;i<n-k+1;++i) {
        for (ll j=0;j<n-k+1;++j) {
            vector <ll> R=row, C=col;
            map <ll, ll> c1=cnt_r, c2=cnt_c;
            for (ll p=i;p<i+k;++p) {
                for (ll q=j;q<j+k;++q) {
                    if (a[p][q]=='B') {
                        c1[R[p]]--;
                        c2[C[q]]--;
                        R[p]++;
                        C[q]++;
                        c1[R[p]]++;
                        c2[C[q]]++;
                    }
                }
            }
            ans=max(ans, c1[n]+c2[n]);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#pragma GCC optimize ("O3")
using namespace std;
int main () {
    #ifdef LOCAL
        freopen ("input.txt", "r", stdin);
        freopen ("output.txt", "w", stdout);
        freopen ("error.txt", "w", stderr);
    #else
        ios::sync_with_stdio(false);
        cin.tie (NULL);
    #endif
    ll n, k; cin>>n>>k;
    vector <ll> row (n), col (n);
    map <ll, ll> cnt_r, cnt_c;
    char a[n][n];
    for (ll i=0;i<n;++i) {
        cin>>a[i];
        ll sum=0;
        for (ll j=0;j<n;++j) {
            if (a[i][j]=='W') sum++;
        }
        row[i]=sum;
        cnt_r[sum]++;
    }
    for (ll i=0;i<n;++i) {
        ll sum=0;
        for (ll j=0;j<n;++j) {
            if (a[j][i]=='W') sum++;
        }
        col[i]=sum;
        cnt_c[sum]++;
    }
    ll ans=cnt_r[n]+cnt_c[n], sum=0;
    for (ll i=0;i<n-k+1;++i) {
        for (ll j=0;j<n-k+1;++j) {
            ll temp1[k], temp2[k];
            memset (temp1, 0, sizeof temp1);
            memset (temp2, 0, sizeof temp2);
            for (ll p=i;p<i+k;++p) {
                for (ll q=j;q<j+k;++q) {
                    if (a[p][q]=='B') {
                        temp1[p-i]++;
                        temp2[q-j]++;
                    }
                }
            }
            ll add=0;
            for (ll p=i;p<i+k;++p) {
                if (temp1[p-i] && row[p]+temp1[p-i]==n) add++;
            }
            for (ll q=j;q<j+k;++q) {
                if (temp2[q-j] && col[q]+temp2[q-j]==n) add++;
            }
            sum=max(sum, add);
        }
    }
    cout<<ans+sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#pragma GCC optimize ("O3")
using namespace std;
int main () {
    #ifdef LOCAL
        freopen ("input.txt", "r", stdin);
        freopen ("output.txt", "w", stdout);
        freopen ("error.txt", "w", stderr);
    #else
        ios::sync_with_stdio(false);
        cin.tie (NULL);
    #endif
    int n, k; cin>>n>>k;
    char a[n+1][n+1];
    int row[n+1][n+1], col[n+1][n+1], ans=0;
    for (int i=0;i<=n;++i) row[i][0]=row[0][i]=col[i][0]=col[0][i]=0;
    for (int i=1;i<=n;++i) {
        for (int j=1;j<=n;++j) cin>>a[i][j];
        for (int j=1;j<=n;++j) {
            if (a[i][j]=='W') row[i][j]=row[i][j-1]+1;
            else row[i][j]=row[i][j-1];
        }
        if (row[i][n]==n) ans++;
    }
    for (int i=1;i<=n;++i) {
        for (int j=1;j<=n;++j) {
            if (a[j][i]=='W') col[i][j]=col[i][j-1]+1;
            else col[i][j]=col[i][j-1];
        }
        if (col[i][n]==n) ans++;
    }
    int add=0;
    for (int i=1;i<=n-k+1;++i) {
        for (int j=1;j<=n-k+1;++j) {
            int temp=0;
            for (int p=i;p<i+k;++p) {
                if (row[p][n]!=n) {
                    if (row[p][n]-(row[p][j+k-1]-row[p][j-1])+k==n) temp++;
                }
            }
            for (int p=j;p<j+k;++p) {
                if (col[p][n]!=n) {
                    if (col[p][n]-(col[p][i+k-1]-col[p][i-1])+k==n) temp++;
                }
            }
            add=max(add, temp);
        }
    }
    cout<<ans+add;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,k;
char a[2011][2011];
int valid_row[2011]; // prefix_sum_r
int valid_col[2011]; // prefix_sum_c
bool is_validr[2011];
bool is_validc[2011];
int f[2011][2011];

int inc[2011][2011];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin >> a[i][j];
	if (n == 1)
	{
		cout << 2;
		return 0;
	}
	for (int i=1;i<=n;i++)
	{
		bool ok = true;
		for (int j=1;j<=n;j++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_row[i] = valid_row[i-1];
		if (ok)
		{
			is_validr[i] = true;
			valid_row[i]++;
		}
	}
	for (int j=1;j<=n;j++)
	{
		bool ok = true;
		for (int i=1;i<=n;i++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_col[j] = valid_col[j-1];
		if (ok)
		{
			is_validc[j] = true;
			valid_col[j]++;
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (is_validr[i])
			continue;
		for (int j=1;j<=n;j++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=j+k;l<=n;l++)
					if (a[i][l] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=j+k-1;l>=j;l--)
					if (a[i][l] == 'B')
					{
						d = l;
						break;
					}
				d -= j;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k+d,0)]--;
				inc[max(i-k,0)][max(j-k+d,0)]++;
				break;
			}
		}
	}
	for (int j=1;j<=n;j++)
	{
		if (is_validc[j])
			continue;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=i+k;l<=n;l++)
					if (a[l][j] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=i+k-1;l>=i;l--)
					if (a[l][j] == 'B')
					{
						d = l;
						break;
					}
				d -= i;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k+d,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k+d,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int i=n;i>=1;i--)
	{
		for (int j=n;j>=1;j--)
		{
			f[i][j] = f[i+1][j] + f[i][j+1] - f[i+1][j+1] + inc[i][j];
			cerr << f[i][j];
		}
		cerr << '\n';
	}
	int ans = 0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans = max(ans,f[i][j]);
	ans += valid_row[n] + valid_col[n];
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

int n,k;
char a[2011][2011];
int valid_row[2011]; // prefix_sum_r
int valid_col[2011]; // prefix_sum_c
bool is_validr[2011];
bool is_validc[2011];
int f[2011][2011];

int inc[2011][2011];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin >> a[i][j];
	if (n == 1)
	{
		if (a[1][1] == 'W')
			cout << 1;
		else if (k == 1)
			cout << 1;
		else
			cout << 0;
		return 0;
	}
	for (int i=1;i<=n;i++)
	{
		bool ok = true;
		for (int j=1;j<=n;j++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_row[i] = valid_row[i-1];
		if (ok)
		{
			is_validr[i] = true;
			valid_row[i]++;
		}
	}
	for (int j=1;j<=n;j++)
	{
		bool ok = true;
		for (int i=1;i<=n;i++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_col[j] = valid_col[j-1];
		if (ok)
		{
			is_validc[j] = true;
			valid_col[j]++;
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (is_validr[i])
			continue;
		for (int j=1;j<=n;j++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=j+k;l<=n;l++)
					if (a[i][l] == 'B')
					{
						ok = false;
						break;
					}
				if (!ok)
					break;
				//cerr << i << ' ' << j << '\n';
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int j=1;j<=n;j++)
	{
		if (is_validc[j])
			continue;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=i+k;l<=n;l++)
					if (a[l][j] == 'B')
					{
						ok = false;
						break;
					}
				if (!ok)
					break;
				//cerr << i << ' ' << j << '\n';;
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int i=n;i>=1;i--)
		for (int j=n;j>=1;j--)
			f[i][j] = f[i+1][j] + f[i][j+1] - f[i+1][j+1] + inc[i][j];
	int ans = 0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans = max(ans,f[i][j]);
	ans += valid_row[n] + valid_col[n];
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

int n,k;
char a[2011][2011];
int valid_row[2011]; // prefix_sum_r
int valid_col[2011]; // prefix_sum_c
bool is_validr[2011];
bool is_validc[2011];
int f[2011][2011];

int inc[2011][2011];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin >> a[i][j];
	if (n == 1)
	{
		cout << 2;
		return 0;
	}
	for (int i=1;i<=n;i++)
	{
		bool ok = true;
		for (int j=1;j<=n;j++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_row[i] = valid_row[i-1];
		if (ok)
		{
			is_validr[i] = true;
			valid_row[i]++;
		}
	}
	for (int j=1;j<=n;j++)
	{
		bool ok = true;
		for (int i=1;i<=n;i++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_col[j] = valid_col[j-1];
		if (ok)
		{
			is_validc[j] = true;
			valid_col[j]++;
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (is_validr[i])
			continue;
		for (int j=1;j<=n;j++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=j+k;l<=n;l++)
					if (a[i][l] == 'B')
					{
						ok = false;
						break;
					}
				if (!ok)
					break;
				//cerr << i << ' ' << j << '\n';
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int j=1;j<=n;j++)
	{
		if (is_validc[j])
			continue;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=i+k;l<=n;l++)
					if (a[l][j] == 'B')
					{
						ok = false;
						break;
					}
				if (!ok)
					break;
				//cerr << i << ' ' << j << '\n';;
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int i=n;i>=1;i--)
		for (int j=n;j>=1;j--)
			f[i][j] = f[i+1][j] + f[i][j+1] - f[i+1][j+1] + inc[i][j];
	int ans = 0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans = max(ans,f[i][j]);
	ans += valid_row[n] + valid_col[n];
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

int n,k;
char a[2011][2011];
int valid_row[2011]; // prefix_sum_r
int valid_col[2011]; // prefix_sum_c
bool is_validr[2011];
bool is_validc[2011];
int f[2011][2011];

int inc[2011][2011];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin >> a[i][j];
	if (n == 1)
	{
		cout << 2;
		return 0;
	}
	for (int i=1;i<=n;i++)
	{
		bool ok = true;
		for (int j=1;j<=n;j++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_row[i] = valid_row[i-1];
		if (ok)
		{
			is_validr[i] = true;
			valid_row[i]++;
		}
	}
	for (int j=1;j<=n;j++)
	{
		bool ok = true;
		for (int i=1;i<=n;i++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_col[j] = valid_col[j-1];
		if (ok)
		{
			is_validc[j] = true;
			valid_col[j]++;
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (is_validr[i])
			continue;
		for (int j=1;j<=n;j++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=j+k;l<=n;l++)
					if (a[i][l] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=j+k-1;l>=j;l--)
					if (a[i][l] == 'B')
					{
						d = l;
						break;
					}
				d -= j;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k+d,0)]--;
				inc[max(i-k,0)][max(j-k+d,0)]++;
				break;
			}
		}
	}
	for (int j=1;j<=n;j++)
	{
		if (is_validc[j])
			continue;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=i+k;l<=n;l++)
					if (a[l][j] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=i+k-1;l>=i;l--)
					if (a[l][j] == 'B')
					{
						d = l;
						break;
					}
				d -= i;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k+d,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k+d,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int i=n;i>=1;i--)
		for (int j=n;j>=1;j--)
			f[i][j] = f[i+1][j] + f[i][j+1] - f[i+1][j+1] + inc[i][j];
	int ans = 0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans = max(ans,f[i][j]);
	ans += valid_row[n] + valid_col[n];
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

int n,k;
char a[2011][2011];
int valid_row[2011]; // prefix_sum_r
int valid_col[2011]; // prefix_sum_c
bool is_validr[2011];
bool is_validc[2011];
int f[2011][2011];

int inc[2011][2011];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			cin >> a[i][j];
	if (n == 1)
	{
		cout << 2;
		return 0;
	}
	for (int i=1;i<=n;i++)
	{
		bool ok = true;
		for (int j=1;j<=n;j++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_row[i] = valid_row[i-1];
		if (ok)
		{
			is_validr[i] = true;
			valid_row[i]++;
		}
	}
	for (int j=1;j<=n;j++)
	{
		bool ok = true;
		for (int i=1;i<=n;i++)
			if (a[i][j] == 'B')
			{
				ok = false;
				break;
			}
		valid_col[j] = valid_col[j-1];
		if (ok)
		{
			is_validc[j] = true;
			valid_col[j]++;
		}
	}
	for (int i=1;i<=n;i++)
	{
		if (is_validr[i])
			continue;
		for (int j=1;j<=n;j++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=j+k;l<=n;l++)
					if (a[i][l] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=min(j+k-1,n);l>=j;l--)
					if (a[i][l] == 'B')
					{
						d = l;
						break;
					}
				d -= j;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k,0)][j]--;
				inc[i][max(j-k+d,0)]--;
				inc[max(i-k,0)][max(j-k+d,0)]++;
				break;
			}
		}
	}
	for (int j=1;j<=n;j++)
	{
		if (is_validc[j])
			continue;
		for (int i=1;i<=n;i++)
		{
			if (a[i][j] == 'B')
			{
				bool ok = true;
				for (int l=i+k;l<=n;l++)
					if (a[l][j] == 'B')
					{
						ok = false;
						break;
					}
				int d;
				for (int l=min(i+k-1,n);l>=i;l--)
					if (a[l][j] == 'B')
					{
						d = l;
						break;
					}
				d -= i;
				if (!ok)
					break;
				inc[i][j]++;
				inc[max(i-k+d,0)][j]--;
				inc[i][max(j-k,0)]--;
				inc[max(i-k+d,0)][max(j-k,0)]++;
				break;
			}
		}
	}
	for (int i=n;i>=1;i--)
		for (int j=n;j>=1;j--)
			f[i][j] = f[i+1][j] + f[i][j+1] - f[i+1][j+1] + inc[i][j];
	int ans = 0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			ans = max(ans,f[i][j]);
	ans += valid_row[n] + valid_col[n];
	cout << ans;
}


#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;
}

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(), curr_lines, max_lines = 0;

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

    VVI lines_c = process(cb, false);
    VVI lines_r = process(rb, true);
    // 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;
}