#include <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;

long long int INF = 3e18;
long long int mod = 4294967296;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "DRUL";
//ll bit[500005];
//↓,→,↑,←

#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(15)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,n) for(i=0;i<n;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl

ll rui(ll abc,ll bed){
    //abcのbed乗を計算する
    if(bed==0){return 1;}
    else{
        ll ced = rui(abc,bed/2);
        ced *= ced;
        ced %= mod;
        if(bed%2==1){ced*=abc; ced%=mod;}
        return ced;
    }
}
ll gcd(ll number1,ll number2){
    if(number1 > number2){
        swap(number1,number2);
    }
    if(number1 == 0 || number1 == number2){
        return number2;
    }else{
        return gcd(number2 % number1,number1);
    }
}
void YES(bool condition){
    if(condition){
        p("YES");
    }else{
        p("NO");
    }
    return;
}
void Yes(bool condition){
    if(condition){
        p("Yes");
    }else{
        p("No");
    }
    return;
}

ll n,m,num,sum,ans,a,b,c,d,e,f,g,h,i,j,k,w;
ll x[500005],y[500005];
ll dp[805][805];
bool flag,dame;
bool ban[505][505];
char s[805][805];

int main(){
    cin >> h >> w >> k;
    for(i=1;i<=h;i++){
        cin >> s[i];
        for(j=w;j>=1;j--){
            s[i][j] = s[i][j-1];
        }
    }
    ans = INF;
    ll sx = -1,sy = -1;
    for(i=1;i<=h;i++){
        for(j=1;j<=w;j++){
            dp[i][j] = -1;
            if(s[i][j] == 'S'){
                sx = i;
                sy = j;
            }
        }
    }
    //dp[sx][sy] = 0;
    pqps.push(P(0ll,sx*1000 + sy));
    while(!pqps.empty()){
        P p = pqps.top();
        pqps.pop();
        ll x,y,cost;
        cost = p.first;
        num = p.second;
        x = num / 1000;
        y = num % 1000;
       // pe(x);p(y);
        if(dp[x][y] != -1)continue;
        dp[x][y] = cost;
        ll ansx,ansy;
        ansx = min(x-1,h-x);
        ansy = min(y-1,w-y);
        ans = min(ans,min(ansx,ansy));
        if(x == 1 || x == h || y == 1 || y == w){
           // p("totyu");
            p(1);
            return 0;
        }else if(cost == k){
            continue;
        }else{
            ll nx,ny;
            for(int i = 0;i < 4;i ++){
                nx = x + dx[i];
                ny = y + dy[i];
                if(s[nx][ny] != '#'  && dp[nx][ny] == -1 && 1 <= nx && nx <= h && 1 <= ny && ny <= w){
                    pqps.push(P(cost + 1,nx * 1000 + ny));
                }
            }
        }
    }
   // p("End");
    for(i=1;i<=h;i++){
        for(j=1;j<=w;j++){
            //pe(dp[i][j]);
        }
       // el;
    }
    p(((ans + k - 1)/k) + 1);
    return 0;
}


Measured memory_bytes: 31104