import java.util.ArrayDeque

fun main() {
    fun readString():String{return readLine()!!}
    fun readIntList():List<Int>{return readLine()!!.split(" ").map { it.toInt() }}
    val (h,w)=readIntList()
    val grid=Array(h){readString().toCharArray()}
    var que=ArrayDeque<List<Int>>()
    que.add(listOf(0,0,1))
    var visited=Array(h){BooleanArray(w){false}}
    visited[0][0]=true
    var ng=0
    grid.forEach { i->i.forEach { j->if (j=='#'){ng++} } }
    var road=-1
    val around= listOf(-1 to 0, 1 to 0, 0 to -1, 0 to 1)
    loop@while (!que.isEmpty()){
        val now=que.poll()
        for ((dx, dy) in around){
            val tmp= listOf(now[0]+dx, now[1]+dy)
            if (tmp[0] in 0 until h && tmp[1] in 0 until w && !visited[tmp[0]][tmp[1]] && grid[tmp[0]][tmp[1]]=='.'){
                if (tmp[0]==h-1&&tmp[1]==w-1){road=now[2]+1; break@loop}
                que.add(listOf(tmp[0], tmp[1], now[2]+1))
                visited[tmp[0]][tmp[1]]=true
            }
        }
    }
    if (road==-1){println(-1)}
    else{println(w*h-ng-road)}
}