#include <bits/stdc++.h>
using namespace std;
struct jie {
  int x, y, k;
};
int n, m, us[805][805], a[805][805], dx[4] = {-1, 1, 0, 0},
                                     dy[4] = {0, 0, -1, 1}, ans = 1000000007;
char mp[805][805];
queue<jie> q;
void bfs(int x1, int y1, int k) {
  jie p;
  p.x = x1, p.y = y1, p.k = 0;
  if (x1 == n || y1 == m) {
    ans = 0;
  } else {
    ans = min(ans, (m - y1 + k - 1) / k + 1);
    ans = min(ans, (n - x1 + k - 1) / k + 1);
  }
  us[x1][y1] = 1;
  q.push(p);
  while (!q.empty()) {
    jie pp = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = pp.x + dx[i];
      int ny = pp.y + dy[i];
      int t = pp.k + 1;
      if (nx <= n && ny <= m && nx >= 1 && ny >= 1 && us[nx][ny] == 0 &&
          mp[nx][ny] == '.') {
        us[nx][ny] = 1;
        if (nx == n || ny == m) {
          ans = 1;
        } else {
          ans = min(ans, (m - ny + k - 1) / k + 1);
          ans = min(ans, (n - nx + k - 1) / k + 1);
        }
        if (t < k) {
          p.x = nx;
          p.y = ny;
          p.k = t;
          q.push(p);
        }
      }
    }
  }
}
int main() {
  int i, j, k;
  scanf("%d %d %d", &n, &m, &k);
  for (i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (mp[i][j] == 'S') {
        bfs(i, j, k);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
