import java.util.*

fun main(args: Array<String>) = yorukatsu8d()

fun yorukatsu8d() {
    val (h, w) = readLine()!!.split(' ').map { it.toInt() }
    val grid = (1..h).map { readLine()!!.map { it == '.' }.toBooleanArray() }.toTypedArray()

    val start = Yx(0, 0)
    val queue = ArrayDeque(listOf(start to 1))
    val visited = mutableSetOf(start)
    val distances = Array(h) { IntArray(w) }

    while (queue.isNotEmpty()) {
        val (yx, dist) = queue.poll()
        val (y, x) = yx
        distances[y][x] = dist
        around.map { Yx(y + it.first, x + it.second) }
            .filter { it.y in 0 until h && it.x in 0 until w && grid[it.y][it.x] && visited.add(it) }
            .map { it to dist + 1 }
            .let { queue.addAll(it) }
    }

    val answer = if (visited.contains(Yx(h - 1, w - 1))) {
        h * w - grid.map { it.count { !it } }.sum() - distances[h - 1][w - 1]
    } else -1

    println(answer)
}

private val around = listOf(-1 to 0, 1 to 0, 0 to -1, 0 to 1)

private data class Yx(val y: Int, val x: Int)
