#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1000000000")
using ll = long long;
using ld = long double;
using namespace std;
const int MAXN = 805;
const int INF = MAXN * MAXN;
bool g[MAXN][MAXN];
int mx[] = {0, 1, 0, -1};
int my[] = {1, 0, -1, 0};
pair<int, int> q[MAXN * MAXN];
int qe = 0;
int d[MAXN][MAXN];
void setd(int x, int y, int dd) {
  d[x][y] = dd;
  q[qe++] = make_pair(x, y);
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MAXN; j++) d[i][j] = INF;
  int n, m, k;
  cin >> n >> m >> k;
  memset(g, 0, sizeof(g));
  for (int i = 0; i < MAXN; i++)
    g[i][0] = g[0][i] = g[n + 1][i] = g[i][n + 1] = true;
  int sx, sy;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '#') g[i][j + 1] = true;
      if (s[j] == 'S') sx = i, sy = j + 1;
    }
  }
  setd(sx, sy, 0);
  for (int qb = 0; qb < qe; qb++) {
    int x = q[qb].first, y = q[qb].second;
    for (int j = 0; j < 4; j++) {
      int nx = x + mx[j], ny = y + my[j];
      if ((!g[nx][ny]) && (d[nx][ny] == INF)) setd(nx, ny, d[x][y] + 1);
    }
  }
  int ans = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (d[i][j] <= k) {
        ans = min(ans, i - 1);
        ans = min(ans, j - 1);
        ans = min(ans, n - i);
        ans = min(ans, n - j);
      }
    }
  }
  ans = (ans + k - 1) / k;
  cout << ans + 1 << endl;
  return 0;
}
