import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
    std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
    std.random, std.typecons, std.container;

ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);

void main()
{
    long h, w;
    scan(h, w);
    auto s = new string[](h);
    foreach (ref e; s)
        e = sread();
    auto cost = new long[][](h, w);
    long cntwhite;
    foreach (i; iota(h))
    {
        foreach (j; iota(w))
        {
            if (s[i][j] == '.')
            {
                cost[i][j] = -1;
                cntwhite++;
            }
            else
                cost[i][j] = INF;
        }
    }

    //  迷路の探索はBFSのがいいよね
    auto q = new DList!(Pair)(Pair(0, 0, 1));
    while (!q.empty())
    {
        auto c = q.front();
        q.removeFront();
        if (c.x - 1 >= 0 && cost[c.x - 1][c.y] < 0)
        {
            q.insertBack(Pair(c.x - 1, c.y, c.cost + 1));
            cost[c.x - 1][c.y] = c.cost + 1;
        }
        if (c.x + 1 < h && cost[c.x + 1][c.y] < 0)
        {
            q.insertBack(Pair(c.x + 1, c.y, c.cost + 1));
            cost[c.x + 1][c.y] = c.cost + 1;
        }
        if (c.y - 1 >= 0 && cost[c.x][c.y - 1] < 0)
        {
            q.insertBack(Pair(c.x, c.y - 1, c.cost + 1));
            cost[c.x][c.y - 1] = c.cost + 1;
        }
        if (c.y + 1 < w && cost[c.x][c.y + 1] < 0)
        {
            q.insertBack(Pair(c.x, c.y + 1, c.cost + 1));
            cost[c.x][c.y + 1] = c.cost + 1;
        }
    }
    if (cost[h - 1][w - 1] > 0)
        writeln(cntwhite - cost[h - 1][w - 1]);
    else
        writeln(-1);
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}
