// 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.142462361705442x):
//**************************

//*** writer  :  Alan Hu **

//************************

#include<bits/stdc++.h>



using namespace std;

     

#define CLEAN(a,b) memset(a,b,sizeof(a))

#define mkp make_pair

#define pb push_back

#define rept(i,a,b) for(int i=(int)(a);i<(int)(b);i++)

     

typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;



int Q[1010000][2],head,tail,D[810][810];

int n,m,K,sx,sy;

char p[810][810];



void Ins(int x,int y,int d)

{

	if (D[x][y]<=d || p[x][y]!='.') return;

	D[x][y]=d;

	tail++;

	Q[tail][0]=x;

	Q[tail][1]=y;	

}



int main()

{

	int x,y;

	cin>>n>>m>>K;

	rept(i,1,n+1)

	{

		scanf("%s",p[i]+1);

		rept(j,1,m+1)

		{

			if (p[i][j]=='S')

			{

				sx=i;

				sy=j;

				p[i][j]='.';

			}

			D[i][j]=1324144;

		}

	}

	Ins(sx,sy,0);

	while (head<tail)

	{

		head++;

		x=Q[head][0];

		y=Q[head][1];

		Ins(x+1,y,D[x][y]+1);

		Ins(x,y+1,D[x][y]+1);

		Ins(x-1,y,D[x][y]+1);

		Ins(x,y-1,D[x][y]+1);

	}

	int res=1e9;

	rept(i,1,n+1)

	{

		rept(j,1,m+1)

		{

			if (D[i][j]<=K)

			{

				res=min(res,1+(min(min(i-1,n-i),min(j-1,m-j))+K-1)/K);

			}

		}

	}

	cout<<res<<endl;

	return 0;

}