#include <bits/stdc++.h>
using namespace std;
char s[809][809];
int n, m, k, ans, xx, yy;
int dp[809][809];
struct ee {
  int x, y, id, v;
  bool operator<(const ee &a) const { return id > a.id; }
};
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= m; i++) scanf("%s", s[i] + 1);
  memset(dp, 0x3f3f3f3f, sizeof(dp));
  ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') {
        xx = i, yy = j;
        break;
      }
    }
  }
  priority_queue<ee> q;
  ee r;
  r.x = xx;
  r.y = yy;
  r.v = 0;
  r.id = 0;
  dp[xx][yy] = 0;
  q.push(r);
  int cnt = 0;
  while (!q.empty()) {
    ee t = q.top();
    cnt = t.id;
    cnt++;
    q.pop();
    ee tt = t;
    tt.v = 0;
    queue<ee> p, pp;
    p.push(tt);
    pp.push(tt);
    while (!p.empty()) {
      ee u = p.front();
      p.pop();
      if (u.v == k) continue;
      ee uu = u;
      uu.x = u.x - 1;
      uu.v++, uu.id = cnt;
      if (s[u.x - 1][u.y] == '.' && dp[u.x - 1][u.y] > cnt) {
        dp[u.x - 1][u.y] = cnt;
        if (u.x - 1 > 1) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.y = u.y - 1;
      uu.v++, uu.id = cnt;
      if (s[u.x][u.y - 1] == '.' && dp[u.x][u.y - 1] > cnt) {
        dp[u.x][u.y - 1] = cnt;
        if (u.y - 1 > 1) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.y = u.y + 1;
      uu.v++, uu.id = cnt;
      if (s[u.x][u.y + 1] == '.' && dp[u.x][u.y + 1] > cnt) {
        dp[u.x][u.y + 1] = cnt;
        if (u.y + 1 < m) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.x = u.x + 1;
      uu.v++, uu.id = cnt;
      if (s[u.x + 1][u.y] == '.' && dp[u.x + 1][u.y] > cnt) {
        dp[u.x + 1][u.y] = cnt;
        if (u.x + 1 < n) q.push(uu), p.push(uu);
      }
    }
    while (!pp.empty()) {
      ee u = pp.front();
      pp.pop();
      if (u.v == k) continue;
      ee uu = u;
      uu.x = u.x - 1;
      uu.v++, uu.id = cnt + 1;
      if (s[u.x - 1][u.y] == '#' && dp[u.x - 1][u.y] > cnt + 1) {
        dp[u.x - 1][u.y] = cnt + 1;
        if (u.x - 1 > 1) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.y = u.y - 1;
      uu.v++, uu.id = cnt + 1;
      if (s[u.x][u.y - 1] == '#' && dp[u.x][u.y - 1] > cnt + 1) {
        dp[u.x][u.y - 1] = cnt + 1;
        if (u.y - 1 > 1) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.y = u.y + 1;
      uu.v++, uu.id = cnt + 1;
      if (s[u.x][u.y + 1] == '#' && dp[u.x][u.y + 1] > cnt + 1) {
        dp[u.x][u.y + 1] = cnt + 1;
        if (u.y + 1 < m) q.push(uu), p.push(uu);
      }
      uu = u;
      uu.x = u.x + 1;
      uu.v++, uu.id = cnt + 1;
      if (s[u.x + 1][u.y] == '#' && dp[u.x + 1][u.y] > cnt + 1) {
        dp[u.x + 1][u.y] = cnt + 1;
        if (u.x + 1 < n) q.push(uu), p.push(uu);
      }
    }
  }
  for (int i = 1; i <= m; i++) ans = min(ans, dp[1][i]);
  for (int i = 1; i <= m; i++) ans = min(ans, dp[n][i]);
  for (int i = 1; i <= n; i++) ans = min(ans, dp[i][1]);
  for (int i = 1; i <= n; i++) ans = min(ans, dp[i][m]);
  printf("%d\n", ans);
  return 0;
}
