using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
    static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Program().Solve();
        Console.Out.Flush();
    }
    Scanner cin = new Scanner();

    int H, W;
    void Solve()
    {
        var dd = new[] { 0, 1, 0, -1, 0 };
        H = cin.Nextint;
        W = cin.Nextint;
        int S = cin.Next[0] - 'A';
        int T = cin.Next[0] - 'A';
        input(26, 26);

        var C = new string[H];
        for (int i = 0; i < H; i++)
        {
            C[i] = cin.Next;
        }
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if ('A' <= C[i][j] && C[i][j] <= 'Z')
                {
                    for (int r = 0; r < 4; r++)
                    {
                        int y = i + dd[r] * 2;
                        int x = j + dd[r + 1] * 2;
                        while (0 <= y && y < H && 0 <= x && x < W)
                        {
                            if (r % 2 == 0)
                            {
                                if (C[y][x] == '-')
                                {
                                    y += dd[r];
                                    x += dd[r + 1];
                                }
                                else if (C[y][x] == 'o')
                                {
                                    y += dd[r];
                                    x += dd[r + 1];
                                    D[C[i][j] - 'A', C[y][x] - 'A'] = 1;
                                    D[C[y][x] - 'A', C[i][j] - 'A'] = 1;
                                    break;
                                }
                                else break;
                            }
                            else
                            {
                                if (C[y][x] == '|')
                                {
                                    y += dd[r];
                                    x += dd[r + 1];
                                }
                                else if (C[y][x] == 'o')
                                {
                                    y += dd[r];
                                    x += dd[r + 1];
                                    D[C[i][j] - 'A', C[y][x] - 'A'] = 1;
                                    D[C[y][x] - 'A', C[i][j] - 'A'] = 1;
                                    break;
                                }
                                else break;
                            }
                        }
                    }
                }
            }
        }
        warshall_floyd(26);
        Console.WriteLine(D[S, T]);

    }

    long[,] D;
    const long inf = long.MaxValue / 3;
    void input(int n, int m)
    {
        D = new long[n, n];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                D[i, j] = inf;
                if (i == j) D[i, j] = 0;
            }
        }
        //for (int i = 0; i < m; i++)
        //{
        //    int a = cin.nextint - 1;
        //    int b = cin.nextint - 1;
        //    long c = cin.nextlong;
        //    D[a, b] = Math.Min(D[a, b], c);
        //    D[b, a] = c;
        //}
    }
    void warshall_floyd(int n)
    {
        for (int k = 0; k < n; k++)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    D[i, j] = Math.Min(D[i, j], D[i, k] + D[k, j]);
                }
            }
        }
    }
}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] Scan { get { return Console.ReadLine().Split(); } }
    public int[] Scanint { get { return Array.ConvertAll(Scan, int.Parse); } }
    public long[] Scanlong { get { return Array.ConvertAll(Scan, long.Parse); } }
    public double[] Scandouble { get { return Array.ConvertAll(Scan, double.Parse); } }
    public string Next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return Next;
        }
    }
    public int Nextint { get { return int.Parse(Next); } }
    public long Nextlong { get { return long.Parse(Next); } }
    public double Nextdouble { get { return double.Parse(Next); } }
}


Measured memory_bytes: 9204