macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        h: usize,
        w: usize,
        k: usize,
        v: [chars; h]
    }
    let m = 1000000007;
    let b = v.iter().fold(0, |ac, l| ac + l.iter().filter(|&&x| x == '#').count());
    let mut cv = 0;
    for i in 0..w {
        if v[0][i] == '#' && v[h - 1][i] == '#' {
            cv += 1;
        }
    }
    let mut ch = 0;
    for i in 0..h {
        if v[i][0] == '#' && v[i][w - 1] == '#' {
            ch += 1;
        }
    }
    if cv + ch == 0 || h == 1 && ch == 0 || w == 1 && cv == 0 {
        println!("{}", pow_m(b, k - 1, m));
        return;
    } else if cv > 0 && ch > 0 {
        println!("1");
        return;
    }
    let c;
    let mut d = 0;
    if cv == 0 {
        c = ch;
        for i in 0..h {
            for j in 0..w - 1 {
                if v[i][j] == '#' && v[i][j + 1] == '#' {
                    d += 1;
                }
            }
        }
    } else {
        c = cv;
        for i in 0..h - 1 {
            for j in 0..w {
                if v[i][j] == '#' && v[i + 1][j] == '#' {
                    d += 1;
                }
            }
        }
    }
    println!("{}", (d * pow_m(c, k - 1, m) % m + pow_m(b, k - 1, m) * (b - c - d) % m) * inv_m(b - c, m) % m);
}

fn pow_m(n: usize, mut p: usize, d: usize) -> usize {
    let mut r = n;
    let mut ret = 1;
    while p > 0 {
        if p % 2 == 0 {
            r = r * r % d;
            p /= 2;
        } else {
            ret = ret * r % d;
            p -= 1;
        }
    }
    ret
}

fn inv_m(n: usize, d: usize) -> usize {
    pow_m(n, d - 2, d)
}


Measured memory_bytes: 8572