#include <bits/stdc++.h>
using namespace std;
char a[810][810];
int vis[810][810];
int n, m, k, bx, by, Min, Ans;
void dfs(int x, int y, int s) {
  if (s > k) return;
  if (x == 1 || x == n || y == 1 || y == m) {
    puts("1");
    exit(0);
  }
  if (vis[x][y]) return;
  vis[x][y] = 1;
  int now = min(min(x - 1, n - x), min(y - 1, m - y));
  if (now < Min) Min = now;
  if (a[x + 1][y] != '#') dfs(x + 1, y, s + 1);
  if (a[x - 1][y] != '#') dfs(x - 1, y, s + 1);
  if (a[x][y + 1] != '#') dfs(x, y + 1, s + 1);
  if (a[x][y - 1] != '#') dfs(x, y - 1, s + 1);
}
inline int Ceil(int x, int y) {
  if (x % y)
    return x / y + 1;
  else
    return x / y;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'S') bx = i, by = j;
  Min = min(min(bx - 1, n - bx), min(by - 1, m - by));
  dfs(bx, by, 0);
  Ans = 1 + Ceil(Min, k);
  printf("%d\n", Ans);
  return 0;
}
