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, black, white;
    scan(h, w);
    auto s = new long[][](h, w);
    foreach (i; iota(h))
    {
        auto tmp = sread();
        foreach (j; iota(w))
        {
            s[i][j] = (tmp[j] == '.' ? 0 : 1);
            black += s[i][j];
        }
    }
    white = h * w - black;
    auto b = new bool[][](h, w);
    auto q = DList!(Pair)(Pair(0, 0, 1));
    long cost = INF;
    while (!q.empty)
    {
        auto current = q.front();
        q.removeFront();
        b[current.x][current.y] = true;

        if (s[current.x][current.y])
            continue;
        if (current.x == h - 1 && current.y == w - 1)
            cost = min(current.cost, cost);

        if (current.x + 1 < h && !b[current.x + 1][current.y])
        {
            q.insertBack(Pair(current.x + 1, current.y, current.cost + 1));
        }
        if (current.x - 1 > 0 && !b[current.x - 1][current.y])
        {
            q.insertBack(Pair(current.x - 1, current.y, current.cost + 1));
        }
        if (current.y + 1 < w && !b[current.x][current.y + 1])
        {
            q.insertBack(Pair(current.x, current.y + 1, current.cost + 1));
        }
        if (current.y - 1 > 0 && !b[current.x][current.y - 1])
        {
            q.insertBack(Pair(current.x, current.y - 1, current.cost + 1));
        }
    }
    if (b[h - 1][w - 1])
        writeln(white - cost);
    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;
    }
}
