#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,stepx,stepy;
int a[35][35];
int vis[35][35];
int ans=0x3fffffff;
int posx,posy;
void dfs(int x,int y,int step)
{
if(x<1||y<1||x>n||y>m){
return;
}
if(step>ans||step>n*m){
return;
}
if(a[x][y]==0||a[x][y]==2){
return;
}
if(vis[x][y]==1){
return;
}
if(a[x][y]==4){
ans=min(ans,step);
return;
}
vis[x][y]=1;
dfs(x+stepx,y+stepy,step+1);
dfs(x-stepx,y+stepy,step+1);
dfs(x+stepx,y-stepy,step+1);
dfs(x-stepx,y-stepy,step+1);
dfs(x+stepy,y+stepx,step+1);
dfs(x-stepy,y+stepx,step+1);
dfs(x+stepy,y-stepx,step+1);
dfs(x-stepy,y-stepx,step+1);
vis[x][y]=0;
return;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>stepx>>stepy;
for(int i=1;i<=n;i++){
for(int i1=1;i1<=m;i1++){
cin>>a[i][i1];
if(a[i][i1]==3){
posx=i;
posy=i1;
}
}
}
dfs(posx,posy,0);
cout<<ans;
return 0;
}