#include <bits/stdc++.h>
using namespace std;
struct Node {
  int id, step, x, y;
};
int h, w, k;
char r[810][810];
int s, t;
bool vis[810][810];
int dfn[810][810];
int ans;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void Bfs() {
  queue<Node> q;
  q.push((Node){1, 0, s, t});
  while (!q.empty()) {
    Node t = q.front();
    q.pop();
    vis[t.x][t.y] = true;
    dfn[t.x][t.y] = t.id;
    for (int i = 0; i < 4; ++i) {
      int xx = t.x + dx[i], yy = t.y + dy[i];
      if (vis[xx][yy] || xx < 1 || xx > h || yy < 1 || yy > w) continue;
      if (r[xx][yy] == '.') {
        if (t.step == k)
          q.push((Node){t.id + 1, 1, xx, yy});
        else
          q.push((Node){t.id, t.step + 1, xx, yy});
      } else {
        if (r[t.x][t.y] == '.') {
          q.push((Node){t.id + 1, 1, xx, yy});
        } else {
          if (t.step == k)
            q.push((Node){t.id + 1, 1, xx, yy});
          else
            q.push((Node){t.id, t.step + 1, xx, yy});
        }
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", r[i] + 1);
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (r[i][j] == 'S') {
        s = i;
        t = j;
        break;
      }
    }
  }
  r[s][t] = '.';
  Bfs();
  ans = 0x3f3f3f3f;
  for (int i = 1; i <= w; ++i) {
    ans = min(ans, min(dfn[1][i], dfn[h][i]));
  }
  for (int i = 2; i < h; ++i) {
    ans = min(ans, min(dfn[i][1], dfn[i][w]));
  }
  printf("%d\n", ans);
  return 0;
}
