#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <queue>

using namespace std;

struct BoolName : numpunct<char> {
  string t, f;
  BoolName (string t = "Yes", string f = "No") : t(t), f(f) {}
  string do_truename() const {return t;}
  string do_falsename() const {return f;}
};

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
    cout.imbue(locale(cout.getloc(), new BoolName));
  }
} initializer;

template<typename T> istream& operator>>(istream &s, vector<T> &v) {
  for (T &t : v) s >> t;
  return s;
}

template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
  for (const T &t : v) s << t << endl;
  return s;
}

void set_bool_name(string t, string f) {
  cout.imbue(locale(cout.getloc(), new BoolName(t, f)));
}

template<typename T> bool chmin(T& a, T b) {return a > b ? a = b, true : false;}

template<typename T> bool chmax(T& a, T b) {return a < b ? a = b, true : false;}

template<typename T> void sort(vector<T>& v) {sort(v.begin(), v.end());}

template<int N> class AdjacentLoop {
private:
  const static array<int, N> dy, dx;

  struct Iterator {
    const int y, x, h, w;
    int val;

    Iterator(int y, int x, int h, int w, int val) : y(y), x(x), h(h), w(w), val(val) {
      for (; this->val < N; ++this->val) {
        int yy = y + dy[this->val];
        int xx = x + dx[this->val];
        if (0 <= yy && yy < h && 0 <= xx && xx < w) break;
      }
    }

    pair<int, int> operator*() {return make_pair(y + dy[val], x + dx[val]);}

    bool operator!=(Iterator& itr) {return val < itr.val;}

    void operator++() {
      while (++val < N) {
        int yy = y + dy[val];
        int xx = x + dx[val];
        if (0 <= yy && yy < h && 0 <= xx && xx < w) break;
      }
    }
  };

  Iterator i, n;

public:
  AdjacentLoop(int y, int x, int h, int w) : i(y, x, h, w, 0), n(y, x, h, w, N) {}

  Iterator& begin() {return i;}

  Iterator& end() {return n;}
};

template<> const array<int, 2> AdjacentLoop<2>::dy = {{0, 1}};
template<> const array<int, 2> AdjacentLoop<2>::dx = {{1, 0}};

template<> const array<int, 4> AdjacentLoop<4>::dy = {{0, -1, 0, 1}};
template<> const array<int, 4> AdjacentLoop<4>::dx = {{1, 0, -1, 0}};

template<> const array<int, 5> AdjacentLoop<5>::dy = {{0, -1, 0, 1, 0}};
template<> const array<int, 5> AdjacentLoop<5>::dx = {{1, 0, -1, 0, 0}};

template<> const array<int, 8> AdjacentLoop<8>::dy = {{-1, -1, -1, 0, 0, 1, 1, 1}};
template<> const array<int, 8> AdjacentLoop<8>::dx = {{-1, 0, 1, -1, 1, -1, 0, 1}};

template<> const array<int, 9> AdjacentLoop<9>::dy = {{-1, -1, -1, 0, 0, 0, 1, 1, 1}};
template<> const array<int, 9> AdjacentLoop<9>::dx = {{-1, 0, 1, -1, 0, 1, -1, 0, 1}};

template<typename Edge> struct FullEdge : public Edge {
  int from;

  FullEdge() = default;

  FullEdge(const int from, const Edge& edge) : Edge(edge), from(from) {}
};

struct Edge {
  using CostType = int;
  const static int cost = 1;
  int to;
  Edge(int to = -1) : to(to) {}
  bool isNone() const {return to == -1;}
};

istream& operator>>(istream& s, FullEdge<Edge>& edge) {
  s >> edge.from >> edge.to;
  return s;
}

ostream& operator<<(ostream& s, const FullEdge<Edge>& edge) {
  s << '(' << edge.from << ',' << edge.to << ')';
  return s;
}

template<typename Cost> struct WeightedEdge : public Edge {
  using CostType = Cost;
  Cost cost;
  WeightedEdge(int to = -1, Cost cost = 0) : Edge(to), cost(cost) {}
};

template<typename Cost> istream& operator>>(istream& s, FullEdge<WeightedEdge<Cost>>& edge) {
  s >> edge.from >> edge.to >> edge.cost;
  return s;
}

template<typename Cost> ostream& operator<<(ostream& s, const FullEdge<WeightedEdge<Cost>>& edge) {
  s << '(' << edge.from << ',' << edge.to << ',' << edge.cost << ')';
  return s;
}

