#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<unordered_set>
#include<sstream>
#include<vector>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
#ifdef LOCAL
#include"rprint.hpp"
#else
template <class... T> void printl(T&&...){ }
template <class... T> void printc(T&&...){ }
template <class... T> void prints(T&&...){ }
template <class... T> void printd(T&&...){ }
template <class... T> void printArr(T&&...){ }
#endif

struct Elem{
    int y, x, blocks;
    bool stay = false;
    bool operator < (const Elem& e) const{
        // return y != e.y ? y < e.y : x != e.x ? x < e.x : blocks < e.blocks;
        return y != e.y ? y < e.y : x < e.x;
    }
};

int main(){
    int h, w, k;
    cin >> h >> w >> k;
    vector<vector<char>> as(h, vector<char> (w));
    set<Elem> q;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> as[i][j];
            if(as[i][j] == 'S'){
                as[i][j] = '.';
                q.insert({i, j, 0});
            }
        }
    }
    // map<pair<int, int>, int> visited;
    vector<vector<int>> visited(h, vector<int> (w, 10000));
    for(int i=0;i<800;i++){
        prints(i);
        prints(q.size());
        auto q2 = q;
        for(int j=0;j<k;j++){
            set<Elem> next;
            for(const Elem& e : q){
                if(visited[e.y][e.x] < e.blocks){
                    continue;
                }
                visited[e.y][e.x] = e.blocks;
                auto f = [&](int y, int x, int b){
                    int nb = b + (as[y][x] == '#');
                    if(nb <= k * i){
                        // prints(y, x, b);
                        if(y == 0 || y == h - 1 || x == 0 || x == w - 1){
                            cout << i + 1 << '\n';
                            exit(0);
                        }
                        if(visited[y][x] <= nb){
                            return;
                        }
                        next.insert({y, x, nb});
                    }
                };
                if(visited[e.y][e.x] <= e.blocks){
                    // next.insert({e.y, e.x, e.blocks});
                }
                f(e.y + 1, e.x, e.blocks);
                f(e.y - 1, e.x, e.blocks);
                f(e.y, e.x + 1, e.blocks);
                f(e.y, e.x - 1, e.blocks);
            }
            next.swap(q);
        }
        for(const Elem& e : q2){
            if(e.stay == false){
                q.insert({e.y, e.x, e.blocks, true});
            }
        }
    }
    return 0;
}
