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 <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
	T i=0;char c;
	while(!isdigit(c=getchar())&&c!='-');
	bool flag=c=='-';
	flag?c=getchar():0;
	while(i=i*10-'0'+c,isdigit(c=getchar()));
	return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;}
const int N=1010,O=1000000007;
inline int fpow(int x,lint n){
	int a=1;
	for(;n;n>>=1,x=(lint)x*x%O){
		if(n&1){
			a=(lint)a*x%O;
		}
	}
	return a;
}
struct Mat{
	const static int N=3;
	int a[N][N];
	inline friend Mat operator * (const Mat &a,const Mat &b){
		Mat c;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				lint tmp=0;
				for(int k=0;k<N;k++){
					tmp+=(lint)a.a[i][k]*b.a[k][j]%O;
				}
				c.a[i][j]=tmp%O;
			}
		}
		return c;
	}
}trans,res=(Mat){1,0,0,0,1,0,0,0,1};
inline void matfpow(lint n){
	if(n==0){
		res=(Mat){1,0,0,0,1,0,0,0,1};
		return;
	}
	matfpow(n>>1);
	res=res*res;
	if(n&1){
		res=res*trans;
	}
}
char mat[N][N];
inline int Main(){
	int n=ni,m=ni;
	lint tot=next_num<lint>();
	if(tot<=1)return 1;
	for(int i=1;i<=n;i++){
		scanf("%s",mat[i]+1);
	}
	int hor=0,ver=0;
	for(int i=1;i<=n;i++){
		hor+=mat[i][1]=='#'&&mat[i][m]=='#';
	}
	for(int j=1;j<=m;j++){
		ver+=mat[1][j]=='#'&&mat[n][j]=='#';
	}
	if(hor&&ver)return 1;
	if(!(hor||ver)){
		int cnt=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cnt+=mat[i][j]=='#';
			}
		}
		return fpow(cnt,tot-1);
	}
	if(ver){
		for(int i=1,ti=max(n,m);i<=ti;i++){
			for(int j=1;j<i;j++){
				swap(mat[i][j],mat[j][i]);
			}
		}
		swap(hor,ver),swap(n,m);
	}
	memset(&trans,0,sizeof(trans));
	for(int i=1;i<=n;i++){
		bool all=true;
		for(int j=1;j<=m;j++){
			if(mat[i][j]=='#'){
				trans.a[2][1]++;
				trans.a[2][2]++;
				if(mat[i][j-1]!='#'){
					trans.a[2][0]++;
					trans.a[2][1]++;
				}
			}else{
				all=false;
			}
		}
		if(all){
			trans.a[2][0]--;
			trans.a[0][0]++;
		}else if(mat[i][1]=='#'&&mat[i][m]=='#'){
			trans.a[2][0]-=2;
			trans.a[1][0]++;
		}
		if(mat[i][1]=='#'&&mat[i][m]=='#'){
			trans.a[2][1]-=2;
			trans.a[1][1]++;
		}
	}
	matfpow(tot-2);
	int vec[Mat::N]={0,0,0};
	for(int i=1;i<=n;i++){
		bool all=true;
		int cnt=0;
		for(int j=1;j<=m;j++){
			if(mat[i][j]=='#'){
				if(mat[i][j-1]!='#'){
					cnt++;
				}
			}else{
				all=false;
			}
		}
		if(all){
			vec[0]++;
		}else if(mat[i][1]=='#'&&mat[i][m]=='#'){
			vec[1]++;
			vec[2]+=cnt-2;
		}else{
			vec[2]+=cnt;
		}
	}
	lint ans=0;
	for(int i=0;i<Mat::N;i++){
		lint tmp=0;
		for(int j=0;j<Mat::N;j++){
			tmp+=(lint)res.a[i][j]*vec[j]%O;
		}
		ans+=tmp<<(i==1);
	}
	return ans%O;
}
int main(){
	printf("%d\n",Main());
	return 0;
}