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

class Program
{
    static string unos(char[,] a)
    {
        string res1 = "";
        string res2 = "";
        bool imaRes = false;
        for (int i = 0; i < a.GetLength(0); i++)
        {
            string s = Console.ReadLine();
            for (int j = 0; j < a.GetLength(1); j++)
            {
                a[i, j] = s[j];
                if (s[j] == 'S')
                {
                    res1 += i.ToString();
                    res1 += j.ToString();
                }
                if (s[j] == 'X')
                {
                    imaRes = true;
                    res2 += i.ToString();
                    res2 += j.ToString();
                }
            }
        }
        if(imaRes) return res1 + res2 + "1";
        else return res1 + res2;
    }
    static void ispis(char[,] a)
    {
        for (int i = 0; i < a.GetLength(0); i++)
        {
            for (int j = 0; j < a.GetLength(1); j++) Console.Write(a[i, j]);
            Console.WriteLine();
        }
    }
    static int res(char[,] a)
    {
        int br = 0;
        for (int i = 0; i < a.GetLength(0); i++)
        {
            for (int j = 0; j < a.GetLength(1); j++) if(a[i, j] == 'S') br++;
        }
        return br;
    }
    static bool uMatrici(int x, int y, int n, int m)
    {
        return x >= 0 && x < n && y >= 0 && y < m;
    }
    static void pretraga(char[,] a, int n, int m, int xS, int yS, int xX, int yX)
    {
        int[] dx = { -1, 1, 0, 0 };
        int[] dy = { 0, 0, -1, 1 };
        var red1 = new Queue<Tuple<int, int>>();
        var red2 = new Queue<Tuple<int, int>>();
        red1.Enqueue(Tuple.Create(xS, yS));
        if(xX >= 0 && yX >= 0) red2.Enqueue(Tuple.Create(xX, yX));
        a[xS, yS] = 'S';
        a[xX, yX] = 'X';
        while (red1.Count > 0 || red2.Count > 0)
        {
            if (red2.Count > 0)
            {
                int length = red2.Count;
                for (int i = 0; i < length; i++)
                {
                    var koordinate = red2.Dequeue();
                    int trenutnoX = koordinate.Item1;
                    int trenutnoY = koordinate.Item2;
                    for (int j = 0; j < 4; j++)
                    {
                        int novoX = trenutnoX + dx[j];
                        int novoY = trenutnoY + dy[j];
                        if (uMatrici(novoX, novoY, n, m) && a[novoX, novoY] == '.')
                        {
                            a[novoX, novoY] = 'X';
                            red2.Enqueue(Tuple.Create(novoX, novoY));
                        }
                    }
                }
            }
            if (red1.Count > 0)
            {
                int length = red1.Count;
                for (int i = 0; i < length; i++)
                {
                    var koordinate = red1.Dequeue();
                    int trenutnoX = koordinate.Item1;
                    int trenutnoY = koordinate.Item2;
                    for (int j = 0; j < 4; j++)
                    {
                        int novoX = trenutnoX + dx[j];
                        int novoY = trenutnoY + dy[j];
                        if (uMatrici(novoX, novoY, n, m) && a[novoX, novoY] == '.')
                        {
                            a[novoX, novoY] = 'S';
                            red1.Enqueue(Tuple.Create(novoX, novoY));
                        }
                    }
                }
            }
        }
    }
    static void Main()
    {
        int[] s = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int n = s[0];
        int m = s[1];
        char[,] a = new char[n, m];
        var koordinate = unos(a);
        int xS = int.Parse(koordinate[0].ToString());
        int yS = int.Parse(koordinate[1].ToString());
        int xX, yX;
        if (koordinate.Length == 5)
        {
            xX = int.Parse(koordinate[2].ToString());
            yX = int.Parse(koordinate[3].ToString());
            pretraga(a, n, m, xS, yS, xX, yX);
            Console.WriteLine(res(a));
        }
        else Console.WriteLine(n * m);
        Console.ReadLine();
    }
}