import std.stdio, std.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons;

void main() {
    auto S = readln.split.to!(int[]);
    auto H = S[0], W = S[1];
    char[][] cell;
    foreach (y; 0..H) {
        cell ~= readln.split.to!(char[]);
    }
    auto white = cell.map!(c => c.count('.')).sum;
    DList!(Tuple!(int, int,int)) queue;
    int x = 0, y = 0;
    long score = long.max;
    queue.insertBack(tuple(0,0,0));
    while (!queue.empty) {
        x = queue.front[0];
        y = queue.front[1];
        auto s = queue.front[2];
        queue.removeFront();
        if (x == W-1 && y == H-1) {
            score = min(score, s);
        }
        if (x < 0) continue;
        if (y < 0) continue;
        if (x >= W) continue;
        if (y >= H) continue;
        if (cell[y][x] == '#') continue;
        cell[y][x] = '#';
        queue.insertBack(tuple(x-1,y,s+1));
        queue.insertBack(tuple(x+1,y,s+1));
        queue.insertBack(tuple(x,y-1,s+1));
        queue.insertBack(tuple(x,y+1,s+1));
    }
    if (score == long.max) {
        writeln(-1);
        return;
    }
    writeln(white - score - 1);
}
