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

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    int H, W;
    scan(H, W);
    auto G = iota(H).map!(i => readln.chomp).array;

    int kuro;
    foreach (i ; 0 .. H) {
        foreach (j ; 0 .. W) {
            kuro += G[i][j] == '#';
        }
    }

    auto dist = new int[][](H, W);
    fillAll(dist, inf);
    dist[0][0] = 0;
    auto q = Queue!int(H * W);
    q.insert(0);

    auto di = [1, -1, 0, 0];
    auto dj = [0, 0, 1, -1];
    
    while (!q.empty) {
        auto i = q.front / W, j = q.front % W;
        q.popFront();

        foreach (k ; 0 .. 4) {
            auto ni = i + di[k];
            auto nj = j + dj[k];
            if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue;
            if (G[ni][nj] == '#' || dist[ni][nj] < inf) continue;
            dist[ni][nj] = dist[i][j] + 1;
            q.insert(ni * W + nj);
        }
    }

    auto kyori = dist[H - 1][W - 1];
    if (kyori == inf) {
        writeln(-1);
        return;
    }
    auto ans = H * W - (kyori + 1) - kuro;
    writeln(ans);
}




struct Queue(T) {
    private {
        size_t cap, head, tail;
        T[] data;
    }

    this (size_t n) in {
        assert(n > 0, "The capacity of a queue must be a positive integer.");
    } body {
        cap = n + 1;
        data = new T[](cap);
    }

    void clear() {
        head = tail = 0;
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return head == (tail + 1) % cap;
    }

    size_t length() {
        return head <= tail ? tail - head : cap - head + tail;
    }

    T front() in {
        assert(!empty, "The queue is empty.");
    } body {
        return data[head];
    }

    void removeFront() in {
        assert(!empty, "The queue is empty.");
    } body {
        (++head) %= cap;
    }

    alias popFront = removeFront;

    void insertBack(T x) in {
        assert(!full, "The queue is full.");
    } body {
        data[tail++] = x;
        tail %= cap;
    }

    alias insert = insertBack;

    T[] array() {
        return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail];
    }

    string toString() {
        import std.format : format;

        if (head <= tail) {
            return format("[%(%s, %)]", data[head .. tail]);
        }
        else {
            return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]);
        }
    }
}



void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}


Measured memory_bytes: 256