/*
************************************************
*           * KAMRUL HASAN TUSHER              *
*           * JAHANGIRNAGAR UNIVERSITY         *
*           * CSE-45                           *
************************************************
*/
#include <bits/stdc++.h>

using namespace std;

typedef     long long               ll;
typedef     unsigned long long      ull;
typedef     pair<int, int>          pii;
typedef     pair<ll, ll>            pll;
typedef     pair<double, double>    pdd;
typedef     vector<double>          vd;
typedef     vector<ll>              vl;
typedef     vector<int>             vi;
typedef     vector<pii>             vii;
typedef     vector<pll>             vll;
typedef     vector<char>            vcc;
typedef     vector<string>          vss;
typedef     queue<int>              qi;
typedef     queue<pii>              qii;
typedef     priority_queue<int>     PQ;
typedef     priority_queue<pii>     PQII;

#define     sc                scanf
#define     pf                printf
#define     NL                printf("\n")
#define     pb                push_back
#define     mk_pr             make_pair
#define     NIL               -1
#define     VISITED           1
#define     UNVISITED         0
#define     PI                acos(-1.0)
#define     eps               0.0000001
#define     modulo            1000000007
#define     MX                262345
#define     INF               10000000000

#define     READ              freopen("INPUT.in", "r", stdin)
#define     WRITE             freopen("OUTPUT.out", "w", stdout)
#define     read              freopen("INPUT.txt", "r", stdin)
#define     write             freopen("OUTPUT.txt", "w", stdout)
#define     starr(arr, n, v)  for(int i=0; i<n; i++) arr[i] = v
#define     FasterIO          ios_base :: sync_with_stdio(false); cin.tie(0);

int dx4[] = { 0, 0, 1, -1 };    ///4 connected
int dy4[] = { -1, 1, 0, 0 };
int dx8[] = { 0, 0, 1, -1, -1, -1, 1, 1 };  /// 8 connected
int dy8[] = { -1, 1, 0, 0, 1, -1, -1, 1 };

ll LCM( ll a, ll b ) { return ( (a*b)/__gcd( a, b) ); }
ll POW( ll b, ll p ) { if( !p ) { return 1; } ll x = POW( b, p/2 ); x*=x; if( p&1 ) { x*=b; } return x; }
ll bigMod( ll b, ll p, ll m ) {  if( !p ) { return 1; } ll x = bigMod( b, p/2, m ); x = (x*x) % m; if( p&1 ) { x = (b*x) % m; } return x;  }
ll inv_mod( ll x ) { return bigMod( x, modulo-2, modulo ); }
///int prnt[500]; int prnt_find( int x ) { if( x == prnt[x] ) return x; return prnt[x] = prnt_find( prnt[x] ); }

///struct data{ int u, v, w; };

/*...........................................................................................................................................*/

int h, w, k;
string g[850];
int level[850][850];
bool vis[850][850];

bool valid( int x, int y ) {
        if( x < 0 || y < 0 || x >= h || y >= w || level[x][y] != -1 ) return false;
        return true;
}

