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

#include <fstream>

#include <cstdio>

#include <algorithm>

#include <string>

#include <vector>

#include <queue>

#include <deque>

#include <utility>

#include <map>

#include <set>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <functional>

#include <sstream>

#include <cstring>

#include <bitset>

#include <stack>

using namespace std;

#define MP make_pair

#define PB push_back

const int INF=1e9;



int n,m,k,sx,sy,ans;

char c[805][805];

int dist[805][805];

int mx[]={0,0,1,0,-1};

int my[]={0,1,0,-1,0};



inline void bfs(int X,int Y)

{

	queue <int> q;

	q.push(X);q.push(Y);

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

	dist[X][Y]=0;

	while (!q.empty())

	{

		int x=q.front();q.pop();

		int y=q.front();q.pop();

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

		{

			int tx=x+mx[i];

			int ty=y+my[i];

			if (tx<1 || tx>n || ty<1 || ty>m || dist[tx][ty]!=-1 || c[tx][ty]!='.') continue;

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

			q.push(tx);

			q.push(ty);

		}

	} 

}



int main()

{

	cin>>n>>m>>k;

	for (int i=1;i<=n;i++) scanf("%s",c[i]+1);

	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (c[i][j]=='S') {sx=i;sy=j;break;}

	bfs(sx,sy);

	ans=INF;

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

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

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

			{

		//		cout<<i<<" "<<j<<endl;

				int res=min(i-1,n-i);

				res=min(res,min(j-1,m-j));

				ans=min(ans,res);

			}

	cout<<1+(ans+k-1)/k;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.4398675147030673x):
#include<bits/stdc++.h>



using namespace std;

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

#define REP(i,a,b) REPP(i,a,b,1)

#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)

#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) REP(i,0,(int)a-1)

#define FORD(i,a) REV(i,(int)a-1,0)

#define PB push_back

#define POB pop_back

#define FI first

#define SE second

#define MP make_pair

typedef long long ll;

typedef unsigned long long ull;

typedef pair < int , int > pii;

typedef vector < int > vi;

typedef vector < pii > vii;

typedef vector < ll > vl;

typedef pair < ll , ll > pll;



const double PI = acos(-1);

const ll MOD = 1e9 + 7;

const double EPS = 1e-9;

const int INF = 1e9;





ll addMod(ll a, ll b){

	if(a + b >= MOD) return a + b - MOD;

	return a + b;

}



ll mulMod(ll a, ll b){

	return (a*b)%MOD;

}



ll fast(ll a, ll b){

	if(b == 0) return 1;

	ll tmp = fast(a,b/2);

	tmp = mulMod(tmp,tmp);

	if(b & 1) tmp = mulMod(tmp,a);

	return tmp;

}



ll GCD(ll a, ll b){

	if(a > b) return GCD(b,a);

	if(a == 0) return b;

	return GCD(b%a,a);

}



//////////////////////////////////////////////



const int N = 800;



int dy[] = {-1,1,0,0};

int dx[] = {0,0,-1,1};

pii st;

int r,c,k;

string M[N + 5];

bool vis[N + 5][N + 5];



bool inRange(int y, int x){

	return y >= 0 && y < r && x >= 0 && x < c;

}



int solve(){

	queue < pair < pii , int > > q;

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

	q.push(MP(st,k));

	int y,x,yy,xx,ret = INF,mn,val,move;

	while(!q.empty()){

		y = q.front().FI.FI;

		x = q.front().FI.SE;

		move = q.front().SE;

		q.pop();

		if(vis[y][x]) continue;

		if(y == 0 || y == r - 1 || x == 0 || x == c-1) return 1;

		vis[y][x] = true;

		mn = min(min(x,c-x-1), min(y, r-y-1));

		val = 0;

		if(mn % k > 0) val = 1;

		val += mn / k;

		ret = min(ret, val+1);

		if(move == 0) continue;

		FOR(i,4){

			yy = y + dy[i];

			xx = x + dx[i];

			if(inRange(yy,xx) && !vis[yy][xx] && M[yy][xx] == '.'){

				q.push(MP(MP(yy,xx),move-1));

			}

		}

	}

	return ret;

}



int main(){

	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> r >> c >> k;

	FOR(i,r){

		cin >> M[i];

		FOR(j,c){

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

				st = MP(i,j);

			}

		}

	}

	cout << solve() << endl;

	return 0;

}