// Slower C++ solution:
#include<iostream>

#include<vector>

#include<string>

#include<set>

#include<map>

#include<algorithm>

#include<queue>

#include<list>

#include<stack>

#include<cstdio>

#include<fstream>

#include<numeric>

#include<functional>

#include<utility>

#include<memory>

#include <cstring>

using namespace std;

const int M = 1e3;

char mmp[M][M] , s[M][M];

int h , w , k , step[M][M] , dr[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};

struct TnT {

    int x , y , step , flag;

};

bool visit[M][M];

void bfs(TnT sta) {

    memset(step , 0 , sizeof(step));

    step[sta.x][sta.y] = 1;

    memset(visit, false , sizeof(visit));

    visit[sta.x][sta.y] = true;

    queue<TnT>q;

    q.push(sta);

    while(!q.empty()) {

        TnT gg = q.front();

        q.pop();

        for(int i = 0 ; i < 4 ; i++) {

            TnT gl = gg;

            gl.x += dr[i][0];

            gl.y += dr[i][1];

            if(gl.x >= 0 && gl.x < h && gl.y >= 0 && gl.y < w && mmp[gl.x][gl.y] != '#' && !visit[gl.x][gl.y]) {

                gl.step++;

                step[gl.x][gl.y] = gl.step;

                visit[gl.x][gl.y] = true;

                q.push(gl);

            }

        }

    }

}

int main() {

    cin>>h>>w>>k;

    for(int i = 0 ; i < h ; i++) {

        cin>>mmp[i];

    }

    TnT sta;

    for(int i = 0 ; i < h ; i++) {

        for(int j = 0 ; j < w ; j++) {

            if(mmp[i][j] == 'S') {

                sta.x = i , sta.y = j;

                break;

            }

        }

    }

    sta.step=0 , sta.flag=k;

    bfs(sta);

    int ans=1000000000;

    for(int i=0;i<h;i++) {

        for(int j=0;j<w;j++) {

            if(step[i][j] != 0) {

                if(i == 0 || i == h - 1 || j == 0 || j == w - 1) {

                    int gg = step[i][j];

                    if(gg % k == 0) {

                        ans = min(ans , gg / k);

                    }

                    else {

                        ans = min(ans , gg / k + 1);

                    }

                }

                else {

                    int gg = step[i][j] , gl = 0;

                    if(gg % k == 0) {

                        gl = gg / k;

                    }

                    else {

                        gl = gg / k + 1;

                    }

                    gg = i;

                    if(gg % k == 0) {

                        ans = min(ans , gl + gg / k);

                    }

                    else {

                        ans = min(ans , gl + gg / k + 1);

                    }

                    gg = j;

                    if(gg % k == 0) {

                        ans = min(ans , gl + gg / k);

                    }

                    else {

                        ans = min(ans , gl + gg / k + 1);

                    }

                    gg = h - 1 - i;

                    if(gg % k == 0) {

                        ans = min(ans , gl + gg / k);

                    }

                    else {

                        ans = min(ans , gl + gg / k + 1);

                    }

                    gg = w - 1 - j;

                    if(gg % k == 0) {

                        ans = min(ans , gl + gg / k);

                    }

                    else {

                        ans = min(ans , gl + gg / k + 1);

                    }

                }

            }

        }

    }

    cout<<ans;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.100293975317822x):
#include <cstdio>  

#include <iostream>  

#include <cstring>  

#include <string>  

#include <cmath>  

#include <algorithm>  

#include <cstdlib>  

#include <utility>  

#include <map>  

#include <stack>  

#include <set>  

#include <vector>  

#include <queue>  

#include <deque>  

#include <sstream>  

#include <bitset>   

#define x first  

#define y second  

#define mp make_pair  

#define pb push_back  

#define LL long long  

#define ld long double  

#define Pair pair<int,int>  

#define LOWBIT(x) x & (-x)  

using namespace std;  

  

const int INF=0x7ffffff;  

  

int ans=INF;  

int movee[5][3];  

bool visited[848][848];  

int r,c,k;  

int sx,sy;  

int a[848][848];  

char s[1000];  

queue<int> q;  

  

void bfs(int x,int y,int step)  

{  

    q.push(x);q.push(y);q.push(0);  

    int cur_ans,cx,cy,cd,xx,yy,dd;  

    visited[x][y]=true;  

    while (!q.empty())  

    {  

        cx=q.front();q.pop();  

        cy=q.front();q.pop();  

        cd=q.front();q.pop();  

        cur_ans=cx-1;  

        cur_ans=min(cur_ans,cy-1);  

        cur_ans=min(cur_ans,r-cx);  

        cur_ans=min(cur_ans,c-cy);  

        ans=min(ans,cur_ans);  

        if (cd==k) continue;  

        for (int i=1;i<=4;i++)  

        {  

            xx=cx+movee[i][1];yy=cy+movee[i][2];  

            if (xx<1 || xx>r || yy<1 || yy>c) continue;  

            if (a[xx][yy]==1 || visited[xx][yy]) continue;  

            visited[xx][yy]=true;q.push(xx);q.push(yy);q.push(cd+1);  

        }  

    }  

}  

  

int main ()  

{  

    int i,j;  

    movee[1][1]=1;movee[1][2]=0;  

    movee[2][1]=0;movee[2][2]=1;  

    movee[3][1]=-1;movee[3][2]=0;  

    movee[4][1]=0;movee[4][2]=-1;  

    cin>>r>>c>>k; 

    for (i=1;i<=r;i++)  

    {  

        cin>>s; 

        for (j=1;j<=c;j++)  

        {  

            if (s[j-1]=='#') a[i][j]=1;  

            if (s[j-1]=='.') a[i][j]=2;  

            if (s[j-1]=='S')  

            {  

                sx=i;sy=j;  

                a[i][j]=2;  

            }  

        }  

    }  

    bfs(sx,sy,0);  

    int fans=1+(ans+k-1)/k;  

    cout<<fans;  

    return 0;  

}