#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
#include <iomanip>
#include <set>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> vp;
typedef vector<PL> vpl;
typedef vector<vector<P>> vvp;
typedef vector<vector<PL>> vvpl;
const int INF = 1001001001;
const ll LINF = 1e18;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define rep(i, n) for(ll i = 0; i < n; i++)
#define rrep(i, n) for(ll i = n; i >= 0; i--)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back

template <typename T>
T gcd(T a, T b) {
    return (a == 0) ? b : gcd(b%a, a);
}
template <typename T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

bool p_comp_fs(const PL p1, const PL p2){ return p1.first < p2.first;};
bool p_comp_fg(const PL p1, const PL p2){ return p1.first > p2.first;};
bool p_comp_ss(const PL p1, const PL p2){ return p1.second < p2.second;};
bool p_comp_sg(const PL p1, const PL p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}

template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }


ll H, W;

bool bok(ll h, ll w){
    return 0 <= h && h < H && 0 <= w && w < W;
}

vl dh = {-1,1,0,0};
vl dw = {0,0,-1,1};

int main(){
    ll K; cin >> H >> W >> K;
    vs A(H); rep(i, H) cin >> A[i];
    ll sh = -1, sw = -1;
    rep(i, H) rep(j, W) if(A[i][j] == 'S'){
        sh = i; sw = j; i = H; j = W;
    }
    
    priority_queue<pair<ll, pair<ll, PL>>> q; q.push({-1, {K, {sh, sw}}});
    vvl cost(H, vl(W, LINF)); cost[sh][sw] = 1;
    while (!q.empty()) {
        PL now = q.top().ss.ss;
        ll rem = q.top().ss.ff, cnt = -q.top().ff;
        q.pop();
        if(rem == 0){
            q.push({-(cnt+1), {K, now}});
        }
        else if(cnt == 1){
            bool updated = false;
            rep(i, 4){
                ll nh = now.ff + dh[i], nw = now.ss + dw[i];
                if(bok(nh, nw) && A[nh][nw] == '.' && cost[nh][nw] == LINF){
                    cost[nh][nw] = cnt;
                    q.push({-cnt, {rem-1, {nh,nw}}});
                    updated = true;
                }
            }
            if(!updated) q.push({-cnt, {0, now}});
        }
        else{
            rep(i, 4){
                ll nh = now.ff + dh[i], nw = now.ss + dw[i];
                if(bok(nh, nw) && cost[nh][nw] == LINF){
                    cost[nh][nw] = cnt;
                    q.push({-cnt, {rem-1, {nh,nw}}});
                }
            }
        }
    }
    
    ll ans = LINF;
    rep(i, W){
        chmin(ans, cost[0][i]); chmin(ans, cost[H-1][i]);
    }
    rep(i, H){
        chmin(ans, cost[i][0]); chmin(ans, cost[i][W-1]);
    }
    cout << ans << endstr;
    return 0;
}


Measured memory_bytes: 12152