#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101;
int n, m;
queue<pair<int, int> > q;
string mat[MAXN];
int dist[MAXN][MAXN];
int mov[5] = {1, -1, 0, 0};
int mov2[5] = {0, 0, 1, -1};
int bfs(int x, int y, int f) {
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (!x || !y) return dist[x][y];
    for (int i = 0; i < 4; i++) {
      int x1 = x + mov[i];
      int y1 = y + mov2[i];
      if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m &&
          dist[x][y] + 1 < dist[x1][y1]) {
        if (f && mat[x1][y1] == '#') continue;
        dist[x1][y1] = dist[x][y] + 1;
        q.push(pair<int, int>(x1, y1));
      }
    }
  }
  return 1e8;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x, y, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
    for (int j = 0; j < m; j++) {
      dist[i][j] = 1e8;
      if (mat[i][j] == 'S') {
        x = i, y = j;
        dist[i][j] = 0;
        mat[i][j] = '.';
      }
    }
  }
  q.push(pair<int, int>(x, y));
  bfs(x, y, 1);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (dist[i][j] < 1e8) {
        dist[i][j] = 0;
        q.push(pair<int, int>(i, j));
      }
    }
  int ans = bfs(x, y, 0);
  ans = ans / k + (ans % k == 0 ? 0 : 1);
  cout << 1 + ans << '\n';
  return 0;
}
