// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

using namespace std;

typedef long long ll;   

const int MAXN = (int)8e2 + 5;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

vector<pair<int, int> > mem[MAXN * 3];

bool vis[MAXN][MAXN];

char s[MAXN][MAXN];

int d[MAXN][MAXN];

int n, m, k, ans;

void calcD(int stx, int sty) {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            d[i][j] = -1;
        }
    }

    deque<pair<int, int > > S;
    S.pb(mp(stx, sty));
    d[stx][sty] = 0;

    while (!S.empty()) {
        int x = S.front().fi, y = S.front().se;
        S.pop_front();

        for (int dir = 0; dir < 4; ++dir) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];

            if (nx < 1 || nx > n || ny < 1 || ny > m) {
                continue;
            }

            if (d[nx][ny] == -1) {
                if (s[nx][ny] == '#') {
                    d[nx][ny] = d[x][y] + 1;
                    S.pb(mp(nx, ny));
                }
                else {
                    d[nx][ny] = d[x][y];
                    S.push_front(mp(nx, ny));
                }
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (s[i][j] == '#') {
                mem[d[i][j]].pb(mp(i, j));
            }
        }
    }
}

void solve() {
    scanf("%d %d %d", &n, &m, &k);
    int stx, sty;

    for (int i = 1; i <= n; ++i) {
        scanf("%s", s[i] + 1);
    }
    
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (s[i][j] == 'S') {
                stx = i;
                sty = j;
            }
        }
    }

    calcD(stx, sty);    
    vector<pair<int, int> > vv;
    vv.pb(mp(stx, sty));
    vis[stx][sty] = 1;

    while (1) {
        queue<pair<pair<int, int>, int> > q;
        ++ans;

        for (auto it : vv) {
            q.push(mp(mp(it.fi, it.se), 0));
        }

        vv.clear();

        while (!q.empty()) {
            int x = q.front().fi.fi;
            int y = q.front().fi.se;
            int cur = q.front().se;
            q.pop();

            if (x == 1 || x == n || y == 1 || y == m) {
                printf("%d\n", ans);
                return;
            }

            if (cur == k) {
                vv.pb(mp(x, y));
                continue;
            }

            vis[x][y] = 1;
            
            for (int dir = 0; dir < 4; ++dir) {
                int nx = x + dx[dir];
                int ny = y + dy[dir];

                if (nx < 1 || nx > n || ny < 1 || ny > m) {
                    continue;
                }

                if (s[nx][ny] != '#') {
                    if (!vis[nx][ny]) {
                        vis[nx][ny] = 1;
                        q.push(mp(mp(nx, ny), cur + 1));
                    }
                }
            }
        }       

        for (int i = (ans - 1) * k + 1; i <= ans * k; ++i) {
            if (i <= n + m) {
                for (auto it : mem[i]) {
                    s[it.fi][it.se] = '.';

                    for (int dir = 0; dir < 4; ++dir) {
                        int x = it.fi + dx[dir];
                        int y = it.se + dy[dir];

                        if (x < 1 || x > n || y < 1 || y > m) {
                            continue;
                        }

                        if (vis[x][y]) {
                            vv.pb(mp(x, y));
                        }
                    }
                }
            }
        }
    }

    printf("%d\n", ans);
}

int main() {
    int tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 11264