use std::io::{BufRead, Write};
use std::io;
use std::collections::VecDeque;

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut s = String::new();
    input.read_line(&mut s).unwrap();
    let mut split = s.split_whitespace();
    let h = split.next().unwrap().parse::<usize>().unwrap();
    let w = split.next().unwrap().parse::<usize>().unwrap();
    let k = split.next().unwrap().parse::<usize>().unwrap();
    let mut min = h + w;
    let mut start = (0, 0);
    let ab = (0..h)
        .map(|x| {
            let mut s = String::new();
            input.read_line(&mut s).unwrap();
            s.chars()
                .enumerate()
                .map(|(y, c)| {
                         if c == 'S' {
                             start = (x, y);
                         }
                         c != '#'
                     })
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();
    let mut bfs = VecDeque::<((usize, usize), usize)>::new();
    bfs.push_back((start, 0));
    let mut visited = vec![vec![false;w];h];
    while !bfs.is_empty() {
        let ((x, y), depth) = bfs.pop_front().unwrap();
        if depth == k {
            break;
        }
        min = *([x, y, h - 1 - x, w - 1 - y, min].iter().min().unwrap());
        let mut action = |x: usize, y: usize| if ab[x][y] && !visited[x][y] {
            visited[x][y] = true;
            bfs.push_back(((x, y), depth + 1));
        };
        if x != 0 {
            action(x - 1, y);
        }
        if x != w - 1 {
            action(x + 1, y);
        }
        if y != 0 {
            action(x, y - 1);
        }
        if y != h - 1 {
            action(x, y + 1);
        }

    }
    writeln!(output, "{}", 1 + (min + k - 1) / k).unwrap();
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v = Vec::<u8>::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }
    #[test]
    fn case1() {
        test("3 3 3
#.#
#S.
###\n",
             "1\n");
    }
    #[test]
    fn case2() {
        test("3 3 3
###
#S#
###\n",
             "2\n");
    }
    #[test]
    fn case3() {
        test("7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######\n",
             "2\n");
    }
}

