#include <bits/stdc++.h>
const int kN = 805;
int H, W, K, step[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}, d[kN][kN],
             d2[kN][kN], stx, sty;
char g[kN][kN];
bool vis[kN][kN];
std::queue<std::pair<int, int> > que;
void Bfs() {
  memset(vis, false, sizeof(vis));
  que.push(std::make_pair(stx, sty));
  while (!que.empty()) {
    std::pair<int, int> cur = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = cur.first + step[i][0], ny = cur.second + step[i][1], nd;
      if (nx <= 0 || ny <= 0 || nx > H || ny > W) continue;
      nd = d[cur.first][cur.second] + 1;
      if (g[nx][ny] != '.') continue;
      if (!vis[nx][ny]) {
        d[nx][ny] = nd;
        vis[nx][ny] = true;
        que.push(std::make_pair(nx, ny));
      }
    }
  }
}
void Bfs2() {
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      if (d[i][j] <= K) que.push(std::make_pair(i, j));
  while (!que.empty()) {
    std::pair<int, int> cur = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = cur.first + step[i][0], ny = cur.second + step[i][1], nd;
      if (nx <= 0 || ny <= 0 || nx > H || ny > W) continue;
      nd = d2[cur.first][cur.second] + 1;
      if (!vis[nx][ny]) {
        d2[nx][ny] = nd;
        vis[nx][ny] = true;
        que.push(std::make_pair(nx, ny));
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i = 1; i <= H; ++i) {
    scanf("%s", g[i] + 1);
  }
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      if (g[i][j] == 'S') {
        g[i][j] = '.';
        stx = i;
        sty = j;
      }
  Bfs();
  Bfs2();
  int ans = 0;
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j) {
      if (i == 1 || j == 1 || i == H || j == W) {
        ans = std::max(ans, (d2[i][j] - 1) / K + 1);
      }
    }
  printf("%d\n", ans);
  return 0;
}