void bfs( int x, int y ) {
        qi q;
        int i, j;
        memset(level, NIL, sizeof level);
        memset(vis, UNVISITED, sizeof vis);
        q.push(x), q.push(y);
        level[x][y] = 0, vis[x][y] = VISITED;

        while( !q.empty() ) {
                x = q.front(), q.pop();
                y = q.front(), q.pop();
                int t = k;
                for(i=x+1; t>0 && i<h; i++, t--) {
                        if( vis[i][y] == UNVISITED ) {
                                if( g[i][y] == '.' ) {
                                        q.push(i), q.push(y);
                                        level[i][y] = level[x][y]+1;
                                        vis[i][y] = VISITED;
                                }
                                else {
                                        int p = k;
                                        for(j=i; p>0 && j<h; j++, p--) {
                                                if( !vis[j][y] && g[j][y] == '#' ) {
                                                        q.push(j), q.push(y);
                                                        level[j][y] = level[x][y]+2;
                                                        vis[j][y] = VISITED;
                                                        g[j][y] = '.';
                                                        //level[j][y] = level[x][y]+1;
                                                }
                                                else break;
                                        }
                                        break;
                                }
                        }
                        else {
                                break;
                        }
                }

                t = k;
                for(i=x-1; t>0 && i>=0; i--, t--) {
                        if( vis[i][y] == UNVISITED ) {
                                if( g[i][y] == '.' ) {
                                        q.push(i), q.push(y);
                                        level[i][y] = level[x][y]+1;
                                        vis[i][y] = VISITED;
                                }
                                else {
                                        int p = k;
                                        for(j=i; p>0 && j>=0; j--, p--) {
                                                if( !vis[j][y] && g[j][y] == '#' ) {
                                                        q.push(j), q.push(y);
                                                        level[j][y] = level[x][y]+2;
                                                        vis[j][y] = VISITED;
                                                        g[j][y] = '.';
                                                        //level[j][y] = level[x][y]+1;
                                                }
                                                else break;
                                        }
                                        break;
                                }
                        }
                        else {
                                break;
                        }
                }

                t = k;
                for(i=y+1; t>0 && i<w; i++, t--) {
                        if( vis[x][i] == UNVISITED ) {
                                if( g[x][i] == '.' ) {
                                        q.push(x), q.push(i);
                                        level[x][i] = level[x][y]+1;
                                        vis[x][i] = VISITED;
                                }
                                else {
                                        int p = k;
                                        for(j=i; p>0 && j<w; j++, p--) {
                                                if( !vis[x][j] && g[x][j] == '#' ) {
                                                        q.push(x), q.push(j);
                                                        level[x][j] = level[x][y]+2;
                                                        vis[x][j] = VISITED;
                                                        g[x][j] = '.';
                                                        //level[j][y] = level[x][y]+1;
                                                }
                                                else break;
                                        }
                                        break;
                                }
                        }
                        else {
                                break;
                        }
                }

                t = k;
                for(i=y-1; t>0 && i>=0; i--, t--) {
                        if( vis[x][i] == UNVISITED ) {
                                if( g[x][i] == '.' ) {
                                        q.push(x), q.push(i);
                                        level[x][i] = level[x][y]+1;
                                        vis[x][i] = VISITED;
                                }
                                else {
                                        int p = k;
                                        for(j=i; p>0 && j>=0; j--, p--) {
                                                if( !vis[x][j] && g[x][j] == '#' ) {
                                                        q.push(x), q.push(j);
                                                        level[x][j] = level[x][y]+2;
                                                        vis[x][j] = VISITED;
                                                        g[x][j] = '.';
                                                        //level[j][y] = level[x][y]+1;
                                                }
                                                else break;
                                        }
                                        break;
                                }
                        }
                        else {
                                break;
                        }
                }
//                for(int i=0; i<4; i++) {
//                        int x1 = x+dx4[i];
//                        int y1 = y+dy4[i];
//                        if( valid(x1, y1) ) {
//                                if( g[x1][y1] == '.' ) {
//                                        q.push(x),
//                                }
//                        }
//                }
        }

        //for(i=0; i<h; i++) cout << g[i] << endl;

        int mn = INT_MAX;

        for(i=0; i<w; i++) {
                if( level[0][i] != NIL ) mn = min(mn, level[0][i]);
        }
        for(i=0; i<w; i++) {
                if( level[h-1][i] != NIL ) mn = min(mn, level[h-1][i]);
        }
        for(i=0; i<h; i++) {
                if( level[i][0] != NIL ) mn = min(mn, level[i][0]);
        }
        for(i=0; i<h; i++) {
                if( level[i][w-1] != NIL ) mn = min(mn, level[i][w-1]);
        }

        cout << mn << endl;
}

int main() {
        ///write;
        FasterIO
        cin >> h >> w >> k;
        for(int i=0; i<h; i++) cin >> g[i];
        for(int i=0; i<h; i++) {
                for(int j=0; j<w; j++) {
                        if( g[i][j] == 'S' ) {
                                bfs(i, j);
                                return 0;
                        }
                }
        }

        return 0;
}
