package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()
	H := readInt() + 2
	W := readInt() + 2
	s := make([][]byte, H)
	dist := make([][]int, H)
	var dots int
	for i := range s {
		dist[i] = make([]int, W)
		s[i] = make([]byte, 0, W)
		s[i] = append(s[i], '#')
		if i == 0 || i == H-1 {
			for j := 0; j < W-2; j++ {
				s[i] = append(s[i], '#')
			}
		} else {
			s[i] = append(s[i], readBytes()...)
		}
		s[i] = append(s[i], '#')
		for j := range dist[i] {
			if s[i][j] == '.' {
				dots++
			}
			dist[i][j] = 1000000007
		}
	}

	q := make([]struct{ x, y int }, 0)
	push := func(y, x int) {
		q = append(q, struct{ x, y int }{x, y})
	}
	pop := func() (y, x int) {
		res := q[0]
		q = q[1:]
		return res.y, res.x
	}

	push(1, 1)
	dist[1][1] = 1

	dx := []int{-1, 0, 1, 0}
	dy := []int{0, -1, 0, 1}

	for len(q) != 0 {
		y, x := pop()
		d := dist[y][x]
		if y == H-2 && x == W-2 {
			break
		}
		for i := range dx {
			ny := y + dy[i]
			nx := x + dx[i]
			nd := d + 1
			if s[ny][nx] == '#' || dist[ny][nx] <= nd {
				continue
			}
			dist[ny][nx] = nd
			push(ny, nx)
		}
	}
	println(max(dots-dist[H-2][W-2], -1))
}

// -----------------------------------------------------------------------------


Measured memory_bytes: 768