#pragma warning disable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Diagnostics;

static class MainClass
{
    public static void Main()
    {
        var hw = Console.ReadLine().SplittedStringToInt32List();
        var dh = new int[] { 1,0,0,-1 };
        var dw = new int[] { 0,-1,1,0};
        var h = hw[0];
        var w = hw[1];
        var roads = new bool[h, w];

        var ansRoads = new int[h, w];

        var wallCount = 0;
        for (int i = 0; i < h; i++)
        {
            var count = 0;
            Console.ReadLine()
                .ToList()
                .ForEach(x =>
                {
                    var iso = x == '.';
                    if (!iso)
                        wallCount++;
                    roads[i, count] = iso;
                    ansRoads[i, count] = -1;
                    count++;
                });
            
        }
        ansRoads[0, 0] = 1;
        var queue = new Queue<KeyValuePair<int, int>>();
        KeyValuePair<int, int> item;
        queue.Enqueue(new KeyValuePair<int, int>(0, 0));
        while (true)
        {
            try { item = queue.Dequeue(); } catch { break; }
            for (int i = 0; i < 4; i++)
            {
                var hhh = item.Key + dh[i];
                var www = item.Value + dw[i];
                if (hhh < 0 || www < 0 || hhh >= h || www >= w)
                    continue;
                if (roads[hhh,www] && ansRoads[hhh,www] == -1)
                {
                    ansRoads[hhh, www] = ansRoads[item.Key, item.Value] + 1;
                    queue.Enqueue(new KeyValuePair<int, int>(hhh, www));
                }

            }
        }
        if (ansRoads[h-1,w-1] == -1)
            Console.WriteLine(-1);
        else
        {
            Console.WriteLine(h*w - ansRoads[h-1,w-1] - wallCount);
        }
        Console.ReadLine();
    }

    #region ライブラリ
    #region 二分探索
    public static int LowerBound<T>(this IEnumerable<T> ar, int start, int end, T value, IComparer<T> comparer)
    {
        var arr = ar.ToArray();
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) < 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int LowerBound<T>(this IEnumerable<T> arr, T value) where T : IComparable
    {
        return LowerBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }

    public static int UpperBound<T>(this IEnumerable<T> ar, int start, int end, T value, IComparer<T> comparer)
    {
        var arr = ar.ToArray();
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) <= 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int UpperBound<T>(this IEnumerable<T> arr, T value)
    {
        return UpperBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }
    #endregion
    #region　bit全探索
    /// <summary>
    /// Bit全探索用のライブラリ
    /// </summary>
    /// <param name="len">作る配列の長さ(lenが0以下だと（ｒｙ)</param>
    /// <returns>できた配列</returns>
    public static List<List<bool>> CreateBits(int len)
    {
        return CreateBitsPri(new List<List<bool>>(), len, 0);
    }

    private static List<List<bool>> CreateBitsPri(List<List<bool>> bits, int len, int count)
    {
        if (count == 0)
        {
            var lss = new List<bool>();
            lss.Add(true);
            bits.Add(lss);
            var lx = new List<bool>();
            lx.Add(false);
            bits.Add(lx);
            count++;
            return CreateBitsPri(bits, len, count);
        }
        if (len - count == 0)
            return bits;
        count++;
        var ls = new List<List<bool>>();
        foreach (var item in bits)
        {
            var x = item.ToList();
            x.Add(true);
            item.Add(false);
            ls.Add(item);
            ls.Add(x);
        }
        return CreateBitsPri(ls, len, count);
    }
    #endregion
    #region 文字列処理

    public static int ToInt32(this string str)
    {
        return int.Parse(str);
    }
    public static List<string> SplittedStringToList(this string str)
    {
        return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
            .ToList();
    }
    public static List<int> SplittedStringToInt32List(this string str)
    {
        return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
    }
    #endregion
    #region エラトステネスの篩
    public static List<int> GeneratePrimes(int max)
    {

        var range = Enumerable.Range(2, max - 1).ToList();
        var count = range.Count();
        var ls = new List<int>();
        while (true)
        {
            int num = 0;
            try
            {
                num = range.First();
                ls.Add(num);
            }
            catch
            {
                break;
            }
            for (int i = 1; i * num <= max; i++)
            {
                range.Remove(i * num);
            }
            if (range.Count <= 0)
            {
                break;
            }
        }
        return ls;
    }
    #endregion
    #region DeepClone
    public static T DeepClone<T>(this T src)
    {
        using (var memoryStream = new MemoryStream())
        {
            var binaryFormatter
              = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, src);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return (T)binaryFormatter.Deserialize(memoryStream);
        }
    }
    #endregion
    #endregion
}

Measured memory_bytes: 13632