#include<bits/stdc++.h>
using namespace std;
#define inf 1e9
int n,m,k;
int cx[5] = {0,0,1,-1};
int cy[5] = {1,-1,0,0};
int len[805][805];
char s[805][805];
queue<pair<int,int> > q;
bool check(int x,int y)
{
    if(x<0 || y<0 || x>=n || y>=m || s[x][y]=='#') return 0;
    return 1;
}
int near(int x,int y)
{
    return min(min(x,n-1-x),min(y,m-1-y));
}
int main()
{
    int i,j,x,y,sx,sy;
    int ans;
    scanf("%d%d%d",&n,&m,&k);
    for(i=0;i<n;i++) scanf(" %s",s[i]);
    for(i=0;i<n;i++) for(j=0;j<m;j++) if(s[i][j]=='S') {sx = i; sy = j;}

    for(i=0;i<n;i++) for(j=0;j<m;j++) len[i][j] = inf;
    ans = inf;
    len[sx][sy] = 0;
    q.push({sx,sy});
    while(!q.empty())
    {
        x = q.front().first; y = q.front().second; q.pop();
        ans = min(ans,(int)ceil((double)near(x,y)*1.0/k));
        if(len[x][y]==k) continue;
        for(i=0;i<4;i++)
        {
            if(check(x+cx[i],y+cy[i]) && len[x+cx[i]][y+cy[i]] > len[x][y] + 1)
            {
                len[x+cx[i]][y+cy[i]] = len[x][y] + 1;
                q.push({x+cx[i],y+cy[i]});
            }
        }
    }
    printf("%d",ans+1);
}
