#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int MAXK = -1;
const int MAXN = 1 << MAXK;
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 1.01e9;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int main() {
  int n, m, k;
  while (cin >> n >> m >> k) {
    vector<string> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<vector<int> > d(n, vector<int>(m, INF));
    queue<pair<int, int> > q;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 'S') {
          d[i][j] = 0;
          q.push(make_pair(i, j));
        }
      }
    }
    queue<pair<int, int> > q2;
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      if (d[x][y] == k) {
        while (!q2.empty()) {
          q.push(q2.front());
          q2.pop();
        }
      }
      for (int dk = 0; dk < 4; dk++) {
        int nx = x + dx[dk];
        int ny = y + dy[dk];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
        int nd = d[x][y] + 1;
        if (a[nx][ny] == '#') nd = max(nd, k + 1);
        if (d[nx][ny] <= nd) continue;
        d[nx][ny] = nd;
        if (a[nx][ny] == '#' && d[x][y] < k) {
          q2.push(make_pair(nx, ny));
        } else {
          q.push(make_pair(nx, ny));
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < n; i++) {
      ans = min(ans, (d[0][i] + k - 1) / k);
      ans = min(ans, (d[i][0] + k - 1) / k);
      ans = min(ans, (d[n - 1][i] + k - 1) / k);
      ans = min(ans, (d[i][m - 1] + k - 1) / k);
    }
    cout << ans << endl;
  }
  return 0;
}
