//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();
			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;
}