#include <bits/stdc++.h>
using namespace std;
struct mystruct {
  int x, y, doors, steps;
};
deque<mystruct> Q[802 * 2];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char s[802][802];
int n, m, k, d[802][802];
bool isEdge(mystruct node) {
  if (node.x == 1 || node.x == n || node.y == 1 || node.y == m) return 1;
  return 0;
}
int main() {
  int i, j;
  mystruct start, node;
  scanf("%d%d%d\n", &n, &m, &k);
  for (i = 1; i <= n; ++i) scanf("%s\n", s[i] + 1);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (s[i][j] == 'S') start.x = i, start.y = j;
  if (isEdge(start)) {
    printf("0");
    return 0;
  }
  start.doors = 0;
  start.steps = 0;
  Q[1].push_back(start);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) d[i][j] = 0x3f3f3f3f;
  int limit = n + m;
  for (i = 1; i <= limit; ++i)
    while (!Q[i].empty()) {
      node = Q[i].front();
      Q[i].pop_front();
      if (d[node.x][node.y] < i) continue;
      d[node.x][node.y] = i;
      if (isEdge(node)) {
        printf("%d", i);
        return 0;
      }
      mystruct nxt;
      for (int j = 0; j < 4; ++j) {
        nxt.x = node.x + dx[j];
        nxt.y = node.y + dy[j];
        if (s[nxt.x][nxt.y] == '.') {
          if (node.steps == k) {
            nxt.doors = 0;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          } else {
            nxt.doors = node.doors;
            nxt.steps = node.steps + 1;
            Q[i].push_back(nxt);
          }
        } else {
          if (node.doors < k) {
            if (node.steps == k || i == 1) {
              nxt.doors = 0;
              nxt.steps = 1;
              Q[i + 1].push_back(nxt);
            } else {
              nxt.doors = node.doors + 1;
              nxt.steps = node.steps + 1;
              Q[i].push_back(nxt);
            }
          } else {
            nxt.doors = 1;
            nxt.steps = 1;
            Q[i + 1].push_back(nxt);
          }
        }
      }
    }
  return 0;
}
