using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;
        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }
            var res = line[index];
            index++;
            return res;
        }
        public int NextInt()
        {
            return int.Parse(Next());
        }
        public long NextLong()
        {
            return long.Parse(Next());
        }
        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }
        public int[] IntArray()
        {
            return Array().Select(int.Parse).ToArray();
        }
        public long[] LongArray()
        {
            return Array().Select(long.Parse).ToArray();
        }
    }

    class Program
    {
        private int H, W;
        private char[][] S;
        private void Scan()
        {
            var sc = new Scanner();
            H = sc.NextInt();
            W = sc.NextInt();
            S = new char[H][];
            for (int i = 0; i < H; i++)
            {
                S[i] = sc.Next().ToArray();
            }
        }

        struct St
        {
            public int X, Y;
            public St(int x, int y)
            {
                X = x;
                Y = y;
            }
        }

        public void Solve()
        {
            Scan();
            var next = new int[] { 0, 1, 0, -1 };
            var dist = new int[H, W];
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    dist[i, j] = -1;
                }
            }
            dist[0, 0] = 1;
            var q = new Queue<St>();
            q.Enqueue(new St(0, 0));
            while (q.Count > 0)
            {
                var s = q.Dequeue();
                //Console.WriteLine($"{s.X} {s.Y}");
                for (int i = 0; i < 4; i++)
                {
                    var n = new St(s.X + next[i], s.Y + next[(i + 1) % 4]);
                    if ((n.X < 0) || (n.X >= W) || (n.Y < 0) || (n.Y >= H))
                    {
                        continue;
                    }
                    if (S[n.Y][n.X] == '#')
                    {
                        continue;
                    }
                 
                    if (dist[n.Y, n.X] == -1)
                    {
                        q.Enqueue(n);
                        dist[n.Y, n.X] = dist[s.Y, s.X] + 1;
                    }
                }
            }
            if (dist[H - 1, W - 1] == -1)
            {
                Console.WriteLine(-1);
                return;
            }
            int ans = -dist[H - 1, W - 1];

            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    if (S[i][j] == '.')
                    {
                        ans++;
                    }
                }
            }
            Console.WriteLine(ans);
        }

        static void Main(string[] args)
        {
            new Program().Solve();
        }
    }
}

Measured memory_bytes: 13396