using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace pozar
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);

            string[,] Steva = new string[n, m];
            string[,] Vatra = new string[n, m];

            int x = 0;
            int y = 0;
            int xV = 0;
            int yV = 0;

            bool imaVatre = false;

            for (int i = 0; i < n; i++)
            {
                char[] s1 = Console.ReadLine().ToCharArray();
                for (int j = 0; j < m; j++)
                {
                    if (s1[j] == 'S')
                    {
                        x = i;
                        y = j;
                    }
                    else if (s1[j] == 'X')
                    {
                        xV = i;
                        yV = j;
                        imaVatre = true;
                    }
                    Steva[i, j] = s1[j].ToString();
                    Vatra[i, j] = s1[j].ToString();
                }
            }

            Steva[x, y] = "0";
            Path(x, y, ref Steva, n, m, 0);//Steva 

            int br = 0; ;
            if (imaVatre)
            {
                Vatra[xV, yV] = "0";
                Path(xV, yV, ref Vatra, n, m, 0);//Vatra
                br = StevaIliVatra(n, m, Steva, Vatra);
            }
            else
            {
                for(int i = 0; i < n; i++)
                {
                    for (int j = 0;j < m; j++)
                    {
                        if (int.TryParse(Steva[i,j], out int k))
                        {
                            br++;
                        }
                    }
                }
            }
            
            Console.WriteLine(br);
        }

        static void Path(int x, int y, ref string[,] lavirint, int n, int m, int From)//from=0
        {
            int k;
            ///////////////////////
            if (From != 3 && check(x, y + 1, n, m) && (lavirint[x, y + 1] == "." || (int.TryParse(lavirint[x, y + 1], out k) && int.Parse(lavirint[x, y]) + 1 < k))) //down
            {
                lavirint[x, y + 1] = (int.Parse(lavirint[x, y]) + 1).ToString();
                Path(x, y + 1, ref lavirint, n, m, 1);
            }
            ///////////////////////
            if (From != 4 && check(x + 1, y, n, m) && (lavirint[x + 1, y] == "." || (int.TryParse(lavirint[x + 1, y], out k) && int.Parse(lavirint[x, y]) + 1 < k))) //right
            {
                lavirint[x + 1, y] = (int.Parse(lavirint[x, y]) + 1).ToString();
                Path(x + 1, y, ref lavirint, n, m, 2);
            }
            //////////////////////
            if (From != 1 && check(x, y - 1, n, m) && (lavirint[x, y - 1] == "." || (int.TryParse(lavirint[x, y - 1], out k) && int.Parse(lavirint[x, y]) + 1 < k))) //up
            {
                lavirint[x, y - 1] = (int.Parse(lavirint[x, y]) + 1).ToString();
                Path(x, y - 1, ref lavirint, n, m, 3);
            }
            /////////////////////
            if (From != 2 && check(x - 1, y, n, m) && (lavirint[x - 1, y] == "." || (int.TryParse(lavirint[x - 1, y], out k) && int.Parse(lavirint[x, y]) + 1 < k))) //left
            {
                lavirint[x - 1, y] = (int.Parse(lavirint[x, y]) + 1).ToString();
                Path(x - 1, y, ref lavirint, n, m, 4);
            }
        }
        static bool check(int x, int y, int n, int m)
        {
            if(x < n && y < m && x >= 0 && y >= 0) return true;
            return false;
        }

        static int StevaIliVatra(int n, int m, string[,] Steva, string[,] Vatra)
        {
            bool[,] Moze = new bool[n, m];
            int k;
            int l;
            int br = 0;
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    if (int.TryParse(Steva[i, j], out k))
                    {
                        if (int.TryParse(Vatra[i, j], out l))
                        {
                            if (k < l) br++;
                        }
                        else br++;
                    }
                }
            }
            return br;
        }
    }
}