#include <bits/stdc++.h>
using namespace std;
struct state {
  int cast;
  int steps;
  int broken;
} s[888][888];
int h, w, k;
char a[888][888];
pair<int, int> q[888 * 888];
int b, e;
int mov[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int main() {
  scanf("%d %d %d\n", &h, &w, &k);
  for (int i = 0; i < h; i++) scanf("%s", a[i]);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        s[i][j].cast = 1;
        q[e++] = make_pair((i), (j));
        break;
      }
    }
  }
  while (b < e) {
    int x = q[b].first, y = q[b].second;
    b++;
    for (int d = 0; d < 4; d++) {
      int nx = x + mov[d][0], ny = y + mov[d][1];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
        if (s[nx][ny].cast) continue;
        s[nx][ny] = s[x][y];
        if (a[nx][ny] == '.') {
          if (s[x][y].steps == k) {
            s[nx][ny].cast++;
            s[nx][ny].broken += k;
            s[nx][ny].steps = 0;
          }
          s[nx][ny].steps++;
        } else {
          if (s[x][y].broken == 0) {
            s[nx][ny].cast++;
            s[nx][ny].broken += k;
            s[nx][ny].steps = 0;
          }
          s[nx][ny].broken--;
          s[nx][ny].steps++;
        }
        q[e++] = make_pair((nx), (ny));
      }
    }
  }
  int ans = 123456789;
  for (int i = 0; i < w; i++) {
    if (s[0][i].cast) ans = min(ans, s[0][i].cast);
    if (s[h - 1][i].cast) ans = min(ans, s[h - 1][i].cast);
  }
  for (int i = 0; i < h; i++) {
    if (s[i][0].cast) ans = min(ans, s[i][0].cast);
    if (s[i][w - 1].cast) ans = min(ans, s[i][w - 1].cast);
  }
  printf("%d\n", ans);
  return 0;
}
