data class HWC(val h: Int, val w: Int, val c: Int) {
    constructor(hwc: List<Int>): this(hwc[0], hwc[1], hwc[2])
}
fun main(args: Array<String>) {
    val dh = listOf(0, 1, 0, -1)
    val dw = listOf(1, 0, -1, 0)
    val (H, W) = listOfInt()
    var S = Array(H) { next().toCharArray() }
    val white = '.'; val black = '#'; val white2 = '@'
    var whiteCnt = S.sumBy { s -> s.count { it == white } }
    var goal = false
    val que = Queue<HWC>()
    que.enqueue(HWC(0, 0, 1))
    while (!que.isEmpty()) {
        val p = que.dequeue()!!
        val h = p.h; val w = p.w; val c = p.c
        if (S[h][w] != white) continue
        if (h == H - 1 && w == W - 1) {
            goal = true
            whiteCnt -= c
            break
        }
        for (n in 0 until 4) {
            val nh = h + dh[n]; val nw =  w + dw[n]
            if (nh < 0 || nw < 0 || nw > W - 1 || nh > H - 1) continue
            if (S[nh][nw] == white) {
                que.enqueue(HWC(nh, nw, c + 1))
                S[h][w] = white2
            }
        }
    }
    val ans = if (goal) whiteCnt else -1
    println(ans)
}
class Queue<T> {
    val elements = mutableListOf<T>()
    fun isEmpty() = elements.isEmpty()
    fun enqueue(item: T) = elements.add(item)
    fun dequeue() = if (!isEmpty()) elements.removeAt(0) else null
}

fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }


