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[]);

  int si, sj, gi, gj;
  foreach(int i; 0..h)foreach(int j; 0..w){
    if(c[i][j]==s) si=i, sj=j;
    if(c[i][j]==t) gi=i, gj=j;
  }

  auto dir=[[1, 0], [-1, 0], [0, 1], [0, -1]];

  auto d=new int[][](h, w);
  foreach(i; 0..h) fill(d[i], 1_000_000);
  d[si][sj]=0;
  import std.typecons;
  alias P=Tuple!(int, "i", int, "j");
  auto q=new Queue!(P)(h*w);
  q.insertBack(P(si, sj));
  while(q.empty==false){
    auto cur=q.front; q.removeFront;
    foreach(dd; dir){
      auto ni=cur.i+dd[0]*2, nj=cur.j+dd[1]*2;
      if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
      if(c[ni][nj]=='.') continue;
      while(('A'<=c[ni][nj] && c[ni][nj]<='Z')==false){
        ni+=dd[0]; nj+=dd[1];
      }
      if(d[cur.i][cur.j]+1<d[ni][nj]){
        d[ni][nj]=d[cur.i][cur.j]+1;
        q.insertBack(P(ni, nj));
      }
    }
  }

  writeln(d[gi][gj]);
}

class Queue(T){
private:
  int n, l=0, r=0;
  T[] arr;

public:
  this(int size){
    n=size+1;
    arr.length=n;
  }
  bool empty(){
    return l==r;
  }
  bool full(){
    return l==(r+1)%n;
  }
  T front(){
    return arr[l];
  }
  void insertBack(T x){
    assert(full==false);
    arr[r]=x;
    (r+=1)%=n;
  }
  void removeFront(){
    assert(empty==false);
    (l+=1)%=n;
  }
}

void rd(T...)(ref T 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));
  }
}

