import java.util.ArrayDeque
import java.util.Queue

fun main(args: Array<String>) {

    val (h, w) = readLine()!!.split(" ").map(String::toInt)
    val map = Array(h) {
        readLine()!!.toCharArray()
    }

    val queue: Queue<Pair<Int, Int>> = ArrayDeque()
    val route = Array(h) { IntArray(w) { -1 } }
    queue.add(0 to 0)
    route[0][0] = 0

    val list: List<Pair<Int, Int>> = listOf(1 to 0, -1 to 0, 0 to 1, 0 to -1)

    while (queue.isNotEmpty()) {
        val (y, x) = queue.poll()

        for ((dy, dx) in list) {
            val ny = y + dy
            val nx = x + dx
            if (ny in 0 until h && nx in 0 until w && map[ny][nx] == '.' && route[ny][nx] == -1) {
                queue.add(ny to nx)
                route[ny][nx] = route[y][x] + 1
            }
        }
    }
    val result = route[h-1][w-1]
    if (result == -1) {
        println(-1)
    } else {
        val whites = map.sumBy { it.count { c -> c == '.' } } - 1
        println(whites - result)
    }
}

