#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, ans = 123456789, ansx, ansy;
char a[805][805];
int d[805][805];
int used[805][805];
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int Min(int a, int b, int c, int d) { return min(a, min(b, min(c, d))); }
void pre() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'S') x = i, y = j;
      d[i][j] = Min(i - 1, j - 1, n - i, n - j);
    }
}
void dfs(int x, int y, int time, int minn, int px, int py) {
  if (time == k) {
    if (minn < ans) ans = minn, ansx = x, ansy = y;
    return;
  }
  int i, tx, ty, flag = 0;
  for (i = 0; i < 4; i++) {
    tx = x + dir[i][0];
    ty = y + dir[i][1];
    if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] == '.' &&
        used[tx][ty] == 0) {
      flag = 1;
      used[tx][ty] = 1;
      if (d[tx][ty] < minn)
        dfs(tx, ty, time + 1, d[tx][ty], tx, ty);
      else
        dfs(tx, ty, time + 1, minn, px, py);
      used[tx][ty] = 0;
    }
  }
  if (!flag) dfs(x, y, k, minn, px, py);
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  pre();
  used[x][y] = 1;
  ans = d[x][y], ansx = x, ansy = y;
  dfs(x, y, 0, d[x][y], x, y);
  if (ansx == n || ansx == 1 || ansy == 1 || ansy == n)
    cout << 1 << endl;
  else {
    if (ans % k == 0)
      cout << ans / k + 1 << endl;
    else
      cout << ans / k + 2 << endl;
  }
}
