#include <bits/stdc++.h>
using namespace std;
int ans = 1e9, h, w, k, xx, yy;
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};
bool visit[805][805];
char s[805][805];
bool valid(int x, int y) {
  if (x < 0 || y < 0 || x == w || y == h || visit[y][x]) return 0;
  return 1;
}
void solve(int x, int y, int step, int k_step, int cast) {
  if (step > ans) return;
  if (x == 0 || y == 0) {
    ans = min(ans, step);
    return;
  }
  for (int i = 0; i < 4; i++) {
    xx = x + dx[i];
    yy = y + dy[i];
    if (valid(xx, yy)) {
      visit[yy][xx] = 1;
      if (s[yy][xx] == '#') {
        if (cast == 1 || k_step == k) {
          step++;
          k_step = 0;
        } else if (cast == -1) {
          step += 2;
          k_step = 0;
        }
        k_step++;
        cast = 0;
      } else if (s[yy][xx] == '.') {
        if (cast == -1 || k_step == k) {
          step++;
          k_step = 0;
        }
        k_step++;
        cast = 1;
      }
      visit[yy][xx] = 1;
      solve(xx, yy, step, k_step, cast);
      visit[yy][xx] = 0;
    }
  }
  visit[y][x] = 0;
}
int main() {
  int x, y;
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 0; i < h; i++) {
    scanf("%s", &s[i]);
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        x = j;
        y = i;
      }
    }
  }
  visit[y][x] = 1;
  solve(x, y, 0, 0, -1);
  printf("%d\n", ans);
  return 0;
}
