#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
char s[maxn][maxn];
pair<int, int> q[maxn * maxn];
int ds[maxn][maxn];
int l, r;
inline int iabs(int x) { return x >= 0 ? x : -x; }
int main() {
  int n, m, K;
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
  memset(ds, -1, sizeof(ds));
  l = 1;
  r = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (s[i][j] == 'S') {
        q[++r] = make_pair(i, j);
        ds[i][j] = 0;
      }
  for (; l <= r;) {
    pair<int, int> u = q[l++];
    int x = u.first, y = u.second;
    if (ds[x][y] >= K) continue;
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy)
        if (iabs(dx) + iabs(dy) == 1) {
          int nx = x + dx, ny = y + dy;
          if (ds[nx][ny] < 0 && s[nx][ny] != '#') {
            ds[nx][ny] = ds[x][y] + 1;
            q[++r] = make_pair(nx, ny);
          }
        }
  }
  l = 1;
  r = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (ds[i][j] >= 0) {
        ds[i][j] = 0;
        q[++r] = make_pair(i, j);
      }
  for (; l <= r;) {
    pair<int, int> u = q[l++];
    int x = u.first, y = u.second;
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy)
        if (iabs(dx) + iabs(dy) == 1) {
          int nx = x + dx, ny = y + dy;
          if (ds[nx][ny] < 0) {
            ds[nx][ny] = ds[x][y] + 1;
            q[++r] = make_pair(nx, ny);
          }
        }
  }
  int mi = (int)(1e9);
  for (int i = 1; i <= n; ++i) mi = min(mi, min(ds[i][1], ds[i][m]));
  for (int j = 1; j <= m; ++j) mi = min(mi, min(ds[1][j], ds[n][j]));
  printf("%d\n", (mi + K - 1) / K + 1);
  return 0;
}
