#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| min!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| max!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let h: usize = sc.cin();
    let w: usize = sc.cin();
    let k: usize = sc.cin();
    let mut s = (0, 0);
    let mut fs = vec![vec![false; w]; h];
    for i in 0..h {
        let cs: Vec<char> = sc.cin::<String>().chars().collect();
        for j in 0..w {
            fs[i][j] = cs[j] != '#';
            if cs[j] == 'S' {
                s = (i, j);
            }
        }
    }

    let mut memo = vec![vec![k * 2; w]; h];
    memo[s.0][s.1] = 0;

    use std::collections::BinaryHeap;
    let mut q = BinaryHeap::new();
    q.push((0, s));

    while let Some((_, (i, j))) = q.pop() {
        if memo[i][j] == k { continue }
        if i > 0 && fs[i-1][j] && memo[i-1][j] > memo[i][j] + 1 {
            memo[i-1][j] = memo[i][j] + 1;
            q.push((- (memo[i][j] as i64), (i-1, j)));
        }
        if j > 0 && fs[i][j-1] && memo[i][j-1] > memo[i][j] + 1 {
            memo[i][j-1] = memo[i][j] + 1;
            q.push((- (memo[i][j] as i64), (i, j-1)));
        }
        if i < h - 1 && fs[i+1][j] && memo[i+1][j] > memo[i][j] + 1 {
            memo[i+1][j] = memo[i][j] + 1;
            q.push((- (memo[i][j] as i64), (i+1, j)));
        }
        if j < w - 1 && fs[i][j+1] && memo[i][j+1] > memo[i][j] + 1 {
            memo[i][j+1] = memo[i][j] + 1;
            q.push((- (memo[i][j] as i64), (i, j+1)));
        }
    }

    let mut ans = 1000 + h * w;
    for i in 0..h {
        // trace!(&memo[i]);
        for j in 0..w {
            if memo[i][j] <= k {
                ans = min!(ans, 1 + (i + k - 1) / k);
                ans = min!(ans, 1 + (j + k - 1) / k);
                ans = min!(ans, 1 + (h - i - 1 + k - 1) / k);
                ans = min!(ans, 1 + (w - j - 1 + k - 1) / k);
            }
        }
    }
    put!(ans);
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}


Measured memory_bytes: 10492