import std.stdio;
import core.stdc.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
int readint(){
    return readln.chomp.to!int;
}
int[] readints() {
    return readln.split.to!(int[]);
}

string[] a;

void main() {
    int[] hw = readints();
    int h = hw[0], w = hw[1];
    a = new string[h];
    int white;
    foreach(i; 0..h) {
        a[i] = readln.chomp;
        white += count(a[i], '.');
    }

    // BFS
    auto d = new int[][](h, w);
    foreach(dd; d) fill(dd, -1);
    int[][] Q; // x, y, dist
    Q ~= [0, 0, 0];
    d[0][0] = 0;
    while (Q.length) {
        auto t = Q[0]; Q = Q[1..$];
        int cx = t[0], cy = t[1], dist = t[2];
        foreach(dx, dy; zip([1,0,-1,0], [0,1,0,-1])) {
            int nx = cx + dx, ny = cy + dy;
            if (nx<0 || ny<0 || nx>=w || ny>=h) continue;
            if (d[ny][nx] != -1) continue;
            Q ~= [nx, ny, dist + 1]; d[ny][nx] = dist + 1;
        }
    }
    if (d[h-1][w-1] == -1) {
        writeln(-1);
    }
    else {
        writeln(white - d[h-1][w-1] - 1);
    }
}