using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using static System.Math;
using static System.Array;
using static AtCoder.Tool;
namespace AtCoder
{
    class AC
    {
        const int mod = 1000000007;
        const int INF = int.MaxValue / 2;
        const long SINF = long.MaxValue / 2;
        static readonly int[] dx = { 0, 1, 0, -1 };
        static readonly int[] dy = { 1, 0, -1, 0 };
        

        static void Main(string[] args)
        {
            //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(sw);
            var cin = new Scanner();

            var input = cin.SplitRead();
            int H = input[0];
            int W = input[1];
            var map = new char[H + 2, W + 2];
            long white = 0;
            for(var i = 0; i <= H + 1; i++)
            {
                if (i == 0 || i == H + 1)
                {
                    for(var j = 0; j <= W + 1; j++) { map[i, j] = '#'; }
                }
                else
                {
                    string s = Console.ReadLine();
                    for(var j = 0; j <= W + 1; j++)
                    {
                        if (j == 0 || j == W + 1) { map[i, j] = '#'; }
                        else { map[i, j] = s[j - 1];
                            if (s[j - 1] == '.') { white++; }
                        }
                        
                    }
                }
            }
            var bfs = new Queue<Tuple<int, int>>();
            bfs.Enqueue(new Tuple<int, int>(1, 1));

            var seen = new bool[H + 2, W + 2];
            var dist = new int[H + 2, W + 2];
            for(var i = 0; i <= H + 1; i++)
            {
                for(var j = 0; j <= W + 1; j++)
                {
                    seen[i, j] = false;
                    dist[i, j] = -1;
                }
            }
            seen[1, 1] = true;
            dist[1, 1] = 0;

            while (bfs.Count() > 0)
            {
                var xy = bfs.Dequeue();
                for(var i = 0; i < 4; i++)
                {
                    int nx = xy.Item1 + dx[i];
                    int ny = xy.Item2 + dy[i];
                    if (map[nx, ny] == '#'||seen[nx,ny]) { continue; }

                    seen[nx, ny] = true;
                    dist[nx, ny] = dist[xy.Item1, xy.Item2] + 1;
                    bfs.Enqueue(new Tuple<int, int>(nx, ny));
                }
            }

            if (!seen[H, W]) { Console.WriteLine(-1); }
            else { Console.WriteLine(white - dist[H, W] - 1); }


            //Console.Out.Flush();
        }
        
    }
    
    public class Scanner
    {
        public int[] SplitRead()
        {
            int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return array;
        }
        public long[] SplitReadL()
        {
            long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return array;
        }
    }

    public static class Tool
    {
        static public void Initialize<T>(ref T[] array, T initialvalue)
        {
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = initialvalue;
            }
        }
    }
}


Measured memory_bytes: 13524