// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  Point operator + (Point v) { return Point(x + v.x, y + v.y); }
  Point operator - (Point v) { return Point(x - v.x, y - v.y); }
  bool operator == (const Point &v) const {
    return x == v.x && y == v.y;
  }
  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

enum Square {
  BLANK = 0,
  WALL = 1
};
ostream & operator << (ostream & out, Square const & v) {
  if (v == 0) out << ".";
  else if (v ==  1) out << "#";
  return out;
}
#define MAX_W 51
#define INFTY (MAX_W*MAX_W)
Int W, H;
Point start = { 0, 0 }, goal;
const vector<Point> DIRS = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
vector<vector<Int> > D(MAX_W, vector<Int>(MAX_W, INFTY));
vector<vector<Square> > G(MAX_W, vector<Square>(MAX_W, BLANK));
Int numWalls = 0;

bool outOfRange(Point &p) {
  if (p.y < 0 || H <= p.y) return true;
  return p.x < 0 || W <= p.x;
}

bool isWall(Point &p) {
  return G.at(p.y).at(p.x) == WALL;
}

void input() {
  cin >> H >> W;
  goal = { W-1, H-1 };
  char c;
  loop(h,0,H) {
    loop(w,0,W) {
      cin >> c;
      if (c == '#') {
        G.at(h).at(w) = WALL;
        numWalls++;
      }
    }
  }
}

Int bfs() {
  queue<Point> Q;
  Q.push(start);
  D.at(start.y).at(start.x) = 1;
  while (Q.size()) {
    Point u = Q.front(); Q.pop(); // これは必ずBLANK
    Int prev_d = D.at(u.y).at(u.x);
    for (auto dir: DIRS) {
      Point pos = u + dir;
      if (outOfRange(pos)) continue;
      if (isWall(pos)) continue;
      Int &d = D.at(pos.y).at(pos.x);
      if (d <= prev_d + 1) continue; // すでにより最短経路で通過済み
      d = prev_d + 1;
      if (pos == goal) return d; // 幅優先探索では最初に到達した時が最短
      Q.push(pos);
    }
  }

  return -1; // Not found
}

void solve() {
  Int shortest = bfs();
  if (shortest == -1) {
    cout << -1 << endl;
    return;
  }

  cout << (H*W - numWalls - shortest) << endl;
}

int main() {
  input();
  solve();
  return 0;
}


Measured memory_bytes: 256