Problem:
Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// using System.Numerics;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using ll=System.Int64;
using static Contest_D.Lib_IO;
using static Contest_D.Lib_Minifunc;



public static class Contest_D
{

	public static void Main() {
		checked{
			long h, w;
			Lib_IO.rm(out h, out w);
			string[] s;
			rl(h,out s);

			var e = GridToEdges(s);
			var d = Dijkstra(e, h*w, 0);

			long ans = -1;
			for (int y = 0; y < h; y++) for (int x = 0; x < w; x++)
			{
				if(s[y][x]=='.') ans++;
			}
			if(d[h*w-1]<int.MaxValue){
				ans -= d[h*w-1];
			}else{
				ans = -1;
			}

			Lib_IO.wm(ans);
		}
	}


	public static long[] Dijkstra(P<long,long,long>[] inEdges, long V, long start, out long[] prev) {
		long[] dist = Enumerable.Repeat(long.MaxValue, (int)V).ToArray();
		prev = Enumerable.Repeat(-1L, (int)V).ToArray();
		var G = new Dictionary<long,long>[V]; 
		for(int i=0;i<V;i++) G[i] = new Dictionary<long,long>();
		foreach(var e in inEdges){
			G[e.A][e.B] = e.C;
			G[e.B][e.A] = e.C;
		}

		var que = new PriorityQueue<KeyValuePair<long, long>>(V, (x,y)=>(x.Key.CompareTo(y.Key)));
		dist[start] = 0;
		que.Enqueue(new KeyValuePair<long, long>(dist[start], start));
		while(0<que.Count) {
			var q = que.Dequeue();
			if(dist[q.Value] < q.Key) continue;
			foreach(var e in G[q.Value]){
				var next_cost = q.Key + e.Value;
				if(dist[e.Key] <= next_cost) continue;
				dist[e.Key] = next_cost;
				que.Enqueue(new KeyValuePair<long, long>(dist[e.Key], e.Key));
			   	prev[e.Key] = q.Value;
			}
		}
		return dist;
	}
	public static long[] Dijkstra(P<long,long,long>[] inEdges, long V, long start){ long[] prev; return Dijkstra(inEdges, V, start, out prev); }

	public static P<long,long,long>[] GridToEdges(string[] grid){
		char pathchar = '.';
		int h = grid.Length;
		int w = grid[0].Length;
		var edges = new List<P<long, long, long>>();
		for (int y = 0; y < h; y++) for (int x = 0; x < w; x++)
		{
			if(grid[y][x]!=pathchar) continue;
			long k = y*w+x;
			if(0<y   && grid[y-1][x]==pathchar) edges.Add(new P<long, long, long>(k, (y-1)*w+x, 1));
			if(y<h-1 && grid[y+1][x]==pathchar) edges.Add(new P<long, long, long>(k, (y+1)*w+x, 1));
			if(0<x   && grid[y][x-1]==pathchar) edges.Add(new P<long, long, long>(k, y*w+x-1, 1));
			if(x<w-1 && grid[y][x+1]==pathchar) edges.Add(new P<long, long, long>(k, y*w+x+1, 1));
		}
		return edges.ToArray();
	}


public class PriorityQueue<T>
{
	private readonly List<T> heap;
	private readonly Comparison<T> compare;
	private int size;
	public PriorityQueue() : this(Comparer<T>.Default) {} 
	public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
	public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
	public PriorityQueue(long capacity, Comparison<T> comparison)
	{
		this.heap = new List<T>((int)capacity);
		this.compare = comparison;
	}
	public void Enqueue(T item)
	{
		this.heap.Add(item);
		var i = size++;
		while (i > 0)
		{
			var p = (i - 1) >> 1;
			if (compare(this.heap[p], item) <= 0)
				break;
			this.heap[i] = heap[p];
			i = p;
		}
		this.heap[i] = item;

	}
	public T Dequeue()
	{
		var ret = this.heap[0];
		var x = this.heap[--size];
		var i = 0;
		while ((i << 1) + 1 < size)
		{
			var a = (i << 1) + 1;
			var b = (i << 1) + 2;
			if (b < size && compare(heap[b], heap[a]) < 0) a = b;
			if (compare(heap[a], x) >= 0)
				break;
			heap[i] = heap[a];
			i = a;
		}
		heap[i] = x;
		heap.RemoveAt(size);
		return ret;
	}
	public T Peek() { return heap[0]; }
	public long Count { get { return size; } }
	public bool Any() { return size > 0; }
}












#region BaseModule


public static class Lib_Minifunc{
	[MethodImpl(256)] public static void swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	[MethodImpl(256)] public static void swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換
	[MethodImpl(256)] public static bool chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	[MethodImpl(256)] public static bool chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	[MethodImpl(256)] public static bool inside(long i, long n) => (0<=i&&i<n);
	[MethodImpl(256)] public static bool inside(long x, long y, long w, long h) => (inside(x,w)&&inside(y,h));
	[MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable => a.Min();
	[MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable => a.Max();
	[MethodImpl(256)] public static long mod(long a, long m=MOD1) { var v = a%m; return (v<0?v+m:v); }
	[MethodImpl(256)] public static long ceiling(long a, long b) => (a%b==0?a/b:a/b+1);	// 整数商の切り上げ
	[MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	[MethodImpl(256)] public static bool initd<T,U>(Dictionary<T,U> d, T k) { if(d.ContainsKey(k)) { return false; } else { d[k] = default(U); return true; } }
	public static T[] inita<T>(long len1) where T : new() { var m = new T[len1]; for (int i=0;i<m.Length;i++) m[i] = new T(); return m; }
	public static T[][] initm<T>(long len1,long len2,T val) where T : struct { var m = new T[len1][]; for (int i=0;i<m.Length;i++) m[i] = Enumerable.Repeat(val,(int)len2).ToArray(); return m; }
	public static T[][][] initm<T>(long len1,long len2,long len3,T val) where T : struct { var m = new T[len1][][]; for (int i=0;i<m.Length;i++) m[i] = initm(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
}

public struct P<T,U>
{
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V>
{
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W>
{
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO
{
	class Prt : System.IO.StreamWriter
	{
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = z.Select(cv<T>).ToArray();
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			foreach(T x in a) Console.WriteLine(f,x);
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
				Console.WriteLine(f,b[b.Count-1]);
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}