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

namespace AtCoder
{

    static class Program
    {
        static void Main()
        {

            Solve();

            //Console.WriteLine(ans);
            Console.ReadKey();
        }

        static void Solve()
        {
            int H;int W;
            Multi(out H, out W);
            string[] S = new string[H].Select(s => GetStr()).ToArray();
            int[][] dist = new int[H][];
            dist = dist.Select(d => Enumerable.Repeat(-1, W).ToArray()).ToArray();
            Queue<Vector> que = new Queue<Vector>();
            dist[0][0] = 0;
            que.Enqueue(new Vector(0, 0));
            while (que.Count != 0)
            {
                var q = que.Dequeue();
                if (q.X > 0 && dist[q.Y][q.X - 1] == -1 && S[q.Y][q.X - 1] == '.')
                {
                    dist[q.Y][q.X - 1] = dist[q.Y][q.X] + 1;
                    que.Enqueue(new Vector(q.Y, q.X - 1));
                }
                if (q.X < W - 1 && dist[q.Y][q.X + 1] == -1 && S[q.Y][q.X + 1] == '.')
                {
                    dist[q.Y][q.X + 1] = dist[q.Y][q.X] + 1;
                    que.Enqueue(new Vector(q.Y, q.X + 1));
                }
                if (q.Y > 0 && dist[q.Y - 1][q.X] == -1 && S[q.Y - 1][q.X] == '.')
                {
                    dist[q.Y - 1][q.X] = dist[q.Y][q.X] + 1;
                    que.Enqueue(new Vector(q.Y - 1, q.X));
                }
                if (q.Y < H - 1 && dist[q.Y + 1][q.X] == -1 && S[q.Y + 1][q.X] == '.')
                {
                    dist[q.Y + 1][q.X] = dist[q.Y][q.X] + 1;
                    que.Enqueue(new Vector(q.Y + 1, q.X));
                }
            }
            int ans;
            if (dist[H - 1][W - 1] == -1)
            {
                ans = -1;
            }
            else
            {
                ans = S.Sum(s => s.Count(c=>c=='.')) - (dist[H - 1][W - 1] + 1);
            }

            Console.WriteLine(ans); 
        }

        class Vector
        {
            public Vector(int y,int x)
            {
                X = x;
                Y = y;
            }
            public int X;
            public int Y;
        }




        static public string GetStr() { return Console.ReadLine().Trim(); }
        static public int GetInt() { return int.Parse(Console.ReadLine()); }
        static public long GetLong() { return long.Parse(Console.ReadLine()); }
        static public string[] GetStrArray() { return Console.ReadLine().Split(' '); }
        static public int[] GetIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
        static public long[] GetLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
        static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); }
        static public void WriteObjects<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } }
        static string yesno(this bool b) { return b ? "yes" : "no"; }
        static string YesNo(this bool b) { return b ? "Yes" : "No"; }
        static string YESNO(this bool b) { return b ? "YES" : "NO"; }

        static bool eq<T, U>() => typeof(T).Equals(typeof(U));
        static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        static T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        static void Multi<T>(out T a) => a = cv<T>(GetStr());
        static void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        static void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
    }
}

Measured memory_bytes: 13780