Problem:
Score : 1700 points

Problem Statement

Snuke got a grid from his mother, as a birthday present. The grid has H rows and W columns. Each cell is painted black or white. All black cells are 4-connected, that is, it is possible to traverse from any black cell to any other black cell by just visiting black cells, where it is only allowed to move horizontally or vertically.

The color of the cell at the i-th row and j-th column (1 ≦ i ≦ H, 1 ≦ j ≦ W) is represented by a character s_{ij}. If s_{ij} is #, the cell is painted black. If s_{ij} is ., the cell is painted white. At least one cell is painted black.

We will define fractals as follows.
The fractal of level 0 is a 1 × 1 grid with a black cell.
The fractal of level k+1 is obtained by arranging smaller grids in H rows and W columns, following the pattern of the Snuke's grid.
At a position that corresponds to a black cell in the Snuke's grid, a copy of the fractal of level k is placed.
At a position that corresponds to a white cell in the Snuke's grid, a grid whose cells are all white, with the same dimensions as the fractal of level k, is placed.

You are given the description of the Snuke's grid, and an integer K. Find the number of connected components of black cells in the fractal of level K, modulo 10^9+7.

Constraints

1 ≦ H,W ≦ 1000

0 ≦ K ≦ 10^{18}

Each s_{ij} is either # or ..

All black cells in the given grid are 4-connected.

There is at least one black cell in the given grid.

Input

The input is given from Standard Input in the following format:

H W K
s_{11} .. s_{1W}
:
s_{H1} .. s_{HW}

Output

Print the number of connected components of black cells in the fractal of level K, modulo 10^9+7.

Sample Input 1

3 3 3
.#.
###
#.#

Sample Output 1

20

The fractal of level K=3 in this case is shown below. There are 20 connected components of black cells.

.............#.............
............###............
............#.#............
..........#..#..#..........
.........#########.........
.........#.##.##.#.........
..........#.....#..........
.........###...###.........
.........#.#...#.#.........
....#........#........#....
...###......###......###...
...#.#......#.#......#.#...
.#..#..#..#..#..#..#..#..#.
###########################
#.##.##.##.##.##.##.##.##.#
.#.....#..#.....#..#.....#.
###...######...######...###
#.#...#.##.#...#.##.#...#.#
....#.................#....
...###...............###...
...#.#...............#.#...
.#..#..#...........#..#..#.
#########.........#########
#.##.##.#.........#.##.##.#
.#.....#...........#.....#.
###...###.........###...###
#.#...#.#.........#.#...#.#

Sample Input 2

3 3 3
###
#.#
###

Sample Output 2

1

Sample Input 3

11 15 1000000000000000000
.....#.........
....###........
....####.......
...######......
...#######.....
..##.###.##....
..##########...
.###.....####..
.####...######.
###############
#.##..##..##..#

Sample Output 3

301811921

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long,long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k)  for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__)

ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}

const int maxn=1010;
const int mod=1e9+7;
int n,m;ll k;
char c[maxn][maxn];
char tc[maxn][maxn];

struct Matrix{
	int a[3][3];
	Matrix(){memset(a,0,sizeof(a));}
	void init_one(){a[1][1]=a[2][2]=1;}
	Matrix operator * (const Matrix &b) const{
		Matrix ret;
		rep(i,1,2) rep(j,1,2) rep(k,1,2){
			ret.a[i][j]+=(a[i][k]*1ll*b.a[k][j])%mod;
			if(ret.a[i][j]>=mod) ret.a[i][j]-=mod;
		}
		return ret;
	}
	void pr(){
		cout<<a[1][1]<<' '<<a[1][2]<<endl<<a[2][1]<<' '<<a[2][2]<<endl<<endl;
	}
} a;

Matrix ksm(Matrix a,ll p){
	Matrix ret;ret.init_one();
	while(p){
		if(p&1) ret=ret*a;
		p>>=1;
		a=a*a;
	}
	return ret;
}

int ksm(int x,ll p){
	int ret=1;
	while(p){
		if(p&1) ret=ret*1ll*x%mod;
		p>>=1;
		x=x*1ll*x%mod;
	}
	return ret;
}

void work(){
	n=read(),m=read(),k=read();
	int cnt=0;bool f1=0,f2=0;
	rep(i,1,n) rep(j,1,m){
		c[i][j]=getchar();
		while(c[i][j]!='.' && c[i][j]!='#') c[i][j]=getchar();
	}
	rep(i,1,n) if(c[i][1]=='#' && c[i][m]=='#') f1=1;
	rep(j,1,m) if(c[1][j]=='#' && c[n][j]=='#') f2=1;
	if(f2 && !f1){
		rep(i,1,n) rep(j,1,m) tc[j][n+1-i]=c[i][j];
		swap(n,m);
		rep(i,1,n) rep(j,1,m) c[i][j]=tc[i][j];
	}
	rep(i,1,n){
		if(c[i][1]=='#' && c[i][m]=='#') a.a[2][2]++;
		rep(j,1,m){
			if(c[i][j]=='#') a.a[1][1]++;
			if(c[i][j]=='#' && c[i][j-1]=='#') a.a[1][2]++;
		}
	}
	if(!f1 && !f2){
		printf("%d\n",ksm(a.a[1][1],k-1));
		return;
	}
	else if(f1 && f2){
		puts("1");
		return;
	}
	a=ksm(a,k-1);
	printf("%d\n",(a.a[1][1]-a.a[1][2]+mod)%mod);
}

int main(){
	#ifdef LZT
		freopen("in","r",stdin);
	#endif
	
	work();
	
	#ifdef LZT
		Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
	#endif
}