#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int h,w,d,r,xx[5]={0,-1,1,0,0},yy[5]={0,0,0,-1,1},vis[1010][1010][2];
char s[1010][1010];
struct node
{
int x,y,s;
};
queue<node> q;
void bfs()
{
while(!q.empty()&&vis[h][w][0]==-1&&vis[h][w][1]==-1)
{
node t=q.front();q.pop();
for(int i=1;i<=4;i++)
{
int x=t.x+xx[i],y=t.y+yy[i];
if(s[x][y]=='.'&&x>=1&&x<=h&&y>=1&&y<=w&&vis[h][w][t.s]==-1)
{
node k;k.x=x,k.y=y;k.s=t.s;
q.push(k);vis[x][y][t.s]=vis[t.x][t.y][t.s]+1;
if(t.s==0&&s[x+d][y+r]=='.'&&x+d>=1&&x+d<=h&&y+r>=1&&y+r<=w&&vis[x+d][y+r][1]==-1)
{
node z;z.x=x+d;z.y=y+r;z.s=1;q.push(z);
vis[x+d][y+r][1]=vis[x][y][0]+1;
}
}
}
}
}
int main()
{
cin>>h>>w>>d>>r;
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++) cin>>s[i][j];
memset(vis,-1,sizeof(vis));
vis[1][1][0]=0;node k;k.x=1;k.y=1;k.s=0;
q.push(k);
bfs();
if(vis[h][w][0]==-1&&vis[h][w][1]==-1) cout<<-1;
else
{
int a;
if(vis[h][w][0]==-1) a=1<<20;
else a=vis[h][w][0];
int b;
if(vis[h][w][1]==-1) b=1<<20;
else b=vis[h][w][1];
cout<<min(a,b);
}
return 0;
}