#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#define MN 800
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
char st[MN+5][MN+5];
int n,m,K,d[MN+5][MN+5],top=0,vis[MN+5][MN+5];
pair<int,int> q[MN*MN+5];
const int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
    n=read();m=read();K=read();
    for(int i=1;i<=n;++i) scanf("%s",st[i]+1);
    for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(st[i][j]=='S') q[++top]=make_pair(i,j),vis[i][j]=1;
    for(int i=1;i<=top;++i)
    {
        int x=q[i].first,y=q[i].second;
        for(int k=0;k<4;++k)
        {
            int nx=x+dis[k][0],ny=y+dis[k][1];
            if(nx<1||ny<1||nx>n||ny>m||vis[nx][ny]||st[nx][ny]=='#') continue;
            vis[nx][ny]=1;q[++top]=make_pair(nx,ny);d[nx][ny]=d[x][y]+1;
        }
    }
    int ans=1e9;
    for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(vis[i][j]) ans=min(ans,max(1,(d[i][j]+K-1)/K)+(min(min(i-1,n-i),min(j-1,m-j))+K-1)/K);
    cout<<ans;
    return 0;
}
