fun main(args: Array<String>) {
    val sc = java.util.Scanner(System.`in`)
    val h = sc.nextInt()
    val w = sc.nextInt()

    data class Pos(val x: Int, val y: Int)

    val a = sc.asSequence().take(h).map { line -> line.toCharArray().map { if (it == '#') -1 else 0 }.toIntArray() }.toList()
    val q: java.util.Queue<Pair<Pos, Int>> = java.util.LinkedList<Pair<Pos, Int>>()

    fun op(p: Pos, d: Int) {
        if (!(p.x in 0 until w && p.y in 0 until h && a[p.y][p.x] != -1)) {
            return
        }
        if (a[p.y][p.x] > 0) {
            return
        }
        a[p.y][p.x] = d
        q.add(Pos(p.x + 1, p.y) to d + 1)
        q.add(Pos(p.x - 1, p.y) to d + 1)
        q.add(Pos(p.x, p.y + 1) to d + 1)
        q.add(Pos(p.x, p.y - 1) to d + 1)
    }

    q.add(Pos(0, 0) to 1)
    while (q.isNotEmpty()) {
        val (p, d) = q.poll()
        op(p, d)
    }

    val e = a[h - 1][w - 1]
    if (e == 0) {
        println(-1)
        return
    }
    val walls = a.map { line -> line.filter { it == -1 }.sum() }.sum()
    println(h * w + walls - e)
}
