#include <bits/stdc++.h>
using u4t = std::uint32_t;
using i4t = std::int32_t;
using u8t = std::uint64_t;
using i8t = std::int64_t;
struct process {
  int H, W, K;
  std::vector<int> dan;
  std::vector<int> nopen;
  std::vector<int> hosu2;
  int mcost;
  void from(int x, int y, int rest_hosu, int cost) {
    walk(x - 1, y, rest_hosu, cost);
    walk(x + 1, y, rest_hosu, cost);
    walk(x, y - 1, rest_hosu, cost);
    walk(x, y + 1, rest_hosu, cost);
  }
  void walk(int x, int y, int rest_hosu, int cost) {
    if (x < 0 || y < 0 || x >= W || y >= H) return;
    rest_hosu--;
    if (rest_hosu == 0) return;
    if (dan[y * W + x] == '#') cost++;
    if (cost > mcost) return;
    if (nopen[y * W + x] < 0 || nopen[y * W + x] > cost ||
        hosu2[y * W + x] < rest_hosu) {
      nopen[y * W + x] = cost;
      hosu2[y * W + x] = rest_hosu;
      from(x, y, rest_hosu, cost);
    }
  }
  void dbg_print_stat() {
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        char c =
            nopen[y * W + x] >= 0 ? '0' + nopen[y * W + x] : dan[y * W + x];
        std::cout << c;
      }
      std::cout << std::endl;
    }
    std::cout << std::endl;
  }
  void run() {
    std::cin >> H >> W >> K;
    dan.resize(H * W, 0);
    nopen.resize(H * W, -1);
    std::string line;
    std::getline(std::cin, line);
    int x0 = 0, y0 = 0;
    for (int y = 0; y < H; y++) {
      std::getline(std::cin, line);
      for (int x = 0; x < W; x++) {
        char c = line[x];
        if (c == 'S') {
          c = '.';
          x0 = x;
          y0 = y;
        }
        dan[y * W + x] = c;
      }
    }
    mcost = 0;
    nopen[y0 * W + x0] = 0;
    int count = 0;
    for (;;) {
      count++;
      hosu2.clear();
      hosu2.resize(H * W, -1);
      for (int y = 1; y < H - 1; y++) {
        for (int x = 1; x < W - 1; x++) {
          if (nopen[y * W + x] < 0) continue;
          from(x, y, K + 1, nopen[y * W + x]);
        }
      }
      mcost += K;
      bool goal = false;
      for (int y = 0; y < H; y++) {
        if (nopen[y * W + W - 1] >= 0 || nopen[y * W + 0] >= 0) {
          goal = true;
          break;
        }
      }
      for (int x = 0; x < H; x++) {
        if (nopen[(H - 1) * W + x] >= 0 || nopen[x] >= 0) {
          goal = true;
          break;
        }
      }
      if (goal) break;
    }
    std::cout << count << std::endl;
  }
};
int main() {
  process p;
  p.run();
  return 0;
}
