#include <bits/stdc++.h>
using namespace std;
const int N = 8e2 + 7;
short n, m, k;
short dp[N][N][N / 2];
char tbl[N][N];
queue<pair<short, short> > q[N / 2];
short g[6][2];
void solve(int xs, int ys) {
  q[0].push({xs, ys});
  dp[xs][ys][0] = 1;
  short x, y, xp, yp;
  for (short t = 0; t + 1 < N / 2; t++) {
    while (q[t].size()) {
      x = q[t].front().first;
      y = q[t].front().second;
      q[t].pop();
      for (short i = 0; i <= 4; i++) {
        xp = x + g[i][0];
        yp = y + g[i][1];
        bool bl = (tbl[xp][yp] == '#') || (xp == x && yp == y);
        if (xp > 0 && yp > 0 && xp <= n && yp <= m && !dp[xp][yp][t + bl])
          q[t + bl].push({xp, yp}),
              dp[xp][yp][t + bl] = dp[x][y][t] + 1 - (xp == x && yp == y);
      }
    }
  }
}
int main() {
  g[0][1] = g[1][0] = 1;
  g[2][1] = g[3][0] = -1;
  cin >> n >> m >> k;
  int xs, ys;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> tbl[i][j];
      if (tbl[i][j] == 'S') xs = i, ys = j;
    }
  solve(xs, ys);
  int res = N;
  for (int t = 0; t < N / 2; t++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j += m - 1) {
        if (dp[i][j][t] > 0) {
          res = min(res, (t + k - 1) / k + (dp[i][j][t] - 1 + k - 1) / k);
        }
      }
    }
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i += n - 1) {
        if (dp[i][j][t] > 0) {
          res = min(res, (t + k - 1) / k + (dp[i][j][t] - 1 + k - 1) / k);
        }
      }
    }
  }
  cout << res;
  return 0;
}
