#include <bits/stdc++.h>
using namespace std;
const int MAXN = 805;
const int INF = 0x3f3f3f3f;
int n, m, K, ans = INF;
int cs[MAXN][MAXN], rs[MAXN][MAXN];
bool vis[MAXN][MAXN];
char s[MAXN][MAXN];
pair<int, int> S;
int df[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int f(int first, int second, int k) {
  int mine = INF;
  mine = min(mine, max((first - 1 + K - 1) / K,
                       (cs[first][second] - cs[0][second] + K - 1) / K + k));
  mine =
      min(mine, max((n - first + K - 1) / K,
                    (cs[n][second] - cs[first - 1][second] + K - 1) / K + k));
  mine = min(mine, max((second - 1 + K - 1) / K,
                       (rs[first][second] - rs[first][0] + K - 1) / K + k));
  mine = min(mine, max((m - second + K - 1) / K,
                       (rs[first][m] - rs[first][second - 1] + K - 1) / K + k));
  return mine;
}
void dfs(int first, int second, int dep = 0) {
  if (dep > K || vis[first][second]) return;
  vis[first][second] = 1;
  if (first != S.first && second != S.second)
    ans = min(ans, f(first, second, 0) + 1);
  for (int i = 0; i < 4; i++) {
    int nx = first + df[i][0];
    int ny = second + df[i][1];
    if (nx < 1 || ny < 1 || nx > n || ny > m || s[nx][ny] == '#' || vis[nx][ny])
      continue;
    dfs(nx, ny, dep + 1);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') S.first = i, S.second = j;
      if (s[i][j] == '#') cs[i][j] = rs[i][j] = 1;
      cs[i][j] += cs[i - 1][j];
      rs[i][j] += rs[i][j - 1];
    }
  }
  ans = min(ans, f(S.first, S.second, 1));
  dfs(S.first, S.second);
  printf("%d", ans);
  fclose(stdin);
  fclose(stdout);
}