template<typename Capacity> struct ResidualEdge : public Edge {
  using CapacityType = Capacity;
  Capacity cap;
  int rev;
  ResidualEdge(int to = -1, Capacity cap = 0) : Edge(to), cap(cap) {}
  ResidualEdge reverse(int from) const {return ResidualEdge(from, 0);}
};

template<typename Capacity, typename Cost> struct WeightedResidualEdge : public ResidualEdge<Capacity> {
  Cost cost;
  WeightedResidualEdge(int to = -1, Capacity cap = 0, Cost cost = 0) : ResidualEdge<Capacity>(to, cap), cost(cost) {}
  WeightedResidualEdge reverse(int from) const {return WeightedResidualEdge(from, 0, -cost);}
};

template<typename Edge> class Graph {
public:
  using EdgeType = Edge;
  virtual int size() const = 0;
  template<typename... Args> void addEdge(int from, int to, Args...) {
    (void)from;
    (void)to;
  }
  template<typename... Args> void addUndirectedEdge(int from, int to, Args...) {
    (void)from;
    (void)to;
  }

  vector<FullEdge<Edge>> getAllEdges() const {
    vector<FullEdge<Edge>> res;
    for (int i = 0; i < size(); ++i) {
      for (const auto& edge : getEdges(i)) {
        res.emplace_back(i, edge);
      }
    }
    return res;
  }

  virtual vector<Edge> getEdges(int from) const = 0;
  virtual Edge getEdge(int from, int to) const = 0;
  virtual bool hasEdge(int from, int to) const = 0;

  int getDegree(int v) const {
    return getEdges(v).size();
  }

  vector<int> getIndegree() const {
    vector<int> degree(size());
    for (const auto& edge : getAllEdges()) ++degree[edge.to];
    return degree;
  }
};

template<typename Graph> Graph readGraph(int n, int m, bool undirected = true, bool one_origin = true) {
  Graph graph(n);
  for (int i = 0; i < m; ++i) {
    FullEdge<typename Graph::EdgeType> edge;
    cin >> edge;
    if (one_origin) {
      --edge.from;
      --edge.to;
    }
    if (undirected) graph.addUndirectedEdge(edge);
    else graph.addEdge(edge);
  }
  return graph;
}

template<typename Edge> class AdjacencyList : public Graph<Edge> {
protected:
  vector<vector<Edge>> graph;

public:
  using EdgeType = Edge;

  AdjacencyList() = default;

  AdjacencyList(int n) : graph(n) {}

  int size() const {
    return graph.size();
  }
  
  template<typename... Args> void addEdge(int from, int to, Args... args) {
    graph[from].emplace_back(to, args...);
  }

  void addEdge(const FullEdge<Edge>& edge) {
    graph[edge.from].emplace_back(edge);
  }

  template<typename... Args> void addUndirectedEdge(int from, int to, Args... args) {
    addEdge(from, to, args...);
    addEdge(to, from, args...);
  }

  void addUndirectedEdge(FullEdge<Edge> edge) {
    graph[edge.from].emplace_back(edge);
    swap(edge.from, edge.to);
    graph[edge.from].emplace_back(edge);
  }

  vector<Edge> getEdges(int from) const {
    return graph[from];
  }

  Edge getEdge(int from, int to) const {
    Edge res;
    for (const auto& edge : graph[from]) {
      if (edge.to == to) res = edge;
    }
    return res;
  }

  bool hasEdge(int from, int to) const {
    for (const auto& edge : graph[from]) {
      if (edge.to == to) return true;
    }
    return false;
  }

  vector<Edge>& operator[](int v) {
    return graph[v];
  }

  const vector<Edge>& operator[](int v) const {
    return graph[v];
  }
};

template<typename Edge> istream& operator>>(istream& s, AdjacencyList<Edge>& graph) {
  int n, m;
  cin >> n >> m;
  graph = readGraph<AdjacencyList<Edge>>(n, m);
  return s;
}

template<typename Graph, typename State> class Search {
protected:
  using Edge = typename Graph::EdgeType;

  const Graph graph;
  vector<bool> visited;

  virtual void push(const State&) = 0;
  virtual State next() = 0;
  virtual bool isRunning() = 0;

  virtual void visit(const State&) {}

public:
  Search(const Graph& graph) : graph(graph), visited(graph.size(), false) {}

  void solve(vector<int> from) {
    for (int i : from) push(State(i));
    while (isRunning()) {
      State now = next();
      int pos = now.getPos();
      if (visited[pos]) continue;
      visited[pos] = true;
      visit(now);
      for (const Edge& edge : graph.getEdges(pos)) {
        State nextState = now.next(pos, edge);
        if (visited[nextState.getPos()]) continue;
        push(nextState);
      }
    }
  }

  void solve(int from) {solve(vector<int>({from}));}

  bool isReachable(int v) {return visited[v];}
};

