// 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: 1.1554658909452864x):
//Littleboy123 Template 1.1



#include <bits/stdc++.h>

using namespace std;



//pragmas

/*

#pragma comment(linker, "/stack:200000000")

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma GCC target ("avx2")

#pragma GCC optimization ("Ofast")

#pragma GCC optimization ("unroll-loops")

*/



typedef pair<int, int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

typedef long long int ll;

typedef unsigned long long int ull;

typedef long double ld;



#define FOR(a,b,c) for(ll (a)=(b);(a)<(c);++(a))

#define FORE(a,b,c) for(ll (a)=(b);(a)<=(c);++(a))

#define FORN(a,b,c) for(ll (a)=(b);(a)>(c);--(a))

#define FORNE(a,b,c) for(ll (a)=(b);(a)>=(c);--(a))

#define COUT(a) cout << (a) << '\n'

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

#define MAX(a,b) (a) = max((a), (b))

#define MIN(a,b) (a) = min((a), (b))

#define nl '\n'

#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define pi acos(-1.0)



void init(){

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

}



//End of template



int n, m, k;

char arr[805][805];

int lompati[805][805];

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

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



int main(){

	init();



	//debug

	/*

	ifstream in("input.in");

	streambuf *cinbuf = std::cin.rdbuf();

	cin.rdbuf(in.rdbuf());



	ofstream out("out.txt");

	streambuf *coutbuf = std::cout.rdbuf();

	cout.rdbuf(out.rdbuf());

	*/

	//end debug



	cin >> n >> m >> k;

	int x, y;

	FOR(i, 0, n){

		FOR(j, 0, m){

			cin >> arr[i][j];

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

				x = i;

				y = j;

			}

		}

	}

	FOR(i, 0, 805){

		FOR(j, 0, 805)

			lompati[i][j] = 0;

	}

	int count = 0;

	queue<pair<ii, int>> q;

	q.push(mp(mp(x, y), k));

	arr[x][y] = '&';

	queue<pair<ii, int>> lock;

	queue<pair<ii, int>> akhir;

	int rem;

	int nemu = 0;

	int find = 0;

	while(true){

		count++;

		while(q.empty() == false){

			x = q.front().fi.fi;

			y = q.front().fi.se;

			rem = q.front().se;

			q.pop();

			if(x == 0 || x == n-1 || y == 0 || y == m-1){

				nemu = 1;

				break;

			}

			if(rem == 0){

				lock.push(mp(mp(x, y), k));

				akhir.push(mp(mp(x, y), k));

				continue;

			}

			find = 0;

			FOR(i, 0, 4){

				if(x + dr[i] < 0 || x + dr[i] > n || y + dc[i] < 0 || y + dc[i] > m)

					continue;

				if(arr[x+dr[i]][y+dc[i]] == '.'){

					find++;

					arr[x+dr[i]][y+dc[i]] = '&';

					q.push(mp(mp(x+dr[i], y+dc[i]), rem-1));

				}

			}

			if(find == 0){

				lock.push(mp(mp(x, y), k));

				akhir.push(mp(mp(x, y), k));

				continue;

			}

		}

		if(nemu == 1)

			break;

		while(lock.empty() == false){

			x = lock.front().fi.fi;

			y = lock.front().fi.se;

			rem = lock.front().se;

			lock.pop();

			//cout << x << ' ' << y << endl;

			if(rem == 0)

				continue;

			FOR(i, 0, 4){

				if(x + dr[i] < 0 || x + dr[i] > n || y + dc[i] < 0 || y + dc[i] > m)

					continue;

				if(arr[x+dr[i]][y+dc[i]] == '#'){

					arr[x+dr[i]][y+dc[i]] = '.';

					lock.push(mp(mp(x+dr[i], y+dc[i]), rem-1));

				}

				else if(arr[x+dr[i]][y+dc[i]] == '.' && lompati[x+dr[i]][y+dc[i]] == 0){

					lompati[x+dr[i]][y+dc[i]] = 1;

					lock.push(mp(mp(x+dr[i], y+dc[i]), rem));

				}

			}

		}

		while(akhir.empty() == false){

			q.push(akhir.front());

			akhir.pop();

		}

	}

	cout << count << nl;

	return 0;

}