#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
const ld EPS = (ld) 1e-12;
const ll MOD = (ll) 1e9 + 7;

#define sz(x) (int) (x).size()
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb(s, t, x) (int) (lower_bound(s, t, x) - s)
#define ub(s, t, x) (int) (upper_bound(s, t, x) - s)
#define rep(i, f, t) for (auto i = f; i < t; i++)
#define per(i, f, t) for (auto i = f; i >= t; i--)

ll power(ll x, ll y, ll mod = MOD) {
    if (y == 0) {
        return 1;
    }
    if (y & 1) {
        return power(x, y - 1, mod) * x % mod;
    } else {
        ll tmp = power(x, y / 2, mod);
        return tmp * tmp % mod;
    }
}

void add(ll &x, ll y, ll mod = MOD) {
    x += y;
    if (x >= mod) x -= mod;
    if (x < 0) x += mod;
}

ll sum(ll x, ll y, ll mod = MOD) {
    add(x, y, mod);
    return x;
}

ll mult(ll x, ll y, ll mod = MOD) {
    return (x * y) % mod;
}

ll div(ll x, ll y, ll mod) {
    return x * power(y, mod - 2, mod);
}

template<typename A, typename B> bool mini(A &x, const B &y) {
    if (y < x) {
        x = y;
        return true;
    }
    return false;
}

template<typename A, typename B> bool maxi(A &x, const B &y) {
    if (y > x) {
        x = y;
        return true;
    }
    return false;
}

template<typename T> void read(T first, T last) {
    while (first != last) {
        cin >> *(first++);
    }
}

template<typename T> void print(T first, T last) {
    while (first != last) {
        cout << *(first++) << " ";
    }
    cout << "\n";
}

void run();

#define TASK ""

int main() {
#ifdef LOCAL
    if (strlen(TASK) > 0) {
        cerr << "Reminder: you are using file i/o, filename: " TASK "!" << endl << endl;
    }
#endif
#ifndef LOCAL
    if (strlen(TASK)) {
        freopen(TASK ".in", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    cout << fixed << setprecision(12);
    run();
    return 0;
}

// == SOLUTION == //

const int N = 808;

int n, m, k;
string a[N];
int d1[N][N], d2[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

bool check1(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m && a[x][y] != '#';
}

bool check2(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void run() {
    cin >> n >> m >> k;
    rep(i, 0, n) cin >> a[i];
    
    {
        queue<pair<int, int>> q;
        rep(i, 0, n) {
            rep(j, 0, m) {
                if (a[i][j] == 'S') {
                    q.push({i, j});
                } else {
                    d1[i][j] = INF;
                }
            }
        }
        
        while (sz(q)) {
            auto el = q.front();
            q.pop();
            int x = el.first;
            int y = el.second;
            rep(dir, 0, 4) {
                int nx = x + dx[dir];
                int ny = y + dy[dir];
                if (check1(nx, ny) && mini(d1[nx][ny], d1[x][y] + 1)) {
                    q.push({nx, ny});
                }
            }
        }
    }
    
    {
        queue<pair<int, int>> q;
        rep(i, 0, n) {
            rep(j, 0, m) {
                if (d1[i][j] <= k) {
                    d2[i][j] = 0;
                    q.push({i, j});
                    if (min(i, j) == 0 || i == n - 1 || j == m - 1) {
                        cout << "1\n";
                        return;
                    }
                } else {
                    d2[i][j] = INF;
                }
            }
        }
        
        while (sz(q)) {
            auto el = q.front();
            q.pop();
            int x = el.first;
            int y = el.second;
            rep(dir, 0, 4) {
                int nx = x + dx[dir];
                int ny = y + dy[dir];
                if (check2(nx, ny) && mini(d2[nx][ny], d2[x][y] + 1)) {
                    q.push({nx, ny});
                }
            }
        }
        
        int ans = INF;
        
        rep(i, 0, n) {
            rep(j, 0, m) {
                if (min(i, j) == 0 || i == n - 1 || j == m - 1) {
                    mini(ans, (d2[i][j] + k - 1) / k + 1);
                }
            }
        }
        
        cout << ans << "\n";
    }
}


Measured memory_bytes: 6272