void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int h, w; char s, t; rd(h, w, s, t);
  auto c=new char[][](h, w);
  foreach(i; 0..h) c[i]=readln.chomp.to!(char[]);

  const int n=26;
  auto d=new int[][](n, n);
  foreach(i; 0..n) fill(d[i], 1_000_000);

  auto dir=[[1, 0], [-1, 0], [0, 1], [0, -1]];
  bool A(char ch){return ('A'<=ch && ch<='Z');}
  foreach(int i; 0..h)foreach(int j; 0..w){
    if(A(c[i][j])==false) continue;
    d[c[i][j]-'A'][c[i][j]-'A']=0;
    foreach(dd; dir){
      int ni=i+dd[0]*2, nj=j+dd[1]*2;
      if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
      if(((dd[0]!=0 && c[ni][nj]=='|')||(dd[1]!=0 && c[ni][nj]=='-'))==false) continue;
      while((ni>=0 && ni<h && nj>=0 && nj<w) && A(c[ni][nj])==false){
        ni+=dd[0]; nj+=dd[1];
      }
      if((ni>=0 && ni<h && nj>=0 && nj<w)){
        int x=c[i][j]-'A', y=c[ni][nj]-'A';
        // writeln(c[i][j], " ", c[ni][nj]);
        d[x][y]=d[y][x]=1;
      }
    }
  }

  foreach(_; 0..n)foreach(i; 0..n)foreach(j; 0..n){
    d[i][j]=min(d[i][j], d[i][_]+d[_][j]);
  }

  writeln(d[s-'A'][t-'A']);
}

void rd(Type...)(ref Type x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
