#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 805;
int n, m, k, ans;
char a[N][N];
bool vis[N][N];
bool chk(int x, int y) {
  return a[x][y] != '#' && 1 <= x && x <= n && 1 <= y && y <= m && !vis[x][y];
}
void dfs(int x, int y) {
  vis[x][y] = true;
  (ans > min(x - 1, n - x) ? ans = min(x - 1, n - x) : ans);
  (ans > min(y - 1, m - y) ? ans = min(y - 1, m - y) : ans);
  if (chk(x - 1, y)) dfs(x - 1, y);
  if (chk(x, y - 1)) dfs(x, y - 1);
  if (chk(x + 1, y)) dfs(x + 1, y);
  if (chk(x, y + 1)) dfs(x, y + 1);
}
int main(int argc, char *argv[]) {
  scanf("%d%d%d", &n, &m, &k);
  ans = n * m;
  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') {
        dfs(i, j);
      }
    }
  printf("%d\n", (ans + k - 1) / k + 1);
  return 0;
}
