// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.9912780832279333x):
#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;

}