#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <assert.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>

//#define DEBUG 1

#define for0(i,n) for (int i=0; i<n; i++)
#define forr(i,n) for (int i=n-1; i>=0; i--)
#define fori(i,a,b) for (int i=a; i<=b; i++)
#define iter(c) for(auto it=c.begin(); it!=c.end(); it++)
#define vec(x) vector< x >
#define pb push_back
#define ms(a,z) memset(a,z,sizeof(a));
#define mp make_pair
#define X first
#define Y second
#define sqr(x) 1LL*(x)*(x)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define all(a) a.begin(),a.end()
#define size(x) (int)(x).size()
#define read(x) int x; scanf("%d",&x);
#ifdef DEBUG
#define nl cout<<"\n";
#define pr(x) cout<<(x)<<" ";
#define prl(x) cout<<#x " = "<<x<<endl;
#define prp(x) cout<<"("<<(x).first<<" "<<(x).second<<") ";
#define printv(v) {for(int _=0; _<size(v); _++) cout<<v[_]<<" "; cout<<"\n";}
#define printa(a,s) {for (int _=0; _<s; _++) cout<<a[_]<<" "; cout<<"\n";}
#define print2D(a,m,n) {for (int _=0; _<m; _++) {for (int __=0; __<n; __++) cout<<a[_][__]<<" "; cout<<"\n";} cout<<"\n";}
#define debug cout<<"ok at line "<<__LINE__<<endl;
#else
#define nl
#define pr(x)
#define prl(x)
#define prp(x)
#define printv(v)
#define printa(a,s)
#define print2D(a,m,n)
#define debug
#endif

using namespace std;

typedef long long ll;

const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;

int H,W,K;
bool is_open[805][805], is_reachable[805][805];
bool visited[805][805];
bool visited2[805][805];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

struct cell
{
    int x,y;
    int steps;
    cell() {}
    cell(int x, int y): x(x), y(y), steps(0) {}
    cell(int x, int y, int steps): x(x), y(y), steps(steps) {}
};
bool operator < (const cell & a, const cell & b)
{
    return a.steps > b.steps;
}

priority_queue<cell> opened;
priority_queue<cell> reached;

int unlock_rooms(int max_step)
{
    //prl(max_step);
    int num_unlocked = 0;
    while (!opened.empty()){
        cell top = opened.top();
        //pr(top.x) pr(top.y) pr(top.steps) nl
        if (top.steps > max_step) {
            return num_unlocked;
        }
        opened.pop();
        num_unlocked++;
        is_open[top.y][top.x] = true;

        for0(i,4) {
            cell next = cell(top.x+dx[i], top.y+dy[i], top.steps);
            if (0 <= next.x && next.x < W && 0 <= next.y && next.y < H && !visited[next.y][next.x]){
                visited[next.y][next.x] = true;
                if (!is_open[next.y][next.x])
                    next.steps++;
                opened.push(next);
            }
        }
    }

    return num_unlocked;
}

bool travel(int max_step)
{
    while (!reached.empty()){
        cell top = reached.top();
        if (top.steps > max_step) {
            return false;
        }
        reached.pop();

        if (top.x == 0 || top.x == W-1 || top.y == 0 || top.y == H-1)
            return true;

        if (!is_open[top.y][top.x]) {
            top.steps = max_step+1;
            reached.push(top);
            continue;
        }
        is_reachable[top.y][top.x] = true;

        for0(i,4) {
            cell next = cell(top.x+dx[i], top.y+dy[i], top.steps+1);
            if (0 <= next.x && next.x < W && 0 <= next.y && next.y < H && !visited2[next.y][next.x]){
                if (!is_open[next.y][next.x]) {
                    next.steps = max_step+1; //consider on the later cycle
                }
                visited2[next.y][next.x] = true;
                reached.push(next);
            }
        }
    }

    return false;
}

int main()
{
    #ifdef DEBUG
    freopen("C.txt","r",stdin);
    //freopen("","w",stdout);
    #endif

    scanf("%d %d %d",&H,&W,&K);
    cell start;
    for0(y,H) {
        string row;
        cin >> row;
        for0(x,size(row)){
            is_open[y][x] = (row[x] != '#');
            if (row[x] == 'S'){
                start = cell(x,y);
            }
        }
    }

    ms(visited,false);
    ms(visited2,false);
    ms(is_reachable,false);
    visited[start.y][start.x] = true;
    visited2[start.y][start.x] = true;
    opened.push(cell(start.x, start.y, 0));
    reached.push(cell(start.x, start.y, 0));

    int steps = 0;
    int max_step = K;
    bool done = false;
    while (!done) {
        print2D(is_open,H,W)
        done = travel(max_step);
        print2D(is_reachable,H,W);
        unlock_rooms(max_step);

        steps++;
        max_step += K;
    }

    cout<<steps<<endl;
    return 0;
}


Measured memory_bytes: 7476