using System;
using static System.Console;
using System.Text;
using System.Linq;
using static System.Math;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var alg = new cp();
        var hw = alg.inputSplitInt();
        var w = hw[1];
        var h= hw[0];
        var meiro = alg.inputLinesString(h);
        var whitecount = 0;
        //白いマスをカウント
        for (int a = 0; a < h; a++)
        {
            for (int b = 0; b < w; b++)
            {
                if (meiro[a][b] == '.') whitecount++;
            }
        }
        var mincost = bfs(h,w,meiro);
        WriteLine(mincost==-1?-1:whitecount-mincost-1);

    }
    static int bfs(int h,int w, string[] meiro)
    {
        var cost = new List<List<int>>();
        for (int a = 0; a < h; a++)
        {
            cost.Add(new List<int>());
            for (int b = 0; b < w; b++)
            {
                cost[a].Add(-1);
            }
        }
        var dx = new int[] { -1,0,1,0};
        var dy = new int[] { 0,-1,0,1 };
        var que = new Queue<int[]>();
        que.Enqueue(new int[] { 0,0});
        cost[0][0] = 0;
        
        while (que.Count > 0)
        {
            var now = que.Peek();
            que.Dequeue();
            if (now[0] == h - 1 && now[1] == w - 1)
            {
                return cost[now[0]][now[1]];
            }

            for (int a = 0; a < 4; a++)
            {
                var nextY = now[0]+dy[a];
                var nextX = now[1]+dx[a];
                if (nextX < 0 || nextX >= w || nextY < 0 || nextY >= h) continue;
                if (meiro[nextY][nextX]=='#') continue;
                if (cost[nextY][nextX]!=-1) continue;
                cost[nextY][nextX]=cost[now[0]][now[1]] + 1;
                que.Enqueue(new int[] { nextY,nextX});
            }
        }
        return -1;
    }
}

class cp
{

    public cp()
    {
    }
    public int GCD(int a, int b)
    {
        return a % b == 0 ? b : GCD(b, a % b);
    }
    public int LCM(int a, int b)
    {
        return a * b / GCD(a, b);
    }
    public int countChar(string s, string key)
    {
        return s.Length - s.Replace(key, "").Length;
    }
    public string Yn(bool f)
    {
        if (f)
        {
            return "Yes";
        }
        else
        {
            return "No";
        }
    }
    public string YN(bool f)
    {
        if (f)
        {
            return "YES";
        }
        else
        {
            return "NO";
        }
    }
    public string yn(bool f)
    {
        if (f)
        {
            return "yes";
        }
        else
        {
            return "no";
        }
    }
    public void inputSkip()
    {
        ReadLine();
        return;
    }
    public string inputString()
    {
        return ReadLine();
    }
    public string[] inputSplitString()
    {
        return ReadLine().Split();
    }
    public string[] inputLinesString(int len)
    {
        var arr = new string[len];
        for (int a = 0; a < len; a++)
        {
            arr[a] = ReadLine();
        }
        return arr;
    }
    public string[][] inputJaggedArrayString(int len)
    {
        var arr = new string[len][];
        for (int a = 0; a < len; a++)
        {
            arr[a] = ReadLine().Split();
        }
        return arr;
    }
    public int inputInt()
    {
        return int.Parse(ReadLine());
    }
    public int[] inputSplitInt()
    {
        return ReadLine().Split().Select(i => int.Parse(i)).ToArray();
    }
    public int[] inputLinesInt(int len)
    {
        var arr = new int[len];
        for (int a = 0; a < len; a++)
        {
            arr[a] = int.Parse(ReadLine());
        }
        return arr;
    }
    public int[][] inputJaggedArrayInt(int len)
    {
        var arr = new int[len][];
        for (int a = 0; a < len; a++)
        {
            arr[a] = ReadLine().Split().Select(i => int.Parse(i)).ToArray();
        }
        return arr;
    }
    public long inputLong()
    {
        return long.Parse(ReadLine());
    }
    public long[] inputSplitLong()
    {
        return ReadLine().Split().Select(i => long.Parse(i)).ToArray();
    }
    public long[] inputLinesLong(int len)
    {
        var arr = new long[len];
        for (int a = 0; a < len; a++)
        {
            arr[a] = long.Parse(ReadLine());
        }
        return arr;
    }
    public long[][] inputJaggedArrayLong(int len)
    {
        var arr = new long[len][];
        for (int a = 0; a < len; a++)
        {
            arr[a] = ReadLine().Split().Select(i => long.Parse(i)).ToArray();
        }
        return arr;
    }
    public double inputDouble()
    {
        return double.Parse(ReadLine());
    }
    public double[] inputSplitDouble()
    {
        return ReadLine().Split().Select(i => double.Parse(i)).ToArray();
    }
    public double[] inputLinesDouble(int len)
    {
        var arr = new double[len];
        for (int a = 0; a < len; a++)
        {
            arr[a] = double.Parse(ReadLine());
        }
        return arr;
    }
    public double[][] inputJaggedArrayDouble(int len)
    {
        var arr = new double[len][];
        for (int a = 0; a < len; a++)
        {
            arr[a] = ReadLine().Split().Select(i => double.Parse(i)).ToArray();
        }
        return arr;
    }
}

Measured memory_bytes: 13524