template<typename Edge> class Tree {
public:
  using EdgeType = Edge;

  vector<Edge> parent;
  vector<vector<int>> children;
  vector<int> depth;

  Tree() {}

  Tree(int n) : children(n), depth(n, -1) {
    for (int i = 0; i < n; ++i) parent.emplace_back(i);
  }

  int size() const {
    return parent.size();
  }
  
  template<typename... Args> void addEdge(int from, int to, Args... args) {
    parent[from] = Edge(to, args...);
    children[to].emplace_back(from);
  }

  void addEdge(int from, const Edge& edge) {
    parent[from] = edge;
    children[edge.to].emplace_back(from);
  }

  vector<Edge> getEdges(int from) const {
    vector<Edge> res;
    for (int v : children[from]) {
      auto e = parent[v];
      e.to = v;
      res.emplace_back(e);
    }
    if (from != parent[from].to) res.emplace_back(parent[from]);
    return res;
  }

  int getDepth(int v) {
    if (depth[v] != -1) return depth[v];
    if (parent[v].to == v) return depth[v] = 0;
    return depth[v] = getDepth(parent[v].to) + 1;
  }

  vector<int> getPath(int v) {
    vector<int> res{v};
    while (v != parent[v].to) {
      v = parent[v].to;
      res.emplace_back(v);
    }
    return res;
  }
};

template<typename Edge> struct DijkstraState {
  using Cost = typename Edge::CostType;

  int from;
  Edge edge;
  Cost cost;

  DijkstraState(const int pos) : from(pos), edge(pos), cost(0) {}

  DijkstraState(const int from, const Edge& edge, const Cost cost) : from(from), edge(edge), cost(cost) {}

  DijkstraState next(const int from, const Edge& edge) const {return DijkstraState(from, edge, cost + edge.cost);}

  bool operator<(const DijkstraState& state) const {return cost > state.cost;}

  int getPos() const {return edge.to;}
};

template<typename Graph, bool Restoration = false, typename State = DijkstraState<typename Graph::EdgeType>> class Dijkstra : public Search<Graph, State> {
protected:
  using Edge = typename Graph::EdgeType;
  using Cost = typename Edge::CostType;

  constexpr static Cost INF = numeric_limits<Cost>::max();

  priority_queue<State> que;

  void push(const State& state) {
    if (dis[state.getPos()] <= state.cost) return;
    que.push(state);
    dis[state.getPos()] = state.cost;
  }

  State next() {
    State now = que.top();
    que.pop();
    return now;
  }

  bool isRunning() {return !que.empty();}

  void visit(const State& state) {
    if (Restoration) {
      int from = state.from, to = state.edge.to;
      if (from != to) {
        auto e = state.edge;
        e.to = from;
        shortestPathTree.addEdge(to, e);
      }
    }
  }

public:
  vector<Cost> dis;
  Tree<Edge> shortestPathTree;

  Dijkstra(const Graph& graph) : Search<Graph, State>(graph), dis(graph.size(), INF) {
    if (Restoration) shortestPathTree = Tree<Edge>(graph.size());
  }
};

template<typename Graph> Dijkstra<Graph> shortestPath(Graph& graph, int from) {
  Dijkstra<Graph> dijkstra(graph);
  dijkstra.solve(from);
  return dijkstra;
}

template<typename Graph> typename Graph::EdgeType::CostType shortestPath(Graph& graph, int from, int to) {
  return shortestPath(graph, from).dis[to];
}

template<typename Graph> Dijkstra<Graph, true> shortestPathTree(Graph& graph, int from) {
  Dijkstra<Graph, true> dijkstra(graph);
  dijkstra.solve(from);
  return dijkstra;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  cin >> s;
  AdjacencyList<Edge> graph(h * w);
  int res = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '.') ++res;
      for (auto p : AdjacentLoop<2>(i, j, h, w)) {
        if (s[i][j] == '.' && s[p.first][p.second] == '.') {
          graph.addUndirectedEdge(i * w + j, p.first * w + p.second);
        }
      }
    }
  }
  int c = shortestPath(graph, 0, h * w - 1);
  cout << (c != numeric_limits<int>::max() ? res - c - 1 : -1) << endl;
}



Measured memory_bytes: 512