#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class ArcadeManao { public:
	int shortestLadder(vector <string> level, int coinRow, int coinColumn)
	{
		for(int L=0; L<=level.size(); ++L)
			if(can(level, coinRow-1, coinColumn-1, L))
				return L;
		return level.size();
	}

	bool can(vector <string> level, int Cy, int Cx, int L)
	{
		int H = level.size();
		int W = level[0].size();
		typedef pair<int,int> point;
		set<point> V;
		queue<point> Q;
		Q.push(point(H-1, 0));
		V.insert(Q.front());
		while(!Q.empty()) {
			point p = Q.front();
			int y=p.first, x=p.second;
			Q.pop();
			if(y==Cy && x==Cx)
				return true;
			vector<point> cand;
			cand.push_back(point(y,x-1));
			cand.push_back(point(y,x+1));
			for(int dy=-L; dy<=+L; ++dy)
				cand.push_back(point(y+dy, x));

			for(int i=0; i<cand.size(); ++i) {
				int yy = cand[i].first;
				int xx = cand[i].second;
				point pp(yy,xx);
				if(0<=yy&&yy<H&&0<=xx&&xx<W&&level[y][x]=='X'&&!V.count(pp)) {
					Q.push(pp);
					V.insert(pp);
				}
			}
		}
		return false;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, ArcadeManao().shortestLadder(level, coinRow, coinColumn));}
int main(){

CASE(0)
	string level_[] = {"XXXX....",
 "...X.XXX",
 "XXX..X..",
 "......X.",
 "XXXXXXXX"};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 2; 
	int coinColumn = 4; 
	int _ = 2; 
END
CASE(1)
	string level_[] = {"XXXX",
 "...X",
 "XXXX"};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 1; 
	int coinColumn = 1; 
	int _ = 1; 
END
CASE(2)
	string level_[] = {"..X..",
 ".X.X.",
 "X...X",
 ".X.X.",
 "..X..",
 "XXXXX"};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 1; 
	int coinColumn = 3; 
	int _ = 4; 
END
CASE(3)
	string level_[] = {"X"};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 1; 
	int coinColumn = 1; 
	int _ = 0; 
END
CASE(4)
	string level_[] = {"XXXXXXXXXX",
 "...X......",
 "XXX.......",
 "X.....XXXX",
 "..XXXXX..X",
 ".........X",
 ".........X",
 "XXXXXXXXXX"};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 1; 
	int coinColumn = 1; 
	int _ = 2; 
END
CASE(5)
	string level_[] = {
		"XXXXXXXXXXXXXXX",
		"..............X",
		"..............X",
		"..............X",
		"XXXXXXXXXXXXXXX",
};
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = 1; 
	int coinColumn = 1; 
	int _ = 1; 
END
/*
CASE(6)
	string level_[] = ;
	  vector <string> level(level_, level_+sizeof(level_)/sizeof(*level_)); 
	int coinRow = ; 
	int coinColumn = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
