import java.awt.Point
import java.util.*

fun main(args:Array<String>) {
    val (h,w) = readLine()!!.split(" ").map { it.toInt() }
    val map = (1..h).map { readLine()!! }
    val step = (1..h).map { (1..w).map { MapState( h*w+1, null) }.toMutableList() }
    step[0][0] = MapState(0, null)
    val queue = LinkedList<Pos>()
    queue.push(Pos(0,0))
    while (queue.isNotEmpty()) {
        val current = queue.pop()
        for(i in (Math.max(0, current.r - 1)..Math.min(h-1, current.r + 1))) {
            for(j in Math.max(0, current.c -1)..Math.min(w-1, current.c + 1)) {
                if(i == current.r && j == current.c) {
                    continue
                }
                if(i != current.r && j != current.c) {
                    continue
                }
                if(map[i][j] == '#') {
                    continue
                }
                if(step[i][j].step <= step[current.r][current.c].step + 1) {
                    continue
                }
                step[i][j] = MapState(step[current.r][current.c].step + 1, current)
                queue.push(Pos(i,j))
            }
        }
    }
    if(step.last().last().step == h*w+1) {
        println(-1)
        return
    }
    val flag = (1..h).map { (1..w).map { false }.toMutableList() }
    queue.push(Pos(h-1,w-1))
    while (queue.isNotEmpty()) {
        val current = queue.pop()
        flag[current.r][current.c] = true
        step[current.r][current.c].from?.let { queue.push(it) }
    }
    var ans = 0
    for(i in (0 until h)) {
        for(j in (0 until w)) {
            if(map[i][j] == '#') {
                continue
            }
            if(flag[i][j]) {
                continue
            }
            ans++
        }
    }
    println(ans)

}

class MapState(val step:Int, val from:Pos?)
class Pos(val r:Int, val c:Int)
