1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <set>

using namespace std;

const int N = 3002;
const int MOD = 1000000007;

char board[N][N];

int n;
char getField(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= n) {
        return '#';
    }

    return board[x][y];
}

bool isFree(int x, int y) {
    return getField(x, y) == '.';
}

long long solve(int k, set<pair<int, int>>& possibleMoves) {
    long long result = 0;

    if (k == 1) {
        result = (long long) possibleMoves.size();
    } else {
        set<pair<int, int>> nextPossibleMove = possibleMoves;
        int index = 0;

        for (auto& move : possibleMoves) {
            nextPossibleMove.erase(move);
            board[move.first][move.second] = '#';

            int i = move.first;
            int j = move.second;

            if (isFree(i + 1, j)) {
                nextPossibleMove.insert(pair<int, int>(i + 1, j));
            }

            if (isFree(i - 1, j)) {
                nextPossibleMove.insert(pair<int, int>(i - 1, j));
            }

            if (isFree(i, j + 1)) {
                nextPossibleMove.insert(pair<int, int>(i, j + 1));
            }

            if (isFree(i, j - 1)) {
                nextPossibleMove.insert(pair<int, int>(i, j - 1));
            }



            result += solve(k - 1, nextPossibleMove) - index;
            result %= MOD;



            if (isFree(i + 1, j)) {
                nextPossibleMove.erase(pair<int, int>(i + 1, j));
            }

            if (isFree(i - 1, j)) {
                nextPossibleMove.erase(pair<int, int>(i - 1, j));
            }

            if (isFree(i, j + 1)) {
                nextPossibleMove.erase(pair<int, int>(i, j + 1));
            }

            if (isFree(i, j - 1)) {
                nextPossibleMove.erase(pair<int, int>(i, j - 1));
            }

            board[move.first][move.second] = '.';
            nextPossibleMove.insert(move);
            index++;
        }
    }

    return result % MOD;
}

int main() {
    ios_base::sync_with_stdio(0);

    int k;
    cin >> n >> k;

    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < n; j++) {
            cin >> board[j][i];
        }
    }

    set<pair<int, int>> possibleMoves;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (board[i][j] == '#') {
                if (isFree(i + 1, j)) {
                    possibleMoves.insert(pair<int, int>(i + 1, j));
                }

                if (isFree(i - 1, j)) {
                    possibleMoves.insert(pair<int, int>(i - 1, j));
                }

                if (isFree(i, j + 1)) {
                    possibleMoves.insert(pair<int, int>(i, j + 1));
                }

                if (isFree(i, j - 1)) {
                    possibleMoves.insert(pair<int, int>(i, j - 1));
                }
            }
        }
    }

    cout << solve(k, possibleMoves);

    return 0;
}