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

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int rows, int cols, string[] g) {
    auto t = new int[][](rows, cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            t[i][j] = int.max;
        }
    }

    int[][] q;
    q ~= [0, 0, 0];
    while (q.length > 0) {
        auto s = q[0];
        q = q[1..$];

        int r = s[0];
        int c = s[1];
        int cost = s[2];
        if (t[r][c] <= cost) continue;
        t[r][c] = cost;

        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (abs(i) + abs(j) == 1) {
                    int row = r + i;
                    int col = c + j;
                    if (0 <= row && row < rows && 0 <= col && col < cols) {
                        if (g[row][col] == '#') continue;
                        q ~= [row, col, cost + 1];
                    }
                }
            }
        }
    }

    assert(t[rows-1][cols-1] != int.max);
    int last = t[rows-1][cols-1];
    if (last == int.max) return -1;

    int ans = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int cost = t[i][j];
            if (cost == int.max && g[i][j] == '#') continue;

            ans++;
        }
    }
    return ans - last - 1;
}

void main() {
    auto hw = readints;
    int rows = hw[0], cols = hw[1];

    string[] g;
    for (int i = 0; i < rows; i++) {
        g ~= readln.chomp;
    }
    int ans = calc(rows, cols, g);
    writeln(ans);
}