#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int h, w; char s, t;
string art[50];
vector<int> et[26];

P toPos(char a) {
	int i, j;
	rep(i, h) rep(j, w) if (art[i][j] == a) return P(i, j);
	return P(-1, -1);
}

bool isConnect(char a, char b) {
	P posA = toPos(a); int yA = posA.first; int xA = posA.second;
	P posB = toPos(b); int yB = posB.first; int xB = posB.second;
	int i;
	
	if (yA == -1 || yB == -1) return false;
	if (yA == yB) {
		int y = yA;
		if (xA > xB) swap(xA, xB);
		if (art[y][xA + 1] != 'o') return false;
		if (art[y][xB - 1] != 'o') return false;
		for (i = xA + 2; i <= xB - 2; i++) {
			if (art[y][i] != '-') return false;
		}
		return true;
	}
	if (xA == xB) {
		int x = xA;
		if (yA > yB) swap(yA, yB);
		if (art[yA + 1][x] != 'o') return false;
		if (art[yB - 1][x] != 'o') return false;
		for (i = yA + 2; i <= yB - 2; i++) {
			if (art[i][x] != '|') return false;
		}
		return true;
	}
	return false;
}

int main() {
	int i, j, k;
	
	cin >> h >> w >> s >> t;
	for (i = 0; i < h; i++) cin >> art[i];
	
	for (i = 0; i < 26; i++) {
		for (j = i + 1; j < 26; j++) {
			if (isConnect('A' + i, 'A' + j)) {
				et[i].push_back(j);
				et[j].push_back(i);
			}
		}
	}
	
	int dist[26][26], INF = 114514;
	rep(i, 26) rep(j, 26) dist[i][j] = INF;
	rep(i, 26) dist[i][i] = 0;
	rep(i, 26) rep(j, et[i].size()) dist[i][et[i][j]] = 1;
	rep(k, 26) rep(i, 26) rep(j, 26) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
	cout << dist[s - 'A'][t - 'A'] << endl;
	return 0;
}
