object Main extends App {
	case class pos(val x: Int, val y:Int, val v: Int)

	val hw = scala.io.StdIn.readLine.split(" ").map(_.toInt)
	val s = Array.ofDim[Int](hw(0), hw(1))
	for(i<-0 until hw(0)){
		s(i) = scala.io.StdIn.readLine.toArray.map{case '#' => 1; case '.' => 0}
	}

	if(s(0)(0) == 1){
		println(-1)
	}else{

		val a = new scala.collection.mutable.Queue[pos]
		a += pos(0,0,1)

		var flag = true

		while(a.size > 0){
			val temp = a.dequeue

			if(temp.x == hw(1)-1 && temp.y == hw(0)-1){
				a.clear
				var count = 0
				for(i<-0 until hw(0)){
					count += s(i).sum
				}
				println(hw(0)*hw(1) - count - temp.v)
				flag = false
			}else if(temp.x == hw(1)-1){
				val x = temp.x
				val y = temp.y
				val v = temp.v

				if(s(x+0)(y+1) == 0) a+= pos(x+0, y+1, v+1)

			}else if(temp.y == hw(0)-1){
				val x = temp.x
				val y = temp.y
				val v = temp.v

				if(s(x+1)(y+0) == 0) a+= pos(x+1, y+0, v+1)

			}else{
				val x = temp.x
				val y = temp.y
				val v = temp.v

				if(s(x+1)(y+0) == 0) a+= pos(x+1, y+0, v+1)
				if(s(x+0)(y+1) == 0) a+= pos(x+0, y+1, v+1)
			}
		}

		if(flag) println(-1)
	}
}