1247537832
#include <bits/stdc++.h>
using namespace std;
const int mn=1e6+7;
const int mm=1e3+7;
int tt=0;
struct ccf{
int x,y;
}a[mn];
//bool p1[mm][mm],p2[mm][mm],p3[mm][mm],p4[mm][mm];//1:+ +,2:- +,3:+ -,4:- -
map<long double,bool> m1,m2,m3,m4;
int main()
{
//freopen("1.in","r",stdin);
//freopen("sol.out","w",stdout);
int ans=0;
bool flag1=false,flag2=false,flag3=false,flag4=false;
int n,m,l,r;
cin>>n>>m;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
char c;
c=getchar();
while(c!='K'&&c!='.'&&c!='S')
c=getchar();
if(c=='K')
{
a[++tt].x=i;
a[tt].y=j;
}
if(c=='S')
{
l=i;
r=j;
}
}
for(int i=1;i<=tt;++i)
{
int y1=(r-a[i].y),x1=(l-a[i].x);
if(x1==0)
{
if(y1>0&&flag1==false)
{
flag1=true;
++ans;
}
if(y1<0&&flag3==false)
{
flag3=true;
++ans;
}
continue;
}
if(y1==0)
{
if(x1>0&&flag2==false)
{
flag2=true;
++ans;
}
if(x1<0&&flag4==false)
{
flag4=true;
++ans;
}
continue;
}
if(x1>0&&y1>0)
{
long double k=(long double)y1/(long double)x1;
if(m1.find(k)==m1.end())
{
m1[k]=true;
++ans;
}
continue;
}
if(x1>0&&y1<0)
{
x1=abs(x1);y1=abs(y1);
long double k=(long double)y1/(long double)x1;
if(m2.find(k)==m2.end())
{
m2[k]=true;
++ans;
}
continue;
}
if(x1<0&&y1>0)
{
x1=abs(x1);y1=abs(y1);
long double k=(long double)y1/(long double)x1;
if(m3.find(k)==m3.end())
{
m3[k]=true;
++ans;
}
continue;
}
if(x1<0&&y1<0)
{
x1=abs(x1);y1=abs(y1);
long double k=(long double)y1/(long double)x1;
if(m4.find(k)==m4.end())
{
m4[k]=true;
++ans;
}
continue;
}
}
cout<<ans;
}