import java.util.*

fun main(args: Array<String>) {
    val (H, W) = readIntList()
    val map = Array(H) { readString() }
    val sumW =map.map { it.count{ it == '.'} }.sum()

    val dist = BFS(0 to 0, H-1 to W - 1, map)
    println(if (dist == null) -1 else sumW - dist - 1)
}

fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)
fun powInt(a: Int, b: Int) = Math.pow(a.toDouble(), b.toDouble()).toInt()


val DIR_4 = listOf(
    -1 to 0,
    1 to 0,
    0 to -1,
    0 to 1
)

fun Array<String>.mapSearch(ch: Char): Pair<Int, Int> {
    for ((h, line) in this.withIndex())
        for ((w, c) in line.withIndex())
            if (ch == c) return h to w
    return -1 to -1
}


fun BFS(
    s: Pair<Int, Int>,
    g: Pair<Int, Int>,
    map: Array<String>,
    wall:Char = '#',
    dir:List<Pair<Int, Int>> = DIR_4
):Int? {
    val set = mutableSetOf<Pair<Int, Int>>()
    val que = ArrayDeque<Pair<Int, Int>>()
    que.add(s)
    var res = 0

    while (que.isNotEmpty()) {
        val tmpQue = ArrayDeque<Pair<Int, Int>>()
        while (que.isNotEmpty()) {
            val hw = que.pop()
            val (h, w) = hw
            if (h !in map.indices || w !in map[0].indices) continue
            if (map[h][w] == wall) continue
            if (hw in set) continue
            if (hw == g) return res

            set.add(hw)
            tmpQue.addAll(dir.map { it.first + h to it.second + w })
        }
        que.addAll(tmpQue)
        res++
    }

    return null
}
