import java.util.*


fun main(args: Array<String>) {
    val (h, w) = readIntList()
    val s = (1..h).map { readLine()!! }
    val map = (1..w).mapIndexed { index, i -> s.map { it[index] }.joinToString("").map { it == '.' } }
    val dist = (1..w).map { (1..h).map { -1 }.toMutableList() }
    val queue = LinkedList<Pair<Int, Int>>()
    queue.offer(0 to 0)
    val end = Pair(h, w)
    val moveX = listOf(0, 0, 1, -1)
    val moveY = listOf(1, -1, 0, 0)
    while (queue.size > 0) {
        val point = queue.poll()
        if (point == end) {
            break
        }
        for (i in 0..3) {
            val tempX = point.first + moveX[i]
            val tempY = point.second + moveY[i]
            if (tempX >= 0 && tempY >= 0 && tempX < w && tempY < h && map[tempX][tempY] && dist[tempX][tempY] == -1) {
                queue.offer(tempX to tempY)
                dist[tempX][tempY] = dist[point.first][point.second] + 1
            }
        }
    }
    val out = dist[w - 1][h - 1] + 2
    if (dist[w - 1][h - 1] == -1) {
        println(-1)
    } else {
        println(s.sumBy { it.count { it == '.' } } - out)
    }
}

fun printYesOrNo(flag: Boolean) {
    println(if (flag) "Yes" else "No")
}

fun readIntList(): List<Int> = readLine()!!.split(' ').map { it.toInt() }

fun readInt(): Int = readLine()!!.toInt()

fun err(print: Any) {
    System.err.println(print)
}