#include #include #include #include using namespace std ; const int NMAX = 105 ; const int dx[] = { 1, 0} ; const int dy[] = { 0, 1} ; //ifstream cin("run.in") ; //ofstream cout("run.out") ; int D[NMAX][NMAX], M, N, bestx, besty; queue > Q; void Lee() { while( !Q.empty() ) { int x = Q.front().first; int y = Q.front().second; Q.pop(); for(int i = 0; i < 4 ; ++ i) { int xnou = x + dx[i]; int ynou = y + dy[i]; if(xnou >= 1 && xnou <= N && ynou >= 1 && ynou <= N && D[xnou][ynou] != -2) if(D[xnou][ynou] > D[x][y] + 1 || D[xnou][ynou] == 1) { D[xnou][ynou] = D[x][y] + 1; Q.push(make_pair(xnou, ynou)); bestx = xnou ; besty = ynou ; } } } //cout << D[xnou][ynou] << '\n' ; } void citire() { // memset(D, INF, sizeof(D)) cin >> N >> M ; cin.get() ; for(int i = 1; i<= N ;++ i) for(int j = 1; j <= M ; ++ j) { char c ; cin >> c ; if(c == '\n') cin >> c ; if(c == '&') D[i][j] = - 2 ; else if(c == '.') D[i][j] = 1 ; } Q.push(make_pair(1, 1)) ; } void afisare() { // for(int i = 1; i <= N ; ++ i) // {for(int j = 1 ; j <= M ; ++ j) // cout << D[i][j] << ' ' ; //cout << '\n' ; } cout << D[bestx][besty] <<'\n' ; } int main() { citire(); Lee() ; afisare() ; // cin.close(); // cout.close(); return 0; }