#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;

#define fr first
#define sc second
#define pb push_back
#define mpr make_pair
#define ppb pop_back()
#define ins insert
#define sz(s) int(s.size())
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound
int n, m, k, d[1000][1000], b[1000][1000];
bool in(int x, int y){
	if(x < 1 || x > n || y < 1 || y > m)return 0;
	return 1;
}
pii s;
set <piii> st;

char c;
int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> k;
	for(int i = 1;i <= n; ++ i){
		for(int j = 1;j <= m; ++ j){
			cin >> c;
			if(i == 1 || j == 1 || i == n || j == m){
				d[i][j] = (c == '#');
				st.ins(mpr(d[i][j], mpr(i, j)));
			}
			else {
				b[i][j] = 1;
				b[i][j] += (c == '#');
				if(c == 'S')s = mpr(i, j);
				d[i][j] = 1000;
			}
		}
	}
	while(sz(st)){
		piii p2 = *st.begin();
		pii p = p2.sc;
		st.erase(st.begin());
		for(int i = -1;i <= 1; ++ i){
			for(int j = -1;j <= 1; ++ j){
				if(abs(i + j) != 1)continue;
				if(!in(p.fr + i, p.sc + j))continue;
				if(d[p.fr + i][p.sc + j] > d[p.fr][p.sc] + b[p.fr + i][p.sc + j]){
					st.erase(mpr(d[p.fr + i][p.sc + j], mpr(p.fr + i, p.sc + j)));
					d[p.fr + i][p.sc + j] = d[p.fr][p.sc] + b[p.fr + i][p.sc + j];
					st.ins(mpr(d[p.fr + i][p.sc + j], mpr(p.fr + i, p.sc + j)));
				}
			}
		}
	}
	if(d[s.fr][s.sc] > 2 * k){
		cout << k
		return 0;
	}
	cout << d[s.fr][s.sc];
	return 0;
}