using System;
using System.Collections.Generic;

class Program
{
    static int[] dr = { -1, 1, 0, 0 };
    static int[] dc = { 0, 0, -1, 1 };

    static void Main()
    {
        var d = Console.ReadLine().Split();
        int n = int.Parse(d[0]);
        int m = int.Parse(d[1]);

        char[,] l = new char[n, m];
        Queue<(int, int)> rv = new Queue<(int, int)>();
        Queue<(int, int)> rs = new Queue<(int, int)>();

        (int, int) sp = (-1, -1);

        for (int i = 0; i < n; i++)
        {
            string r = Console.ReadLine();
            for (int j = 0; j < m; j++)
            {
                l[i, j] = r[j];
                if (l[i, j] == 'X')
                {
                    rv.Enqueue((i, j));
                }
                else if (l[i, j] == 'S')
                {
                    sp = (i, j);
                    rs.Enqueue((i, j));
                }
            }
        }

        int[,] vv = new int[n, m];
        int[,] sv = new int[n, m];

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                vv[i, j] = -1;
                sv[i, j] = -1;
            }
        }

        int vd = 0;
        while (rv.Count > 0)
        {
            int sz = rv.Count;
            for (int i = 0; i < sz; i++)
            {
                var (r, c) = rv.Dequeue();
                if (vv[r, c] != -1) continue;
                vv[r, c] = vd;

                for (int k = 0; k < 4; k++)
                {
                    int nr = r + dr[k];
                    int nc = c + dc[k];
                    if (nr >= 0 && nr < n && nc >= 0 && nc < m && l[nr, nc] == '.' && vv[nr, nc] == -1)
                    {
                        rv.Enqueue((nr, nc));
                    }
                }
            }
            vd++;
        }

        int sd = 0;
        sv[sp.Item1, sp.Item2] = sd;

        while (rs.Count > 0)
        {
            int sz = rs.Count;
            for (int i = 0; i < sz; i++)
            {
                var (r, c) = rs.Dequeue();
                for (int k = 0; k < 4; k++)
                {
                    int nr = r + dr[k];
                    int nc = c + dc[k];
                    if (nr >= 0 && nr < n && nc >= 0 && nc < m && l[nr, nc] == '.' && sv[nr, nc] == -1)
                    {
                        if (vv[nr, nc] == -1 || vv[nr, nc] > sv[r, c] + 1)
                        {
                            sv[nr, nc] = sv[r, c] + 1;
                            rs.Enqueue((nr, nc));
                        }
                    }
                }
            }
            sd++;
        }

        int br = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (l[i, j] == '.' && sv[i, j] != -1 && (vv[i, j] == -1 || vv[i, j] > sv[i, j]))
                {
                    br++;
                }
            }
        }

        Console.WriteLine(br+1);
    }
}