#include <bits/stdc++.h>
using namespace std;
int xcomp(vector<long long int>& a, vector<long long int>& b) {
  return a[0] < b[0];
}
long long int mod = 1000000007;
long long int H, W, K;
vector<vector<bool>> isRode(800, vector<bool>(800, false));
vector<vector<bool>> canMove(800, vector<bool>(800, false));
long long int m = 1000000;
long long int sx, sy;
void tansaku(long long int x, long long int y, long long int k) {
  k--;
  canMove[x][y] = true;
  if (x < m) {
    m = x;
  }
  if (y < m) {
    m = y;
  }
  if (W - x - 1 < m) {
    m = W - x - 1;
  }
  if (H - y - 1 < m) {
    m = H - y - 1;
  }
  if (k >= 1) {
    long long int i, j;
    i = x + 1;
    j = y;
    if (0 <= i && i < W && 0 <= j && j < H) {
      if (isRode[i][j] && !canMove[i][j]) {
        tansaku(i, j, k);
      }
    }
    i = x - 1;
    j = y;
    if (0 <= i && i < W && 0 <= j && j < H) {
      if (isRode[i][j] && !canMove[i][j]) {
        tansaku(i, j, k);
      }
    }
    i = x;
    j = y + 1;
    if (0 <= i && i < W && 0 <= j && j < H) {
      if (isRode[i][j] && !canMove[i][j]) {
        tansaku(i, j, k);
      }
    }
    i = x;
    j = y - 1;
    if (0 <= i && i < W && 0 <= j && j < H) {
      if (isRode[i][j] && !canMove[i][j]) {
        tansaku(i, j, k);
      }
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (long long int i = (0); i < (H); ++i) {
    string s;
    cin >> s;
    for (long long int j = (0); j < (W); ++j) {
      if (s[j] == '.') {
        isRode[i][j] = true;
      }
      if (s[j] == 'S') {
        isRode[i][j] = true;
        sx = i;
        sy = j;
      }
    }
  }
  tansaku(sx, sy, K);
  cout << (m + K - 1) / K + 1 << endl;
}
