#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
using ll = long long;
using lint = long long;
typedef pair<int,int> P;
const lint inf=1e9+7;
const int MOD=1000000007;
struct Point{
    int x;
    int y;
    vector<Point> next;
    void get_next(vector<vector<int>> const& map){
        int py=y+1;int px=x;
        if(map[py][px]!=inf){
            Point buf;
            buf.x=px;buf.y=py;
            next.push_back(buf);
        }
        py=y-1;px=x;
        if(map[py][px]!=inf){
            Point buf;
            buf.x=px;buf.y=py;
            next.push_back(buf);
        }
        py=y;px=x+1;
        if(map[py][px]!=inf){
            Point buf;
            buf.x=px;buf.y=py;
            next.push_back(buf);
        }
        py=y;px=x-1;
        if(map[py][px]!=inf){
            Point buf;
            buf.x=px;buf.y=py;
            next.push_back(buf);
        }
    }
};
struct Map{
int h;
int w;
P S;
char road='.';//道を表す文字
char wall='#';//壁を表す文字
vector<vector<int>> map;
void mkmap(int in_h,int in_w,vector<string> const& input){
    h=in_h;w=in_w;
    for(int i=0;i<=h+1;i++){
        map.push_back(vector<int>(w+2,inf));
    }
    //道ならinf-1にする処理
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(input[i][j]!=wall){
                map[i+1][j+1]--;
            }
            if(input[i][j]=='S'){
                S.first=i+1;S.second=j+1;
            }
        }
    }
}
void output(){
    for(int i=0;i<=h+1;i++){
        for(int j=0;j<=w+1;j++){
            if(this->map[i][j]==inf)cout<<wall;
            else cout<<map[i][j];
        }
        cout<<endl;
    }
}
void BFS(){
Point s;s.x=S.second;s.y=S.first;
queue<Point> que;
vector<vector<int>> memo(h+2,vector<int> (w+2,inf));
que.push(s);
this->map[s.y][s.x]=0;
memo[s.y][s.x]=0;
while(!que.empty()){
    Point buf;
    buf=que.front();
    que.pop();
    buf.get_next(map);
    int num=buf.next.size();
    for(int i=0;i<num;i++){
        if(memo[buf.next[i].y][buf.next[i].x]>memo[buf.y][buf.x]+1){
            que.push(buf.next[i]);
            memo[buf.next[i].y][buf.next[i].x]=memo[buf.y][buf.x]+1;
        }
    }
}
for(int i=0;i<=h+1;i++){
  for(int j=0;j<=w+1;j++){
    map[i][j]=memo[i][j];
  }
}
}
int map_max(){
    int max=0;
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            if(map[i][j]!=inf){
                if(map[i][j]>max)max=map[i][j];
            }
        }
    }
    return max;
}
int dist(int y,int x){
    return map[y][x];
}
void solve(int k){
    BFS();
    int res=inf;
    //output();
    rep(i,h){
        rep(j,w){
            int buf=min(i,h-i-1);
            buf=min(buf,j);
            buf=min(buf,w-j-1);
            if(map[i+1][j+1]<=k){
                if(buf==0){
                    res=min(res,1);
                    break;
                }
                res=min(res,(buf-1)/k+1+1);
            }
        }
    }
    cout<<res<<"\n";
}
};

signed main(){
  int h,w,k;cin>>h>>w>>k;
  Map lab;
  vector<string> input(h);
  rep(i,h)cin>>input[i];
  lab.mkmap(h,w,input);
  lab.solve(k);
  return 0;
}

Measured memory_bytes: 9504