# include <bits/stdc++.h>

using namespace std;
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
using pt = pair<int,int>; 
int n, m, k;
char a[808][808];
int d[808][808];
int u[808][808];
vector < pt > mem[1606];

inline void bfs01 (pt st) {
	deque < pt > q;
	q.pb(st);

	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			d[i][j] = -1;

	d[st.F][st.S] = 0;
	while(q.size()) {
		pt v = q.front();
		q.pop_front();

		for (int dir = 0; dir < 4; ++dir) {
			pt nxt = v;
			nxt.F += dx[dir];
			nxt.S += dy[dir];

			if (d[nxt.F][nxt.S] != -1) {
				continue;
			}

			if (a[nxt.F][nxt.S] == '#') {
				d[nxt.F][nxt.S] = d[v.F][v.S] + 1;
				q.pb(nxt);
			} else {
				d[nxt.F][nxt.S] = d[v.F][v.S];
				q.push_front(nxt);
			
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) if (a[i][j] == '#') {
			mem[d[i][j]].pb(mp(i, j));
		}
	}
}

int main () {
	SpeedForce;
	cin >> n >> m >> k;
	pt st;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j];

			if (a[i][j] == 'S') {
				st = mp(i, j);	
				u[i][j] = 1;
			}
		}
	}
//	cout << a[2][2] << '\n';
//	cout << st.F << ' ' << st.S << '\n';
//	exit(0);
	bfs01(st);

	int ans = 0;
	vector < pt > vv;
	vv.pb(st);

	while(1) {	
		++ ans;

		//step 1
		queue < pair<pt, int> > q;
		for (auto it : vv) {
			q.push(mp(it, 0));
	//		cout << it.F << ' ' << it.S << '\n';
		}
	//	exit(0);

		vv.clear();

		while(q.size()) {
			auto v = q.front().F;
			int dist = q.front().S;
			q.pop();
			if (v.F == 1 || v.F == n || v.S == 1 || v.S == m) {
                cout << ans << '\n';
                exit(0);
            }
		//	cout << "step : pos : " << ans << ' '<< v.F << ' ' << v.S << ' ' << dist << '\n';

			if (dist == k) {
				vv.pb(v);
				continue;
			}

			for (int dir = 0; dir < 4; ++dir) {
				pt nxt = v;
				nxt.F += dx[dir];
				nxt.S += dy[dir];

				if (nxt.F < 1 || nxt.S < 1 || nxt.F > n || nxt.S > m) continue;
				if (u[nxt.F][nxt.S]) {
					continue;
				}
				if (a[nxt.F][nxt.S] == '#') {
					continue;
				}

				u[nxt.F][nxt.S] = 1;
				q.push(mp(nxt, dist + 1));
			}
		}
		//exit(0);
		// step 2
		for (int i = (ans - 1) * k + 1; i <= ans * k; ++i) if (i <= n + m) {
			for (auto it : mem[i]) {
				a[it.F][it.S] = '.';
				for (int dir = 0; dir < 4; ++dir) {
					pt nxt = it;
					nxt.F += dx[dir];
					nxt.S += dy[dir];
					if (u[nxt.F][nxt.S]) {
						vv.pb(nxt);
					}
 	            }

			}


		} 

//		if (ans > n + m) break;
	}
	assert(0);

   	return Accepted;
}

// B...a

Measured memory_bytes: 12416