// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;

const int MAXN=888;

const int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

int n,m,k;

char g[MAXN][MAXN];

int dist[MAXN][MAXN];

struct State

{

    int x,y;

    State(int x=0,int y=0):x(x),y(y){}

};

bool inRange(int x,int y)

{

    return (x>=0&&y>=0&&x<n&&y<m);

}

void bfs(int bx,int by)

{

    queue<State> q;

    while(!q.empty()) q.pop();

    q.push(State(bx,by));

    dist[bx][by]=0;

    while(!q.empty())

    {

        State u=q.front();

        q.pop();

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

        {

            int tx=u.x+dir[i][0],ty=u.y+dir[i][1];

            if (inRange(tx,ty)&&dist[tx][ty]==-1&&g[tx][ty]=='.')

            {

                dist[tx][ty]=dist[u.x][u.y]+1;

                q.push(State(tx,ty));

            }

        }

    }

}

int main()

{

    cin >> n >> m >> k;

    int bx=0,by=0,ans=INT_MAX;

    for (int i=0;i<n;i++)

    {

        for (int j=0;j<m;j++)

        {

            cin >> g[i][j];

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

            {

                bx=i;

                by=j;

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

            }

        }

    }

    memset(dist,-1,sizeof(dist));

    bfs(bx,by);

    for (int i=0;i<n;i++)

        for (int j=0;j<m;j++)

    {

        if (dist[i][j]<=k&&dist[i][j]!=-1)

        {

            ans=min(ans,1+(min(min(i,n-i-1),min(j,m-j-1))+k-1)/k);

        }

    }

    cout << ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <algorithm>

#include <cstring>

#include <cstdio>

#include <queue>

#include <cmath>

 

#define REDIRECT(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)

#define CLOSEALL() fclose(stdin), fclose(stdout)

 

using namespace std;

 

const int MAXN = 800 + 5;

const int DIRX[] =

{ 1, -1, 0, 0 };

const int DIRY[] =

{ 0, 0, 1, -1 };

 

int H, W, K;

int stX, stY;

char mz[MAXN][MAXN];

bool vist[MAXN][MAXN];

 

int answer = 0x7F7F7F7F;

 

void BFS()

{

	queue<pair<pair<int, int>, int> > Q;

	Q.push(make_pair(make_pair(stX, stY), 0));

 

	while (!Q.empty())

	{

		pair<pair<int, int>, int> cur = Q.front();

		Q.pop();

 

		if(vist[cur.first.first][cur.first.second])

			continue;

		vist[cur.first.first][cur.first.second] = true;

 

		int curAns = min(min(cur.first.first - 1, H - cur.first.first),

				min(cur.first.second - 1, W - cur.first.second));

		curAns = (curAns + K - 1) / K;

		answer = min(answer, curAns + 1);

 

		if (cur.second >= K)

			continue;

 

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

		{

			pair<int, int> newPos = make_pair(cur.first.first + DIRX[i],

					cur.first.second + DIRY[i]);

			if(newPos.first <= 0 || newPos.first > H)

				continue;

			if(newPos.second <= 0 || newPos.second > W)

				continue;

			if(mz[newPos.first][newPos.second] != '#' && !vist[newPos.first][newPos.second])

				Q.push(make_pair(newPos, cur.second + 1));

		}

	}

}

 

int main()

{

 

	//REDIRECT("room");

 

	scanf("%d%d%d", &H, &W, &K);

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

	{

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

		for (int j = 1; j <= W; j++)

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

				stX = i, stY = j;

	}

 

	BFS();

 

	printf("%d\n", answer);

 

	CLOSEALL();

	return 0;

}

// Fastest known solution for this problem: