#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<char>> a(h, vector<char>(w));
  vector<vector<int>> r(h, vector<int>(w, -1));
  vector<pair<int, int>> p, pex;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        pex.push_back(make_pair(i, j));
        r[i][j] = 0;
      }
      if (a[i][j] == '#') {
        r[i][j] = -2;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < pex.size(); j++) {
      pair<int, int> temp = pex[j];
      int x = temp.first;
      int y = temp.second;
      if (temp.first != 0) {
        if (r[x - 1][y] == -1) {
          r[x - 1][y] = 0;
          p.push_back(make_pair(x - 1, y));
        }
      }
      if (temp.first != h - 1) {
        if (r[x + 1][y] == -1) {
          r[x + 1][y] = 0;
          p.push_back(make_pair(x + 1, y));
        }
      }
      if (y != 0) {
        if (r[x][y - 1] == -1) {
          r[x][y - 1] = 0;
          p.push_back(make_pair(x, y - 1));
        }
      }
      if (y != w - 1) {
        if (r[x][y + 1] == -1) {
          r[x][y + 1] = 0;
          p.push_back(make_pair(x, y + 1));
        }
      }
    }
    pex = p;
  }
  int mind = 800;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (r[i][j] == 0) {
        int xp = h - 1 - i;
        int xm = i;
        int yp = w - 1 - j;
        int ym = j;
        int mint = xp;
        if (mint > xm) mint = xm;
        if (mint > yp) mint = yp;
        if (mint > ym) mint = ym;
        if (mind > mint) mind = mint;
      }
    }
  }
  if (mind) {
    cout << 1 + (mind - 1) / k + 1 << endl;
  } else {
    cout << 1 << endl;
  }
  return 0;
}
