#include <bits/stdc++.h>
using namespace std;
int h, w, k;
string s[810];
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int sx, sy;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  int to1 = sx, to2 = sy, to3 = h - sx - 1, to4 = w - sy - 1,
      mto = min(to1, min(to2, min(to3, to4)));
  string vv[4];
  if (mto == to1) {
    int cx = sx, cy = sy;
    while (cx >= 0 && cy >= 0 && cx < h && cy < w) {
      vv[0].push_back(s[cx][cy]);
      cx--;
    }
  }
  if (mto == to2) {
    int cx = sx, cy = sy;
    while (cx >= 0 && cy >= 0 && cx < h && cy < w) {
      vv[1].push_back(s[cx][cy]);
      cy--;
    }
  }
  if (mto == to3) {
    int cx = sx, cy = sy;
    while (cx >= 0 && cy >= 0 && cx < h && cy < w) {
      vv[2].push_back(s[cx][cy]);
      cx++;
    }
  }
  if (mto == to4) {
    int cx = sx, cy = sy;
    while (cx >= 0 && cy >= 0 && cx < h && cy < w) {
      vv[3].push_back(s[cx][cy]);
      cy++;
    }
  }
  int minn = 1e9;
  string v;
  for (int i = 0; i < 4; i++) {
    int now = 0;
    for (int j = 0; j < vv[i].size(); j++) {
      if (vv[i][j] == '#') now++;
    }
    if (now < minn) {
      minn = now;
      v = vv[i];
    }
  }
  int ans = 0;
  if (v[0] == 'S') v[0] = '.';
  int cur = 0, cnt = 0;
  bool st = 0;
  while (cur != v.size() - 1) {
    for (int i = 0; i < k; i++) {
      if (cur == v.size() - 1 || v[cur + 1] == '#') break;
      cur++;
    }
    for (int i = cur; i < v.size(); i++) {
      int cc = 0;
      if (v[i] == '#') {
        v[i] = '.';
        cc++;
      }
      if (cc == k) {
        break;
      }
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
