#include <bits/stdc++.h>
using namespace std;
int h , w , d , r , cnt , dis[2005][2005];
char c[2005][2005];
bool vis[2005][1005] , go[2005][2005];
struct node{
int x , y;
bool type;
};
int dx[4] = {0 , 0 , 1 , -1} , dy[4] = {1 , -1 , 0 , 0};
bool range_point(int x , int y){
if(x < 1 || y < 1) return 0;
if(x > h || y > w) return 0;
return 1;
}
void flood_file(int sx , int sy){
queue <node> q;
q.push((node){sx , sy , 0});
while(!q.empty()){
node now = q.front();
q.pop();
int x = now.x , y = now.y;
for(int i = 0; i < 4; i++){
int nx = x + dx[i] , ny = y + dy[i];
if(range_point(nx , ny) && !vis[nx][ny] && c[nx][ny] == '.'){
q.push((node){nx , ny , 0});
vis[nx][ny] = true;
}
}
}
return;
}
void Bfs(){
queue <node> q;
go[1][1] = true;
q.push((node){1 , 1 , 0});
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if(range_point(nx , ny) && !go[nx][ny] && c[nx][ny] == '.'){
go[nx][ny] = true;
q.push((node){nx , ny , 0});
}
}
}
return;
}
void bfs(){
queue <node> q;
memset(dis , -1 , sizeof(dis));
dis[1][1] = 0;
q.push((node){1 , 1 , 0});
while(!q.empty()){
node now = q.front();
q.pop();
if(now.x == h && now.y == w){
cout << dis[h][w];
return;
}
for(int i = 0; i < 4; i++){
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if(range_point(nx , ny) && dis[nx][ny] == -1 && c[nx][ny] == '.'){
dis[nx][ny] = dis[now.x][now.y] + 1;
q.push((node){nx , ny , now.type});
}
}
bool type = now.type;
int nx = now.x + d;
int ny = now.y + r;
if(!type && (!go[nx][ny] || cnt == 1) && (d >= 0 || r >= 0)){
if(range_point(nx , ny) && dis[nx][ny] == -1 && c[nx][ny] == '.'){
dis[nx][ny] = dis[now.x][now.y] + 1;
q.push((node){nx , ny , 1});
}
}
}
cout << -1;
return;
}
int main(){
cin >> h >> w >> d >> r;
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++)
cin >> c[i][j];
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
if(c[i][j] == '.' && !vis[i][j]){
flood_file(i , j);
vis[i][j] = true;
cnt++;
}
}
}
Bfs();
bfs();
return 0;
}