#include <bits/stdc++.h>
using namespace std;
char str[888][888];
int MIN(int a, int b) { return a < b ? a : b; }
int main() {
  int sx, sy;
  int h, w, k;
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 0; i < h; i++) {
    scanf("%s", str[i]);
    for (int j = 0; j < w; j++) {
      if (str[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  int h1 = 0;
  int fx = sx;
  while (fx != 0) {
    if (str[fx][sy] == '#') {
      h1++;
    }
    h1++;
    fx--;
    if (str[fx][sy] == '#') {
      h1++;
    }
  }
  int h2 = 0;
  fx = sx;
  while (fx != h - 1) {
    if (str[fx][sy] == '#') {
      h2++;
    }
    h2++;
    fx++;
    if (str[fx][sy] == '#') {
      h2++;
    }
  }
  int h3 = 0;
  int fy = sy;
  while (fy != 0) {
    if (str[sx][fy] == '#') {
      h3++;
    }
    h3++;
    fy--;
    if (str[sx][fy] == '#') {
      h3++;
    }
  }
  int h4 = 0;
  fy = sy;
  while (fy != w - 1) {
    if (str[sx][fy] == '#') {
      h4++;
    }
    h4++;
    fy++;
    if (str[sx][fy] == '#') {
      h4++;
    }
  }
  cout << MIN(h1, MIN(h2, MIN(h3, MIN(h4, k)))) << endl;
  return 0;
}
