#include <bits/stdc++.h>
using namespace std;
unsigned long long min4(unsigned long long a, unsigned long long b,
                        unsigned long long c, unsigned long long d) {
  if (a < b && a < c && a < d) {
    return a;
  }
  if (b < c && b < d) {
    return b;
  }
  if (c < d) {
    return c;
  }
  return d;
}
int main(void) {
  unsigned long long H, W, K;
  cin >> H >> W >> K;
  unsigned long long a[H][W];
  unsigned long long start_i, start_j;
  for (unsigned long long i = 0; i < H; ++i) {
    string s;
    cin >> s;
    for (unsigned long long j = 0; j < W; ++j) {
      if (s[j] == '#') {
        a[i][j] = 0;
      } else if (s[j] == 'S') {
        start_i = i;
        start_j = j;
        a[i][j] = 2;
      } else {
        a[i][j] = 1;
      }
    }
  }
  for (unsigned long long k = 0; k < K; ++k) {
    for (unsigned long long i = 0; i < H; ++i) {
      for (unsigned long long j = 0; j < W; ++j) {
        bool flg = false;
        if (a[i][j] == 1) {
          if (i > 0 && a[i - 1][j] == 2) {
            a[i][j] = 2;
            flg = true;
          }
          if (i < H - 1 && a[i + 1][j] == 2) {
            a[i][j] = 2;
            flg = true;
          }
          if (j > 0 && a[i][j - 1] == 2) {
            a[i][j] = 2;
            flg = true;
          }
          if (j < W - 1 && a[i][j + 1] == 2) {
            a[i][j] = 2;
            flg = true;
          }
          if (flg) {
            if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
              cout << 1 << endl;
              return 0;
            }
          }
        }
      }
    }
  }
  unsigned long long magic = 0;
  for (unsigned long long i = 0; i < H; ++i) {
    for (unsigned long long j = 0; j < W; ++j) {
      if (a[i][j] == 2) {
        unsigned long long p = min4(i, j, H - 1 - i, W - 1 - j);
        unsigned long long magic_now = (p - 1) / K + 2;
        if (magic > magic_now || magic == 0) {
          magic = magic_now;
        }
      }
    }
  }
  cout << magic << endl;
  return 0;
}
