Program steva;
var N,M,v,k,p,t:integer;
a,b:array[1..1000,1..1000] of char;
begin
read(N);read(M); t:=0;p:=1; 
for v:=0 to N do begin
for k:= 0 to M do begin
read (a[v,k]);b[v,k]:=a[v,k]; if a[v,k]='.' then t:=t+1;   write (a[v,k]); 
end;
end;
while t>0 do begin
for v:=1 to N do
for k:=1 to M do begin
if a[v,k]='X' then begin
if b[v-1,k]='.' then begin
b[v-1,k]:='X';t:=t-1;
end;
if b[v+1,k]='.' then begin
b[v+1,k]:='X';t:=t-1;
end;
if b[v,k-1]='.' then begin
b[v,k-1]:='X';t:=t-1;
end;
if b[v,k+1]='.' then begin
b[v,k+1]:='X';t:=t-1;
end;
end;
end;
for v:=1 to N do
for k:=1 to M do begin
if a[v,k]='S' then begin
if b[v-1,k]='.' then begin
b[v-1,k]:='S';t:=t-1;p:=p+1;
end;
if b[v+1,k]='.' then begin
b[v+1,k]:='S';t:=t-1;p:=p+1;
end;
if b[v,k-1]='.' then begin
b[v,k-1]:='S';t:=t-1;p:=p+1;
end;
if b[v,k+1]='.' then begin
b[v,k+1]:='S';t:=t-1;p:=p+1;
end;
end;
end;
for v:=1 to N do
for k:=1 to M do 
a[v,k]:=b[v,k];
end;
write(p);
end.Program steva;
var N,M,v,k,p,t:integer;
a,b:array[1..1000,1..1000] of char;
begin
read(N);read(M); t:=0;p:=1; 
for v:=0 to N do begin
for k:= 0 to M do begin
read (a[v,k]);b[v,k]:=a[v,k]; if a[v,k]='.' then t:=t+1;   write (a[v,k]); 
end;
end;
while t>0 do begin
for v:=1 to N do
for k:=1 to M do begin
if a[v,k]='X' then begin
if b[v-1,k]='.' then begin
b[v-1,k]:='X';t:=t-1;
end;
if b[v+1,k]='.' then begin
b[v+1,k]:='X';t:=t-1;
end;
if b[v,k-1]='.' then begin
b[v,k-1]:='X';t:=t-1;
end;
if b[v,k+1]='.' then begin
b[v,k+1]:='X';t:=t-1;
end;
end;
end;
for v:=1 to N do
for k:=1 to M do begin
if a[v,k]='S' then begin
if b[v-1,k]='.' then begin
b[v-1,k]:='S';t:=t-1;p:=p+1;
end;
if b[v+1,k]='.' then begin
b[v+1,k]:='S';t:=t-1;p:=p+1;
end;
if b[v,k-1]='.' then begin
b[v,k-1]:='S';t:=t-1;p:=p+1;
end;
if b[v,k+1]='.' then begin
b[v,k+1]:='S';t:=t-1;p:=p+1;
end;
end;
end;
for v:=1 to N do
for k:=1 to M do 
a[v,k]:=b[v,k];
end;
write(p);
end.