//pragma
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)

#define mp make_pair

#define pb push_back
#define F first
#define S second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo,int hi){
    return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

const ll lrand(ll lo,ll hi){
    return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

#define getc getchar
template<typename T>
T getnum(){
    int sign = 1;
    T ret = 0;
    char c;

    do{
        c = getc();
    }while(c == ' ' || c == '\n');
    if(c == '-')sign = -1;
    else ret = c-'0';
    while(1){
        c = getc();
        if(c < '0' || c > '9')break;
        ret = 10*ret + c-'0';
    }
    return sign * ret;
}

inline void ini(int& x){
    x = getnum<int>();
}

inline void scani(int& x){
    scanf("%d",&x);
}

//end of macro

const int cx[] = {0, 1, 0, -1};
const int cy[] = {1, 0, -1, 0};

const int N = 805;

int n,m,k;
char grid[N][N];
queue<pii>q;
bool vis[N][N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #define endl '\n'
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(10);

    cin>>n>>m>>k;
    int sx, sy;
    REP(i,1,n){
        REP(j,1,m){
            cin>>grid[i][j];
            if(grid[i][j] == 'S'){
                sx = i;
                sy = j;
            }
        }
    }

    // bfs
    vis[sx][sy] = true;
    q.push(mp(sx, sy));
    int ans = -1;

    for(int step=0; !q.empty() && step<k; step++){
        for(int sz=q.size(); sz>0; sz--){
            int x = q.front().F;
            int y = q.front().S;
            q.pop();

            FOR(d,4){
                int tx = x + cx[d];
                int ty = y + cy[d];

                if(tx < 1 || tx > n || ty < 1 || ty > m)continue;
                if(grid[tx][ty] == '#')continue;
                if(vis[tx][ty])continue;

                vis[tx][ty] = true;
                q.push(mp(tx, ty));

                if(tx == 1 || tx == n || ty == 1 || ty == m){
                    ans = 0;
                }
            }
        }
    }
    while(!q.empty())q.pop();

    if(ans == -1){
        REP(i,1,n){
            REP(j,1,m){
                if(vis[i][j]){
                    q.push(mp(i,j));
                    // printf("start %d %d\n",i,j);
                }
            }
        }
        for(int step=0; !q.empty(); step++){
            int cast = step/k + 1;
            for(int sz=q.size(); sz>0; sz--){
                int x = q.front().F;
                int y = q.front().S;
                q.pop();

                FOR(d,4){
                    int tx = x + cx[d];
                    int ty = y + cy[d];

                    if(tx < 1 || tx > n || ty < 1 || ty > m)continue;
                    if(vis[tx][ty])continue;

                    vis[tx][ty] = true;
                    q.push(mp(tx, ty));

                    if(tx == 1 || tx == n || ty == 1 || ty == m){
                        // printf("found %d %d, step %d, cast %d\n",tx,ty,step,cast);
                        ans = cast;
                        goto finish;
                    }
                }
            }
        }
        finish:;
    }

    cout << ans+1 << endl;

    return 0;
}


Measured memory_bytes: 1664