Language: C++

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = a, i##end = b; i < i##end; ++ i)
#define Rep(i, a, b) for (int i = a, i##end = b; i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = b; i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-10, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
/*------------------------------header------------------------------*/

const int MAXN = 1000 + 5, MOD = 1000000007;

IL int add(int a, int b) {
  a += b;
  return a >= MOD ? a - MOD : a;
}

IL int mul(int a, int b) {
  return (LL)a * b % MOD;
}

IL int quickPow(int a, LL p) {
  int result = 1;
  for (int base = a; p; p >>= 1, base = mul(base, base)) {
    if (p & 1) {
      result = mul(result, base);
    }
  }
  return result;
}

template<int N>
struct Matrix {
  int det[N][N];
  
  Matrix() {
    memset(det, 0, sizeof det);
  }
  
  int *operator[](int x) {
    return det[x];
  }
  
  const int *operator[](int x) const {
    return det[x];
  }
  
  friend Matrix &operator*=(Matrix &a, const Matrix &b) {
    static Matrix<N> x;
    FOR(i, 0, N) {
      FOR(j, 0, N) {
        x[i][j] = 0;
        FOR(k, 0, N) {
          x[i][j] = add(x[i][j], mul(a[i][k], b[k][j]));
        }
      }
    }
    return a = x;
  }
};

IL int solve(int x, int y, int z, LL p) {
  Matrix<2> result, base;
  result[0][0] = result[1][1] = 1;
  base[0][0] = x, base[0][1] = y, base[1][1] = z;
  for (; p; p >>= 1, base *= base) {
    if (p & 1) {
      result *= base;
    }
  }
  return add(result[0][0], result[0][1]);
}

char str[MAXN][MAXN];

int main() {
  int n, m;
  LL k;
  scanf("%d%d%lld", &n, &m, &k);
  if (k <= 1) {
    puts("1");
    exit(0);
  }
  int num = 0;
  For(i, 1, n) {
    scanf("%s", str[i] + 1);
    For(j, 1, m) {
      num += str[i][j] == '#';
    }
  }
  int adj_r = 0, adj_c = 0, n_r = 0, n_c = 0;
  For(i, 1, n) {
    FOR(j, 1, m) {
      if (str[i][j] == '#' && str[i][j + 1] == '#') {
        ++ adj_r;
      }
    }
    if (str[i][1] == '#' && str[i][m] == '#') {
      ++ n_r;
    }
    row |= ok;
  }
  For(i, 1, m) {
    FOR(j, 1, n) {
      if (str[j][i] == '#' && str[j + 1][i] == '#') {
        ++ adj_c;
      }
    }
    if (str[1][i] == '#' && str[n][i] == '#') {
      ++ n_c;
    }
  }
  if (n_r && n_c) {
    puts("1");
  } else if (!n_r && !n_c) {
    printf("%d\n", quickPow(num, k - 1));
  } else if (n_r) {
    printf("%d\n", solve(num, MOD - adj_r, n_r, k - 1));
  } else {
    printf("%d\n", solve(num, MOD - adj_c, n_c, k - 1));
  }
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:134:5: error: 'row' was not declared in this scope; did you mean 'pow'?
  134 |     row |= ok;
      |     ^~~
      |     pow
a.cc:134:12: error: 'ok' was not declared in this scope; did you mean 'k'?
  134 |     row |= ok;
      |            ^~
      |            k