/*

         o###########oo
      o##"          ""##o
    o#"                "##
  o#"                    "#o
 #"  ##              ##   "##
#"                          ##
#  ###################       #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#o                           #
"#o                         ##
 "#o                       ##
  "#o                    o#"
   "#o                  ##
     "#o              o#"
       "#ooo      ooo#######oo
        ###############   "######o
     o###""        "###o      # ###
   o###o     oooo    ###    oo####"
 o###**#     #**#   ############"
 ""##""""""""""###########    #
    # oooooooo#"#**     ##    #
    # #       # # **    ##    #
    #o#       #o#  *****###ooo#
                        ##
                        ##   o###o
                        ## o##***##
               o########## #***#**##o
             o##"   ""###  #***##***#
 o#######o  ###   oo####   ##**####*#
o##"  ""#############""     ##****###
##"         ##              ##*##*###
##          ###              ##### ##
##           ###              # ##  #
##            ##                 #
##             ##
##             ###
##              ###oo
###              ""###
 ###
  ###
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

void setmax(int &x, int y){
    x = max(x, y);
}

void setmin(int &x, int y){
    x = max(x, y);
}

void setmax(ll &x, ll y){
    x = max(x, y);
}

void setmin(ll &x, ll y){
    x = max(x, y);
}

ll AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MOD = 1e9 + 993;

const int mod = 1e9 + 7;

ll myrand(){
    ll ZR = (XR * AR + YR * BR + CR) % MOD;
    XR = YR;
    YR = ZR;
    return ZR;
}

string prm(string s){
    string t;
    for (int i = 1; i < s.length(); i++)
        t.push_back(s[i]);
    t.push_back(s[0]);
    return t;
}

ll bpow(ll x, ll y){
    if (y == 0)
        return 1;
    if (y == 1)
        return x;
    ll ret = bpow(x, y >> 1);
    ret = (ret * ret) % mod;
    if (y & 1)
        ret = (ret * x) % mod;
    return ret;
}

ll bdiv(ll x, ll y){
    return (x * bpow(y, mod - 2)) % mod;
}

int md(int x){
    if (x >= mod)
        x -= mod;
    if (x < 0)
        x += mod;
    return x;
}

ll MD(ll x){
    x %= MOD;
    if (x < 0)
        x += MOD;
    return x;
}

ll sqr(ll x){
    return x * x;
}

const ll llinf = 2e18;

const ld EPS = 1e-9;

const int maxn = 1e5 + 100, inf = 1e9 + 100, sq = 300;

int n, m, k;

int a[800][800];

int sti, stj;

bool s[800][800];

pair<int, int> b[800 * 800];

int sz;

int get(int x, int y){
    return min((x + k - 1) / k, min((y + k - 1) / k, min((n - 1 - x + k - 1) / k, (m - 1 - y + k - 1) / k)));
}

int main()
{
    #ifdef ONPC
    ifstream cin("a.in");
    ofstream cout("a.out");
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    #else
    //ifstream cin("a.in");
    //ofstream cout("a.out");
    //freopen("trap.in", "r", stdin);
    //freopen("trap.out", "w", stdout);
    #endif // ONPC
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++){
        char c;
        cin >> c;
        a[i][j] = inf;
        if (c == 'S')
            sti = i, stj = j, a[i][j] = 0;
        s[i][j] = (c != '#');
    }
    sz = 1;
    b[0] = make_pair(sti, stj);
    int answer = inf;
    for (int it = 0; it < sz; it++){
        int i = b[it].first, j = b[it].second;
        answer = min(answer, 1 + get(i, j));
        if (a[i][j] == k)
            continue;
        for (int x = -1; x <= 1; x++)
            for (int y = -1; y <= 1; y++)
                if (x * y == 0 && i + x >= 0 && i + x < n && j + y >= 0 && j + y < m && a[i + x][j + y] == inf && s[i + x][j + y])
                    a[i + x][j + y] = a[i][j] + 1, b[sz] = make_pair(i + x, j + y), sz++;
    }
    cout << answer;
}


Measured memory_bytes: 8448