#include <bits/stdc++.h>
using namespace std;
const int maxn = 2333;
int dirx[] = {0, 0, 1, -1};
int diry[] = {1, -1, 0, 0};
int h, w, k;
char xxx[maxn][maxn];
bool vis[maxn][maxn];
int anss[maxn];
inline int get(int x, int y) { return min(min(x, h - x), min(y, w - y)); }
int main() {
  scanf("%d%d%d", &h, &w, &k);
  queue<pair<int, int> > q;
  pair<int, int> S;
  int ans = 2333333;
  for (int i = 1; i <= h; ++i) {
    scanf("%s", xxx[i] + 1);
    for (int j = 1; j <= w; ++j) {
      if (xxx[i][j] == 'S') {
        q.push(S = make_pair(i, j));
        vis[i][j] = true;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    int too = get(now.first, now.second);
    int go = now.first - S.first + now.second - S.second;
    int tmpans = ceil((double)go / k);
    int oth = ceil((double)too / k);
    ans = min(oth, tmpans);
    cout << now.first << ' ' << now.second << ' ' << tmpans << ' ' << oth
         << endl;
    for (int i = 0; i < 4; ++i) {
      int xx = now.first + dirx[i];
      int yy = now.second + diry[i];
      if (xx && yy && xx <= h && yy <= w && !vis[xx][yy] &&
          xxx[xx][yy] == '.') {
        vis[xx][yy] = true;
        q.push(make_pair(xx, yy));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
