#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
template <typename T>
void read(T &x) {
  char ch = getchar();
  int f = 1;
  for (x = 0; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
    ;
  if (ch == '-') f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar())
    ;
  x *= f;
}
int n, m, k, ans, x, y;
char mp[810][810];
bool vis[810][810];
char getopt() {
  char ch = getchar();
  while (ch != '#' && ch != '.' && ch != 'S') ch = getchar();
  return ch;
}
int calc(int x, int y) {
  if (min(min(x - 1, n - x), min(y - 1, m - y)) == 0) return 0;
  return (min(min(x - 1, n - x), min(y - 1, m - y)) - 1) / k + 1;
}
void dfs(int x, int y, int deep) {
  vis[x][y] = 1;
  if (deep > k) return;
  ans = min(ans, calc(x, y));
  for (int i = 0; i < 4; i++) {
    int newx = x + dir[i][0];
    int newy = y + dir[i][1];
    if (newx >= 1 && newx <= n && newy >= 1 && newy <= m &&
        mp[newx][newy] == '.' && !vis[newx][newy])
      dfs(newx, newy, deep + 1);
  }
}
int main() {
  read(n), read(m), read(k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      mp[i][j] = getopt();
      if (mp[i][j] == 'S') x = i, y = j;
    }
  ans = 0x3f3f3f3f;
  dfs(x, y, 0);
  printf("%d", ans + 1);
  return 0;
}
