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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

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

public class Program
{
    static public void Main()
    {
        var s = new Sol();
        //s.Solve1();
        //s.Solve2();
        s.Solve3();
        s.Solve4();
        s.Solve5();
        s.Solve6();
    }
}
public class Sol
{
    //ABC 007 C 幅優先探索
    //https://atcoder.jp/contests/abc007/tasks/abc007_3
    //public void Solve1()
    //{
    //    int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    //    int r = In[0];
    //    int c = In[1];
    //    int[] In2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    //    int sy = In2[0];
    //    int sx = In2[1];
    //    sy--;sx--;
    //    int[] In3 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    //    int gy = In3[0];
    //    int gx = In3[1];
    //    gy--;gx--;
    //    var C = new char[r, c];
    //    for(int i = 0; i < r; i++)
    //    {
    //        string s = Console.ReadLine();
    //        for(int j = 0; j < c; j++)
    //        {
    //            C[i, j] = s[j];
    //        }
    //    }
    //    var que = new Queue<Pair>();
    //    que.Enqueue(new Pair(sy, sx));
    //    var dis = new int[r, c];
    //    for(int i = 0; i < r; i++)
    //    {
    //        for(int j = 0; j < c; j++)
    //        {
    //            dis[i, j] = -1;
    //        }
    //    }
    //    dis[sy, sx] = 0;
    //    while (que.Count > 0)
    //    {
    //        Pair p = que.Dequeue();
    //        //Console.WriteLine($"{p.y} {p.x}");
    //        if (p.y == gy && p.x == gx)
    //        {
    //            Console.WriteLine(dis[p.y, p.x]);
    //            break;
    //        }
    //        for (int i=0; i < 4; i++)
    //        {
    //            int ny = p.y + dy[i];
    //            int nx = p.x + dx[i];
    //            if (ny < 0 || r <= ny || nx < 0 || c <= nx || C[ny,nx]=='#' || dis[ny,nx]!=-1) continue;
    //            dis[ny, nx] = dis[p.y, p.x] + 1;
    //            que.Enqueue(new Pair(ny, nx));
    //        }
    //    }
    //}
    //public struct Pair
    //{
    //    public int y,x;
    //    public Pair(int y, int x)
    //    {
    //        this.x = x;
    //        this.y = y;
    //    }
    //}
    public static int[] dx = new int[] { 1, 0, -1, 0 };
    public static int[] dy = new int[] { 0, 1, 0, -1 };

    //JOI 2010　予選E チーズ
    //https://atcoder.jp/contests/joi2011yo/tasks/joi2011yo_e
    public void Solve2()
    {
        int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int h = In[0];
        int w = In[1];
        int n = In[2];
        var C = new char[h, w];
        for(int i = 0; i < h; i++)
        {
            string s = Console.ReadLine();
            for (int j = 0; j < w; j++)
            {
                C[i, j] = s[j];
            }
        }
        var dis = new int[h, w];
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = -1;
        var que = new Queue<Pair>();
        //Sを探す
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++)
                if (C[i, j] == 'S') { que.Enqueue(new Pair(i, j)); C[i, j] = '.'; dis[i, j] = 0; }
        int HP = 1;
        int ans = 0;
        while (que.Count > 0 && n>0)
        {
            var p = que.Dequeue();
            //Console.WriteLine($"{p.x} {p.y}");
            char m = C[p.x, p.y];
            if('1' <= m && m <= '9' && int.Parse(m.ToString()) <= HP)
            {
                //Console.WriteLine("!");
                ans += dis[p.x, p.y];
                for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = -1;
                C[p.x, p.y] = '.';
                dis[p.x, p.y] = 0;
                n--;
                HP++;
                que.Clear();
                que.Enqueue(p);
                continue;
            }
            for(int i = 0; i < 4; i++)
            {
                int nx = p.x + dx[i];
                int ny = p.y + dy[i];
                if (nx < 0 || h <= nx || ny < 0 || w <= ny || C[nx, ny] == 'X' || dis[nx, ny] != -1) continue;
                dis[nx, ny] = dis[p.x, p.y] + 1;
                que.Enqueue(new Pair(nx, ny));
            }
        }
        Console.WriteLine(ans);
    }
    public struct Pair
    {
        public int x, y;
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    //ABC 088 D grid Repainting
    //https://atcoder.jp/contests/abc088/tasks/abc088_d
    public void Solve3()
    {
        int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int h = In[0];
        int w = In[1];
        var C = new char[h, w];
        int wcnt = 0;
        for(int i = 0; i < h; i++)
        {
            string s = Console.ReadLine();
            for(int j = 0; j < w; j++)
            {
                C[i, j] = s[j];
                if (s[j] == '.') wcnt++;
            }
        }
        var dis = new int[h, w];
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = -1;
        var que = new Queue<Pair>();
        que.Enqueue(new Pair(0, 0));
        dis[0, 0] = 0;
        int min=1001001001;
        while (que.Count > 0)
        {
            Pair p = que.Dequeue();
            if(p.x == h - 1 && p.y == w - 1)
            {
                min = dis[p.x, p.y];
                break;
            }
            for(int i = 0; i < 4; i++)
            {
                int nx = p.x + dx[i];
                int ny = p.y + dy[i];
                if (nx < 0 || h <= nx || ny < 0 || w <= ny || C[nx, ny] == 'X' || dis[nx, ny] != -1) continue;
                dis[nx, ny] = dis[p.x, p.y] + 1;
                que.Enqueue(new Pair(nx, ny));
            }
        }
        Console.WriteLine(wcnt - min - 1);
    }

    //A
    //
    public void Solve4()
    {

    }

    //
    //
    public void Solve5()
    {

    }

    //
    //
    public void Solve6()
    {

    }
}