object Main extends App {
	private val Empty = Int.MaxValue - 1
	private val Wall = Int.MaxValue

	private def pre(inputs: Seq[String]) = {
		val g = inputs.head.split(' ').map(_.toInt)
		val total = g(0) * g(1)
		val w = inputs.map(_.count(_ == '#')).sum //TODO '.'に変えてみる？
		val min = solve(inputs)
		val ret = if (min < 0) -1 else total - w - min - 1
		//println(ret)
		assert(-1 <= ret) //FIXME RE
		//assert(-1 != ret) //FIXME RE
		//assert(ret < Empty)
		ret
	}

	private def solve(inputs: Seq[String]): Int = {
		val g = inputs.head.split(' ').map(_.toInt)
		val sy = 1
		val sx = 1
		val gy = g(0)
		val gx = g(1)
		val ffff = inputs.tail.map(_.toCharArray.map(e => if (e == '#') Wall else Empty).toArray).toArray
		val fff = ffff.map(Wall +: _ :+ Wall)
		val wall = fff(0).map(_ => Wall)
		val f = wall +: fff :+ wall

		val q = scala.collection.mutable.Queue[(Int, Int)]()
		q.enqueue((sx, sy))
		f(sy)(sx) = -1
		do {
			val (ny, nx) = q.dequeue()
			if (f(ny)(nx) == -1 || f(ny)(nx) == Empty) {
				//f.foreach(e => println(e.toSeq.filterNot(_ == Empty)))

				f(ny)(nx) = 1 + Math.min(f(ny)(nx),
					Math.min(
						Math.min(f(ny - 1)(nx), f(ny + 1)(nx)),
						Math.min(f(ny)(nx - 1), f(ny)(nx + 1))))

				q.enqueue((ny - 1, nx))
				q.enqueue((ny + 1, nx))
				q.enqueue((ny, nx - 1))
				q.enqueue((ny, nx + 1))
			}
			//if (nx == gx && ny == gy) return if (f(gy)(gx) >= Empty) -1 else f(gy)(gx) //q.clear()
			if (nx == gx && ny == gy) return f(gy)(gx) //q.clear()
		} while (q.nonEmpty)

		-1
	}

	private val s1 =
		"""3 3
			|..#
			|#..
			|...
			|""".stripMargin.split("\r\n")
	private val s2 =
		"""10 37
			|.....................................
			|...#...####...####..###...###...###..
			|..#.#..#...#.##....#...#.#...#.#...#.
			|..#.#..#...#.#.....#...#.#...#.#...#.
			|.#...#.#..##.#.....#...#.#.###.#.###.
			|.#####.####..#.....#...#..##....##...
			|.#...#.#...#.#.....#...#.#...#.#...#.
			|.#...#.#...#.##....#...#.#...#.#...#.
			|.#...#.####...####..###...###...###..
			|.....................................
			|""".stripMargin.split("\r\n")
	private val s3 =
		"""10 37
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|.....................................
			|""".stripMargin.split("\r\n")
	private val s4 =
		"""5 37
			|.....................................
			|####################################.
			|.....................................
			|.####################################
			|.....................................
			|""".stripMargin.split("\r\n")
	private val s5 =
		"""5 37
			|.....................................
			|####################################.
			|.....................................
			|.####################################
			|................#....................
			|""".stripMargin.split("\r\n")
	assert(pre(s1) == 2)
	assert(pre(s2) == 209)
	assert(pre(s3) == 370 - 10 - 37 + 1)
	assert(pre(s4) == 0)
	assert(pre(s5) == -1)

	println(pre(scala.io.Source.stdin.getLines().toSeq))
}


Measured memory_bytes: 28492