import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;

void main() {
    int h, w;
    scan(h, w);
    auto map = new char[][](h, w);
    iota(h).each!(i => map[i] = readln.chomp.to!(char[]));

    import std.typecons : Tuple;
    alias Pair = Tuple!(int, "i", int, "j");
    auto dist = new int[][](h, w);
    immutable inf = h*w + 1;
    fillAll(dist, inf);
    dist[0][0] = 0;
    auto qu = Queue!(Pair)(h*w);
    qu.insert(Pair(0, 0));

    auto di = [0, 1, 0, -1];
    auto dj = [1, 0, -1, 0];

    while (!qu.empty) {
        auto v = qu.front;
        qu.popFront();

        foreach (k ; 0 .. 4) {
            int ni = v.i + di[k];
            int nj = v.j + dj[k];

            if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
            if (dist[ni][nj] < inf) continue;
            if (map[ni][nj] == '#') continue;

            dist[ni][nj] = dist[v.i][v.j] + 1;
            qu.insert(Pair(ni, nj));
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", dist);
    }

    if (dist[h-1][w-1] == inf) {
        writeln(-1);
        return;
    }

    int ans = h*w - (dist[h-1][w-1] + 1);

    foreach (i ; 0 .. h) {
        foreach (j ; 0 .. w) {
            if (map[i][j] == '#') ans--;
        }
    }

    writeln(ans);
}




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);
        }
    }
}

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]);
        }
    }
}

Measured memory_bytes: 380