import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static  int N;
    static int M;
    static int Score = 1;
    static Scanner In = new Scanner(System.in);
    public static void main(String[] args) {;
        char[][] Matrix;
        char[][] sus;
        char[] temp;

        System.out.println("Uneti Vrednosti N i M:");
        N = In.nextInt();
        M = In.nextInt();
        if((N>1000) || (M>1000)){System.out.println("nedozvoljeno"); System.exit(-1);}
        Matrix = new char[1001][1001];
        sus = new char[1001][1001];
        for (char[] matrix : Matrix) {
            Arrays.fill(matrix, '*');
            System.arraycopy(Matrix,0, sus, 0, sus.length);
            //Arrays.fill(sus, '*');
        }
        for (int i = 1; i <= N; i++) {
            temp = In.next().toCharArray();
            System.arraycopy(temp,0, Matrix[i], 1, temp.length);
        }
        while(is_Playing(Matrix)){
            for (int i = 1; i <= N; i++) {
                for (int j = 1; j <= M; j++) {
                    if (Matrix[i][j] == 'X'){
                        if (Matrix[i-1][j] == '.'){
                            sus[i-1][j] = 'X';
                        }
                        if (Matrix[i+1][j] == '.'){
                            sus[i+1][j] = 'X';
                        }
                        if (Matrix[i][j-1] == '.'){
                            sus[i][j-1] = 'X';
                        }
                        if (Matrix[i][j+1] == '.'){
                            sus[i][j+1] = 'X';
                        }
                    }
                }
            }
            for (int i = 1; i <= N; i++) {
                for (int j = 1; j <= M; j++) {
                    if (Matrix[i][j] == 'S'){
                        if (Matrix[i-1][j] == '.'){
                            sus[i-1][j] = 'S';
                            Score++;
                        }
                        if (Matrix[i+1][j] == '.'){
                            sus[i+1][j] = 'S';
                            Score++;
                        }
                        if (Matrix[i][j-1] == '.'){
                            sus[i][j-1] = 'S';
                            Score++;
                        }
                        if (Matrix[i][j+1] == '.'){
                            sus[i][j+1] = 'S';
                            Score++;
                        }
                    }
                    else {
                        sus[i][j] = Matrix[i][j];
                    }
                }
            }
            Matrix = sus;
        }
        System.out.printf("\n %d", Score);
        In.close();
        System.exit(0);
    }
    private static boolean is_Playing(char[][] m){
        boolean ret = false;
        for (int i=1; i <= N; i++) {
            for (int j=1; j<=M; j++) {
                //System.out.printf("%c ", m[i][j]);
                if (m[i][j] == '.') {
                    ret = true;
                    break;
                }
            }
            System.out.print("\n");
        }
        return ret;
    }
}