#include <bits/stdc++.h>
using namespace std;
const int N = 8e2 + 7;
int n, m, k;
char tbl[N][N];
int dp[N][N][N];
int g[5][2];
deque<pair<int, int> > d[N];
void chk(int x, int y, int l, int f) {
  if (x >= 1 && y >= 1 && x <= n && y <= m && dp[x][y][l] == 0)
    d[l].push_back({x, y}), dp[x][y][l] = f;
}
void bfs(int xs, int ys) {
  int pnt = 0;
  d[0].push_back({xs, ys});
  dp[xs][ys][0] = 1;
  while (pnt + 1 < N) {
    while (d[pnt].size()) {
      int x = d[pnt].front().first, y = d[pnt].front().second;
      d[pnt].pop_front();
      for (int i = 1; i <= 4; i++)
        chk(x + g[i][0], y + g[i][1],
            (tbl[x + g[i][0]][y + g[i][1]] == '#') + pnt, dp[x][y][pnt] + 1);
    }
    pnt++;
  }
}
int res = N * N;
int main() {
  g[1][0] = g[2][1] = 1;
  g[3][0] = g[4][1] = -1;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> tbl[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (tbl[i][j] == 'S') bfs(i, j);
  for (int h = 0; h <= n; h++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        dp[i][j][h]--;
        if ((i == 1 || i == n || j == 1 || j == m) && dp[i][j][h] >= 0) {
          res = min(res, (h + k - 1) / k + (dp[i][j][h] + k - 1) / k);
        }
      }
    }
  }
  cout << res;
  return 0;
}
