import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.Arrays;
import java.util.Queue;
import java.util.Stack;
import java.util.LinkedList;
import java.util.StringTokenizer;

/* KSP Problem Statement: https://ksp.mff.cuni.cz/tasks/24/tasks2.html#task1 */
public class escape
{
    /* The directions in which the maze runner can move. */
    public static final int[][] DIRECTIONS = new int[][]{{-1, 0},{1, 0},{0, -1},{0, 1}};

    public static final int CLOSED = -4;
    public static final int OPEN   = -3;

    public static final int STONES = -2;
    public static final int GRASS  = -1;
    public static final int FIRE    = 0;

    /* The fire position in the forest */
    public static class FireState
    {
        public int x; // x position of the fire in the forest.
        public int y; // y position of the fire in the forest.

        public FireState() {}

        public FireState(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        /* Returns a new state which was created from this state
         * by moving the fire in the specified direction.
         */
        public FireState move(int[] direction)
        {
            FireState copy = new FireState();

            copy.x = this.x + direction[0];
            copy.y = this.y + direction[1];

            return copy;
        }
    }

    /* The queue for bread-first search. */
    public static Queue<FireState> queue = new LinkedList<FireState>();

    /* Stack for time travel. */
    public static Stack<FireState> stack = new Stack<FireState>();

    /* Stores the map read from the input. */
    public static int[][] map = null;

    /* Read the forest from the standard input and add the fire starters
     * into the queue.
     */
    public static void readForest(BufferedReader reader) throws IOException
    {
        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());

        int width = Integer.valueOf(tokenizer.nextToken());
        int height = Integer.valueOf(tokenizer.nextToken());

        map = new int[height][width + 1];

        int iRow = 0;

        String line = null;

        while ((line = reader.readLine()) != null)
        {
            int iCol = 0;
                        
            for (int i = 0; i < width; i++)
            {
                switch (line.charAt(i))
                {
                case '#':
                    map[iRow][iCol] = STONES;
                    break;

                case '@':
                    map[iRow][iCol] = FIRE;
                    queue.add(new FireState(iCol, iRow));
                    break;

                default:
                    map[iRow][iCol] = GRASS;
                    break;
                }

                ++iCol;
            }

            map[iRow][iCol] = CLOSED;

            ++iRow;
        }
    }

    /* Check whether the fire position is within the boundaries of the forest. */
    public static boolean isWithinBounds(FireState state, int extraColumn)
    {
        return (state.x >= 0 && state.x < (map[0].length - 1 + extraColumn) && state.y >= 0 && state.y < map.length);
    }

    /* Burn the whole forest down. If after the fire spread everywhere and
     * there is still way out of the forest, return ``true``.
     */
    public static boolean forward()
    {
        FireState state = null;

        /* Until the queue get emptied or a solution has been found. */
        while ((state = queue.poll()) != null)
        {
            /* The states will be processed in first-in last-out :) fashion
             * when we go back in time.
             */
            stack.push(state);

            for (int[] direction : DIRECTIONS)
            {
                FireState nextState = state.move(direction);

                if (!isWithinBounds(nextState, 0))
                    continue;

                if (map[nextState.y][nextState.x] == GRASS)
                {
                    map[nextState.y][nextState.x] = map[state.y][state.x] + 1;
                    queue.add(nextState);
                }
            }
        }

        for (int iRow = 0; iRow < map.length; iRow++)
            queue.add(new FireState(map[0].length - 1, iRow));

        boolean wayOutStillExists = false;

        while (!wayOutStillExists && (state = queue.poll()) != null)
        {
            for (int[] direction : DIRECTIONS)
            {
                FireState nextState = state.move(direction);

                if (nextState.x < 0)
                {
                    wayOutStillExists = true;
                    break;
                }

                if (!isWithinBounds(nextState, 0))
                    continue;

                if (map[nextState.y][nextState.x] == GRASS)
                {
                    map[nextState.y][nextState.x] = CLOSED;
                    queue.add(nextState);
                }
            }
        }

        return wayOutStillExists;
    }

    /* Go back in time and look for the latest moment in which the forest
     * was still passable.
     */
    public static int rewind()
    {
        boolean wayOutExists = false;

        int time = Integer.MAX_VALUE;

        while (!wayOutExists && !stack.empty())
        {
            FireState state = stack.pop();

            time = map[state.y][state.x];

            map[state.y][state.x] = OPEN;

            for (int[] direction : DIRECTIONS)
            {
                FireState nextState = state.move(direction);

                if (!isWithinBounds(nextState, 1))
                    continue;

                if (map[nextState.y][nextState.x] == CLOSED)
                {
                    map[state.y][state.x] = CLOSED;
                    queue.add(state);
                    break;
                }
            }

            while (!wayOutExists && (state = queue.poll()) != null)
            {
                for (int[] direction : DIRECTIONS)
                {
                    FireState nextState = state.move(direction);

                    if (nextState.x < 0)
                    {
                        wayOutExists = true;
                        break;
                    }

                    if (!isWithinBounds(nextState, 0))
                        continue;

                    if (map[nextState.y][nextState.x] == OPEN)
                    {
                        map[nextState.y][nextState.x] = CLOSED;
                        queue.add(nextState);
                    }
                }
            }
        }

        return time - 1;
    }

    public static void main(String[] arguments) throws IOException
    {
        readForest(new BufferedReader(new InputStreamReader(System.in)));

        if (forward())
        {
            System.out.println("There is a way through the forest even after the fire!");
        }
        else
        {
            int time = rewind();

            if (time == -1)
                System.out.println("There has not been a way through the forest in the beginning.");
            else
                System.out.println("There is a way through the forest for " + time + " minutes.");
        }
    }
}