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

#include<string>

#include<cstdio>

#include<cstring>

#include<queue>

using namespace std;

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

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

char ma[888][888];

int n,m,k,dist[888][888],pis[888][888],mn;

queue<pair<int,int> > q;

bool In(int x,int y)

{

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

}

int main()

{

	mn=1e9;

	scanf("%d%d%d",&n,&m,&k);

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

	scanf("%s",ma[i]);

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

	{

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

		{

			pis[i][j]=min(min(i,j),min(n-i-1,m-j-1));

			pis[i][j]=(pis[i][j]+k-1)/k;

		}

	}

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

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

	{

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

		{

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

			{

				q.push(make_pair(i,j));

				dist[i][j]=0;

				mn=min(mn,pis[i][j]+1);

			}

		}

	}

	while(!q.empty())

	{

		int x=q.front().first,y=q.front().second;

		q.pop();

		if (dist[x][y]==k) break;

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

		{

			int xx=x+dx[i],yy=y+dy[i];

			if (In(xx,yy) && ma[xx][yy]=='.' && dist[xx][yy]==-1)

			{

				mn=min(mn,pis[xx][yy]+1);

				dist[xx][yy]=dist[x][y]+1;

				q.push(make_pair(xx,yy));

			}

		}

	}

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

	return 0;

}

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

}