import java.util.*

fun main(args: Array<String>) {
    val (H, W) = readLine()!!.split(" ").map { it.toInt() }
    val s = mutableListOf<CharArray>()
    (1..H).forEach {
        val s1 = readLine()!!.toCharArray()
        s.add(s1)
    }

    var whiteSize = 0
    s.forEach {
        it.forEach {
            if (it == '.') {
                whiteSize++
            }
        }
    }

    val queue : Queue<Pair<Int, Int>> = ArrayDeque()
    val distance = Array(H, { val array = IntArray(W); array.fill(-1); array })
    distance[0][0] = 0
    queue.add(Pair(0, 0))
    val dxList = listOf(-1, 0, 0, 1)
    val dyList = listOf(0, -1, 1, 0)

    while(!queue.isEmpty()) {
        val (h, w) = queue.poll()
        dxList.zip(dyList).forEach {
            val (dx, dy) = it
            if ((0 until  H).contains(h + dx) && (0 until W).contains(w + dy)) {
                if (s[h + dx][w + dy] == '.' && distance[h + dx][w + dy] == -1) {
                    distance[h + dx][w + dy] = distance[h][w] + 1
                    queue.add(Pair(h + dx, w + dy))
                }
            }
        }
    }

    if (distance[H -1][W-1] > 0) {
        println((whiteSize -2) - (distance[H -1][W-1] - 1))
    } else {
        println(-1)
    }
}
