#include <iostream>

using namespace std;

struct xy {
    int x;
    int y;
};

int main(void)
{
    int H, W;
    cin >> H >> W;

    char s[51][51];
    int white = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> s[i][j];
            if (s[i][j] == '.') white++;
        }
        s[i][W] = 'n';
    }
    for (int i = 0; i < W; i++) s[H][i] = 'n';
    
    xy queue[2500];
    queue[0].x = 0; queue[0].y = 0;
    xy index[51][51];
    int v[51][51] = {{0}};
    v[0][0] = 1;
    int success = 0;
    int head = 0, tail = 1;
    while(head != tail) {
        if (v[H - 1][W - 1]) {
            success = 1;
            break;
        }
        // cout << s[queue[head].y + 1][queue[head].x] << v[queue[head].y + 1][queue[head].x] << endl;
        if (s[queue[head].y + 1][queue[head].x] == '.' && v[queue[head].y + 1][queue[head].x] == 0) {
            v[queue[head].y + 1][queue[head].x] = 1;
            index[queue[head].y + 1][queue[head].x].y = queue[head].y; index[queue[head].y + 1][queue[head].x].x =  queue[head].x;
            queue[tail].y = queue[head].y + 1; queue[tail++].x = queue[head].x;
        }
        // cout << s[queue[head].y - 1][queue[head].x] << v[queue[head].y - 1][queue[head].x] << endl;
        if (s[queue[head].y - 1][queue[head].x] == '.' && v[queue[head].y - 1][queue[head].x] == 0) {
            v[queue[head].y - 1][queue[head].x] = 1;
            index[queue[head].y - 1][queue[head].x].y = queue[head].y; index[queue[head].y - 1][queue[head].x].x =  queue[head].x;
            queue[tail].y = queue[head].y - 1; queue[tail++].x = queue[head].x;
        }
        // cout << s[queue[head].y][queue[head].x + 1] << v[queue[head].y][queue[head].x + 1] << endl;
        if (s[queue[head].y][queue[head].x + 1] == '.' && v[queue[head].y][queue[head].x + 1] == 0) {
            v[queue[head].y][queue[head].x + 1] = 1;
            index[queue[head].y][queue[head].x + 1].y = queue[head].y; index[queue[head].y][queue[head].x + 1].x =  queue[head].x;
            queue[tail].y = queue[head].y; queue[tail++].x = queue[head].x + 1;
        }
        // cout << s[queue[head].y][queue[head].x - 1] << v[queue[head].y][queue[head].x - 1] << endl;
        if (s[queue[head].y][queue[head].x - 1] == '.' && v[queue[head].y][queue[head].x - 1] == 0) {
            v[queue[head].y][queue[head].x - 1] = 1;
            index[queue[head].y][queue[head].x - 1].y = queue[head].y; index[queue[head].y][queue[head].x - 1].x =  queue[head].x;
            queue[tail].y = queue[head].y; queue[tail++].x = queue[head].x - 1;
        }
        head++;
    }
    
    // for (int i = 0; i < tail; i++) {
    //     cout << queue[i].y << " " << queue[i].x << endl;
    // }
    if (success) {
        int count = 2;
        int y = index[queue[tail - 1].y][queue[tail - 1].x].y, x = index[queue[tail - 1].y][queue[tail - 1].x].x, t;
        while(y != 0 || x != 0) {
            t = index[y][x].y; x = index[y][x].x; y = t;
            // cout << y << " " << x << endl;
            count++;
        }
        // cout << count << endl;
        cout << white - count<< endl;
    }
    else cout << -1 << endl;
    
    return 0;
}

Measured memory_bytes: 256