#include <bits/stdc++.h>
using namespace std;
char ch[900][900], ch2[900][900];
int n, m, k, sum;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int Bfs(int x, int y) {
  bool ok = false;
  while (x != 0 && x != n - 1 && y != 0 && y != m - 1) {
    sum++;
    queue<pair<int, int> > qu;
    queue<int> quv;
    qu.push(make_pair(x, y));
    quv.push(0);
    int dir = 0, minn = 100000;
    if (n - 1 - x < minn) {
      minn = n - 1 - x;
      dir = 0;
    }
    if (m - 1 - y < minn) {
      minn = m - 1 - y;
      dir = 1;
    }
    if (y < minn) {
      minn = y;
      dir = 2;
    }
    if (x < minn) {
      minn = x;
      dir = 3;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) ch2[i][j] = ch[i][j];
    ch2[x][y] = '0';
    while (!qu.empty()) {
      int nowx = qu.front().first;
      int nowy = qu.front().second;
      qu.pop();
      int nowv = quv.front();
      quv.pop();
      for (int i = 0; i < 4; i++) {
        if (nowv >= k) break;
        int fx = nowx + dx[i];
        int fy = nowy + dy[i];
        if ((fx == 0 || fx == n - 1 || fy == 0 || fy == m - 1) &&
            ch2[fx][fy] == '.') {
          ok = true;
          break;
        }
        if (fx >= 0 && fx < n && fy >= 0 && fy < m && ch2[fx][fy] == '.') {
          qu.push(make_pair(fx, fy));
          quv.push(nowv + 1);
          ch2[fx][fy] = '0';
          if (n - 1 - fx < minn) {
            minn = n - 1 - fx;
            x = fx;
            y = fy;
            dir = 0;
          }
          if (m - 1 - fy < minn) {
            minn = m - 1 - fy;
            x = fx;
            y = fy;
            dir = 1;
          }
          if (fy < minn) {
            minn = fy;
            x = fx;
            y = fy;
            dir = 2;
          }
          if (fx < minn) {
            minn = fx;
            x = fx;
            y = fy;
            dir = 3;
          }
        }
      }
      if (ok) break;
    }
    int j = 0, px = x + dx[dir], py = y + dy[dir];
    while (j < k && px < n && px >= 0 && py < m && py >= 0 &&
           ch[px][py] == '#') {
      j++;
      ch[px][py] = '.';
      px += dx[dir];
      py += dy[dir];
    }
    if (ok) break;
  }
  return sum;
}
int main() {
  int x = 0, y = 0;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", ch[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      ch2[i][j] = ch[i][j];
      if (ch[i][j] == 'S') {
        ch[i][j] = '.';
        ch2[i][j] = '.';
        x = i;
        y = j;
      }
    }
  printf("%d\n", Bfs(x, y));
  return 0;
}
