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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

namespace {

struct Polyomino {
  vector<pair<int, int>> coords;
  int n;
  int mi, mj;

  Polyomino(initializer_list<pair<int, int>> il): coords(il), n(il.size()), mi{0}, mj{0}
  {
    int li = 0;
    int lj = 0;
    for (auto const& ij: coords) {
      auto i = ij.first;
      auto j = ij.second;
      if (mi < i) mi = i;
      if (mj < j) mj = j;
      assert(abs(li - i) <= 1);
      assert(abs(lj - j) <= 1);
      li = i;
      lj = j;
    }
  }

  vector<pair<int, int>>::const_iterator begin() const
  {
    return coords.begin();
  }

  vector<pair<int, int>>::const_iterator end() const
  {
    return coords.end();
  }

  friend ostream& draw(ostream& os, Polyomino const& p)
  {
    vector<string> tab(p.mi + 1, string(p.mj + 1, '.'));
    int q = 0;
    for (auto const& ij: p) {
      tab[ij.first][ij.second] = 'A' + q;
      ++q;
    }
    for (auto const& s: tab) {
      os << s << '\n';
    }
    return os;
  }
};

const vector<Polyomino> polyominoes = {
  {
    // 1:
    {{0, 0}},
    // 2:
    {{0, 0}, {0, 1}},
    {{0, 0}, {1, 0}},
    // 3:
    {{0, 0}, {0, 1}, {0, 2}},
    {{1, 0}, {0, 0}, {0, 1}},
    {{0, 0}, {0, 1}, {1, 1}},
    {{0, 0}, {1, 0}, {1, 1}},
    {{0, 0}, {1, 0}, {2, 0}},
    {{0, 1}, {1, 1}, {1, 0}},
    // 4:
    // {{0, 0}, {0, 1}, {0, 2}, {0, 3}},
    // {{0, 0}, {1, 0}, {2, 0}, {3, 0}},
    // {{0, 0}, {0, 1}, {1, 0}, {1, 1}},
    // {{0, 0}, {0, 1}, {0, 2}, {1, 0}},
    // {{0, 0}, {0, 1}, {0, 2}, {1, 1}},
    // {{0, 0}, {0, 1}, {0, 2}, {1, 2}},
    // {{1, 0}, {1, 1}, {1, 2}, {0, 0}},
    // {{1, 0}, {1, 1}, {1, 2}, {0, 1}},
    // {{1, 0}, {1, 1}, {1, 2}, {0, 2}},
    // {{0, 0}, {1, 0}, {2, 0}, {0, 1}},
    // {{0, 0}, {1, 0}, {2, 0}, {1, 1}},
    // {{0, 0}, {1, 0}, {2, 0}, {2, 1}},
    // {{0, 1}, {1, 1}, {2, 1}, {0, 0}},
    // {{0, 1}, {1, 1}, {2, 1}, {1, 0}},
    // {{0, 1}, {1, 1}, {2, 1}, {2, 0}},
    // {{0, 0}, {1, 0}, {1, 1}, {2, 1}},
    // {{0, 1}, {1, 1}, {1, 0}, {2, 0}},
    // {{0, 0}, {0, 1}, {1, 1}, {1, 2}},
    // {{1, 0}, {1, 1}, {0, 1}, {0, 2}},
  }
};

template<typename T>
constexpr T square(T x)
{
  return x*x;
}

template <typename T>
constexpr T ipow(T base, int power)
{
  return power == 0 ? 1 : (square(ipow(base, power / 2)) * (power % 2 == 0 ? 1 : base));
}

constexpr int mod = ipow(10, 9) + 7;

int reduce(int a)
{
  a %= mod;
  if (a < 0) a += mod;
  return a;
}

int reduce(long long a_)
{
  int a = a_ % mod;
  a %= mod;
  if (a < 0) a += mod;
  return a;
}

int sum()
{
  return 0;
}

template <typename... Args>
int sum(int a, Args... args)
{
  return reduce(a + sum(args...));
}

int mul()
{
  return 1;
}

template <typename... Args>
int mul(int a, Args... args)
{
  return reduce(static_cast<long long>(a) * mul(args...));
}

int half(int x)
{
  return mul(x, (mod + 1) / 2);
}

int choose(int n, int k)
{
  vector<int> v; v.reserve(k);
  for (int i = 0; i < k; ++i) {
    v.emplace_back(n - i);
  }
  for (int i = k; i > 1; --i) {
    for (auto& x: v) {
      if (x % i == 0) {
        x /= i;
        goto ok;
      }
    }
    assert(false);
ok:;
  }
  int res = 1;
  for (auto const& x: v) res = mul(res, x);
  return res;
}


int solve(vector<string> const& data, int k)
{
  int n = data.size();
  vector<vector<bool>> anchored(n, vector<bool>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (data[i][j] != '.') continue;
      if (i > 0 && data[i - 1][j] == '#') anchored[i][j] = true;
      else if (j > 0 && data[i][j - 1] == '#') anchored[i][j] = true;
      else if (i < n - 1 && data[i + 1][j] == '#') anchored[i][j] = true;
      else if (j < n - 1 && data[i][j + 1] == '#') anchored[i][j] = true;
    }
  }

  vector<vector<int>> matches;
  for (auto const& p: polyominoes) {
    matches.emplace_back(1 << p.n);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (auto const& p: polyominoes) {
        if (p.n > k) continue;
        if (i + p.mi >= n) continue;
        if (j + p.mj >= n) continue;
        int anchors = 0;
        int cur = 1;
        for (auto const& ij: p) {
          auto i2 = i + ij.first;
          auto j2 = j + ij.second;
          if (data[i2][j2] != '.') goto bad;
          if (anchored[i2][j2]) anchors |= cur;
          cur <<= 1;
        }
        if (anchors == 0) continue;
        ++matches[&p - polyominoes.data()][anchors];
bad:;
      }
    }
  }

  for (auto const& p: polyominoes) {
    if (p.n > k) continue;
    clog << p.n << '\n';
    draw(clog, p);
    for (int c: matches[&p - polyominoes.data()]) {
      clog << c << ' ';
    }
    clog << endl;
  }

  vector<int> c(k + 1);
  vector<vector<int>> m;
  for (int i = 0; i <= k; ++i) {
    m.emplace_back(1 << i);
  }
  for (int q = 0; q < int(polyominoes.size()); ++q) {
    auto const& p = polyominoes[q];
    if (p.n > k) continue;
    for (int i = 0; i < p.n; ++i) {
      c[p.n] += matches[q][1 << i];
    }
    for (int i = 0; i < (1 << p.n); ++i) {
      m[p.n][i] += matches[q][i];
    }
  }
  for (int i = 1; i <= k; ++i) {
    clog << i << ": " << c[i] << ": ";
    for (int x: m[i]) {
      clog << ' ' << x;
    }
    clog << endl;
  }

  if (k == 1) return c[1];
  if (k == 2) return sum(choose(c[1], 2), c[2]);
  if (k == 3) {
    return sum(
      choose(c[1], 3),
      mul(c[1] - 1, c[2]),
      -m[3][5],
      c[3]
    );
  }

  return 42;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  vector<string> data(n);
  for (auto& s: data) {
    cin >> s;
  }

  cout << reduce(solve(move(data), k)) << endl;

  return 0;
}