// Slower C++ solution:
#include <stdio.h>

#include <stdlib.h>

#include <algorithm>



#define MAX_W (50)

#define MAX_H (50)

#define INF (1000000000)

#define SIZE_BUF (30)



using namespace std;



const int dis[] = {1, 0, -1, 0}, djs[] = {0, 1, 0, -1};

void bellman_ford(char *fields, int H, int W, int i0, int j0, int *distances) {

  // initialize

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      distances[j + i * W] = INF;

    }

  }

  distances[j0 + i0 * W] = 0;



  // calculate shortest path

  for (int k = 0; k < H * W; k++) {

    for (int i = 0; i < H; i++) {

      for (int j = 0; j < W; j++) {

        for (int d = 0; d < 4; d++) {

          const int i2 = i + dis[d], j2 = j + djs[d];

          if (i2 >= 0 && i2 < H && j2 >= 0 && j2 < W &&

              fields[j2 + i2 * W] == '.') {

            distances[j2 + i2 * W] =

                min(distances[j2 + i2 * W], distances[j + i * W] + 1);

          }

        }

      }

    }

  }

}



int main(int argc, char *argv[]) {

  // read inputs

  int H, W;

  char fields[MAX_H * MAX_W], buf[SIZE_BUF];

  scanf("%d %d", &H, &W);

  fgets(buf, SIZE_BUF, stdin);

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      scanf("%c", &fields[j + i * W]);

    }

    fgets(buf, SIZE_BUF, stdin);

  }



  // find shortest path

  int distances[MAX_H * MAX_W];

  bellman_ford(fields, H, W, 0, 0, distances);



  // count the number of white squares

  int count_white = 0;

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      if (fields[j + i * W] == '.') {

        count_white++;

      }

    }

  }



  if (distances[W * H - 1] < INF) {

    printf("%d\n", count_white - distances[W * H - 1] - 1);

  } else {

    printf("-1\n");

  }



  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3674862901057603x):
#include <stdio.h>

#include <stdlib.h>

#include <algorithm>

#include <queue>



#define MAX_W (50)

#define MAX_H (50)

#define INF (1000000000)

#define SIZE_BUF (30)



using namespace std;



const int dis[] = {1, 0, -1, 0}, djs[] = {0, 1, 0, -1};

void bellman_ford(char *fields, int H, int W, int i0, int j0, int *distances) {

  // initialize

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      distances[j + i * W] = INF;

    }

  }

  distances[j0 + i0 * W] = 0;



  // calculate shortest path

  for (int k = 0; k < H * W; k++) {

    for (int i = 0; i < H; i++) {

      for (int j = 0; j < W; j++) {

        for (int d = 0; d < 4; d++) {

          const int i2 = i + dis[d], j2 = j + djs[d];

          if (i2 >= 0 && i2 < H && j2 >= 0 && j2 < W &&

              fields[j2 + i2 * W] == '.') {

            distances[j2 + i2 * W] =

                min(distances[j2 + i2 * W], distances[j + i * W] + 1);

          }

        }

      }

    }

  }

}



void bfs(char *fields, int H, int W, int i0, int j0, int *distances) {

  // initialize

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      distances[j + i * W] = -1;

    }

  }

  // bfs

  queue<int> i_que, j_que;

  i_que.push(i0);

  j_que.push(j0);

  distances[j0 + i0 * W] = 0;

  while (!i_que.empty()) {

    const int i = i_que.front(), j = j_que.front();

    i_que.pop();

    j_que.pop();

    for (int d = 0; d < 4; d++) {

      const int i2 = i + dis[d], j2 = j + djs[d];

      if (i2 >= 0 && i2 < H && j2 >= 0 && j2 < W &&

          fields[j2 + i2 * W] == '.' && distances[j2 + i2 * W] < 0) {

        distances[j2 + i2 * W] = distances[j + i * W] + 1;

        i_que.push(i2);

        j_que.push(j2);

      }

    }

  }

}



int main(int argc, char *argv[]) {

  // read inputs

  int H, W;

  char fields[MAX_H * MAX_W], buf[SIZE_BUF];

  scanf("%d %d", &H, &W);

  fgets(buf, SIZE_BUF, stdin);

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      scanf("%c", &fields[j + i * W]);

    }

    fgets(buf, SIZE_BUF, stdin);

  }



  // find shortest path

  int distances[MAX_H * MAX_W];

  // bellman_ford(fields, H, W, 0, 0, distances);

  bfs(fields, H, W, 0, 0, distances);



  // count the number of white squares

  int count_white = 0;

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      if (fields[j + i * W] == '.') {

        count_white++;

      }

    }

  }



  // if (distances[W * H - 1] < INF) {

  if (distances[W * H - 1] >= 0) {

    printf("%d\n", count_white - distances[W * H - 1] - 1);

  } else {

    printf("-1\n");

  }



  return 0;

}