Problem:
Problem A: Ennichi
縁日に来たうさぎが, ある出店のゲームの景品がキャロットケーキであるのを見つけた. このゲームのルールは以下の通りである.
縦h マス× 横w マスの格子状のフィールドがあり, 各マスにつき高々1 個のブロックが置かれる. 各ブロックにはアルファベット大文字(‘A’ - ‘Z’) のいずれかで表される色がついている. 同色のブロックが縦または横に一直線上にn 個以上連続して並ぶと, それらのブロックは消滅する.
参加者は横に隣り合う2 つのマスを選んで, それらの状態を互いに入れ替える操作ができる. ブロックの入れ替え, 消滅, 落下によってブロックの有るマスの1 つ下のマスにブロックが無くなると, このブロックは落下する. このときふたたび同色のブロックがn 個以上並ぶと消滅する. ただし, ブロックの消滅は, 落下するブロックが存在する間は起こらず, すべてのブロックの落下が終了したタイミングで同時に起こる.
1 回の操作でフィールド上のすべてのブロックを消滅させると, このゲームは成功となり景品のケーキを得ることができる. うさぎは1 回分の参加費で確実にケーキを手に入れたく, それができない場合は参加したくない. ゲーム開始時のフィールドの状態から, うさぎがこのゲームに参加すべきであるか答えよ.
Input
入力の一行目にはh, w, n がスペースで区切られて与えられる.
2 ≤
h
,
w
,
n
≤ 30
続く
h
行にはフィールドの状態が上から順に与えられる. アルファベット大文字はブロックを, ‘.’ は空きマスを表す. 与えられるフィールドの状態には, 縦あるいは横に
n
個以上連続する同色のブロックはなく, 落下する状態にあるブロックもない. 1 個以上のブロックが存在する.
Output
うさぎがこのゲームに参加すべきであるなら”YES”を, そうでないなら”NO”を一行に出力せよ.
Sample Input 1
4 6 3
......
...Y..
...Y..
RRYRYY
Sample Output 2
YES
Sample Input 1
4 6 3
......
...Y..
...Y..
RRYRY.
Sample Output 2
NO

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

ll mod = 998244353;
const ll INF = 1e18;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if(argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll h, w, l;
    cin >> h >> w >> l;
    vector<string> s(h);
    for(auto& x : s) cin >> x;

    auto simu = [&](vector<string> inp) -> bool {
        bool loo = true;
        while(loo) {
            loo = false;
            for(int j = 0; j < w; j++) {
                for(int i = h - 2; i >= 0; i--) {
                    ll k = i + 1;
                    while(k < h && inp[k][j] == '.') {
                        swap(inp[k][j], inp[k-1][j]);
                        k++;
                    }
                }
            }

            vector<vector<char>> ok(h, vector<char>(w, false));
            for(int i = 0; i < h; i++) {
                for(int j = 0; j + l - 1 < w; j++) {
                    bool ren = true;
                    for(int k = 0; k < l; k++) {
                        if(inp[i][j + k] != inp[i][j]) ren = false;
                    }
                    if(ren) {
                        for(int k = 0; k < l; k++) {
                            ok[i][j + k] = true;
                        }
                    }
                }
            }
            for(int i = 0; i + l - 1 < h; i++) {
                for(int j = 0; j < w; j++) {
                    bool ren = true;
                    for(int k = 0; k < l; k++) {
                        if(inp[i + k][j] != inp[i][j]) ren = false;
                    }
                    if(ren) {
                        for(int k = 0; k < l; k++) {
                            ok[i + k][j] = true;
                        }
                    }
                }
            }

            for(int i = 0; i < h; i++) {
                for(int j = 0; j < w; j++) {
                    if(ok[i][j]) {
                        loo |= inp[i][j] != '.';
                        inp[i][j] = '.';
                    }
                }
            }
        }
        ll cnt = 0;
        for(auto& x : inp) cnt += count(x.begin(), x.end(), '.');

        return cnt == h * w;
    };

    bool ans = false;

    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w - 1; j++) {
            swap(s[i][j], s[i][j + 1]);
            ans |= simu(s);
            swap(s[i][j], s[i][j + 1]);
        }
    }

    cout << (ans ? "YES" : "NO") << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int N, M, K;
vector<vector<int>> A;

bool Clear(vector<vector<int>>& B) {
    bool Ret = false;
    vector<vector<bool>> flag(N, vector<bool>(M, false));
    rep(i,0,N) {
        rep(j,0,M) {
            if (B[i][j] == -1) continue;
            if (j+K-1 >= M) continue;
            bool check = true;
            rep(k,0,K) {
                if (B[i][j+k] != B[i][j]) check = false;
            }
            if (check) {
                Ret = true;
                rep(k,0,K) flag[i][j+k] = true;
            }
        }
    }
    rep(i,0,N) {
        rep(j,0,M) {
            if (B[i][j] == -1) continue;
            if (i+K-1 >= N) continue;
            bool check = true;
            rep(k,0,K) {
                if (B[i+k][j] != B[i][j]) check = false;
            }
            if (check) {
                Ret = true;
                rep(k,0,K) flag[i+k][j] = true;
            }
        }
    }
    rep(i,0,N) {
        rep(j,0,M) {
            if (flag[i][j]) B[i][j] = -1;
        }
    }
    return Ret;
}

void Fall(vector<vector<int>>& B) {
    vector<vector<int>> C(M);
    rep(i,0,M) {
        rep(j,0,N) {
            if (B[j][i] != -1) C[i].push_back(B[j][i]);
        }
    }
    vector<vector<int>> Ret(N,vector<int>(M,-1));
    rep(i,0,M) {
        rep(j,0,C[i].size()) {
            Ret[j][i] = C[i][j];
        }
    }
    B = Ret;
}

bool Swapping(int X, int Y) {
    vector<vector<int>> B = A;
    swap(B[X][Y], B[X][Y+1]);
    while(1) {
        Fall(B);
        if (!Clear(B)) break;
    }
    bool check = true;
    rep(i,0,N) {
        rep(j,0,M) {
            if (B[i][j] != -1) check = false;
        }
    }
    return check;
}

int main() {
    cin >> N >> M >> K;
    A.assign(N,vector<int>(M));
    rrep(i,0,N) {
        rep(j,0,M) {
            char C;
            cin >> C;
            if (C == '.') A[i][j] = -1;
            else A[i][j] = C - 'A';
        }
    }
    bool ANS = false;
    rep(i,0,N) {
        rep(j,0,M-1) {
            if (Swapping(i,j)) ANS = true;
        }
    }
    cout << (ANS ? "YES" : "NO") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3520
final_rank: 14

code_snippet: #include <bits/stdc++.h>

int main() {
    using Board = std::vector<std::string>;
    int h, w, n;
    std::cin >> h >> w >> n;
    Board init(h);
    for (int i = 0; i < h; i++) std::cin >> init[i];
    const int DI[] = {0, 0};
    const int DJ[] = {1, -1};

    auto clear = [&](Board board) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (board[i][j] != '.') return false;
            }
        }
        return true;
    };

    auto erase_pos = [&](Board board) {
        std::vector erase(h, std::vector<bool>(w));
        // tate
        for (int i = 0; i + n <= h; i++) {
            for (int j = 0; j < w; j++) {
                auto tar = board[i][j];
                if (tar == '.') continue;
                bool ok = true;
                for (int k = 0; k < n; k++) {
                    if (board[i + k][j] != tar) {
                        ok = false;
                        break;
                    }
                }
                if (!ok) continue;
                for (int k = 0; k < n; k++) {
                    erase[i + k][j] = true;
                }
            }
        }

        // yoko
        for (int i = 0; i < h; i++) {
            for (int j = 0; j + n <= w; j++) {
                auto tar = board[i][j];
                if (tar == '.') continue;
                bool ok = true;
                for (int k = 0; k < n; k++) {
                    if (board[i][j + k] != tar) {
                        ok = false;
                        break;
                    }
                }
                if (!ok) continue;
                for (int k = 0; k < n; k++) {
                    erase[i][j + k] = true;
                }
            }
        }

        return erase;
    };

    auto fall = [&](Board board) {
        std::vector c(w, std::vector<char>());
        for (int j = 0; j < w; j++) {
            for (int i = h - 1; i >= 0; i--) {
                if (board[i][j] != '.') {
                    c[j].push_back(board[i][j]);
                }
            }
        }
        Board next(h, std::string(w, '.'));
        for (int j = 0; j < w; j++) {
            for (int i = 0; i < (int)c[j].size(); i++) {
                next[h - 1 - i][j] = c[j][i];
            }
        }
        return next;
    };

    auto print = [&](Board board) {
        for (int i = 0; i < h; i++) {
            std::cerr << board[i] << '\n';
        }
    };

    auto sim = [&](Board board) {
        // std::cerr << "start" << '\n';
        // print(board);
        while (true) {
            board = fall(board);
            if (clear(board)) {
                return true;
            }
            auto erase = erase_pos(board);
            bool not_clear = true;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (erase[i][j]) {
                        assert(board[i][j] != '.');
                        not_clear = false;
                        board[i][j] = '.';
                    }
                }
            }
            if (not_clear) return false;
            // std::cerr << "erased" << '\n';
            // print(board);
        }
    };

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            // if (init[i][j] == '.') continue;
            for (int d = 0; d < 2; d++) {
                int ni = i + DI[d];
                int nj = j + DJ[d];
                if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
                // if (init[ni][nj] == '.') continue;
                Board board = init;
                std::swap(board[i][j], board[ni][nj]);
                if (sim(board)) {
                    std::cout << "YES" << '\n';
                    return 0;
                }
            }
        }
    }
    std::cout << "NO" << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define esper(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<x<<'\n',static_cast<void>(0)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
void SOLVE();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<fixed<<setprecision(16);
  #ifdef LOCAL
  clock_t start=clock();
  #endif
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
  #ifdef LOCAL
  cerr<<"time:";
  cerr<<(clock()-start)/1000;
  cerr<<"ms\n";
  #endif
}
struct UF{
  vector<int>par;
  UF(int n):par(n,-1){}
  int root(int u){
    if(par[u]<0)return u;
    return par[u]=root(par[u]);
  }
  void merge(int u,int v){
    int ru=root(u),rv=root(v);
    if(ru==rv)return;
    if(ru>rv)swap(ru,rv);
    par[ru]+=par[rv];
    par[rv]=ru;
  }
  bool same(int u,int v){return root(u)==root(v);}
};
int h,w;
void SOLVE(){
  int n;
  cin>>h>>w>>n;
  vector<string>s(h);
  cin>>s;
  auto eval=[](vector<string>s)->vector<string> {
    vector<string>nxt(h,string(w,'.'));
    rep(j,w){
      string now;
      rep(i,h)if(s[i][j]!='.')now+=s[i][j];
      rep(i,now.size())nxt[i+h-now.size()][j]=now[i];
    }
    return  nxt;
  };
  auto solve=[&]()->void {
    vector<string>cop(s);
    cop=eval(cop);
    while(true){
      bool non=false;
      rep(i,h)rep(j,w){
        if(cop[i][j]!='.')non=true;
      }
      debugg(cop);
      if(!non){
        cout<<"YES"<<endl;
        exit(0);
      }
      UF uf(h*w);
      rep(i,h)rep(j,w)if(cop[i][j]!='.'){
        if(i+n<=h){
          bool ok=true;
          rep(k,n)ok&=cop[i][j]==cop[i+k][j];
          if(ok)rep(k,n)uf.merge(i*w+j,(i+k)*w+j);
        }
        if(j+n<=w){
          bool ok=true;
          rep(k,n)ok&=cop[i][j]==cop[i][j+k];
          if(ok)rep(k,n)uf.merge(i*w+j,i*w+j+k);
        }
      }
      map<int,vector<pair<int,int>>>idx;
      rep(i,h)rep(j,w)if(cop[i][j]!='.'){
        idx[uf.root(i*w+j)].push_back({i,j});
      }
      bool deleted=false;
      for(auto [k,v]:idx){
        if(v.size()==1)continue;
        assert(v.size()>=n);
        deleted=true;
        for(auto [x,y]:v)cop[x][y]='.';
      }
      if(!deleted)return;
      cop=eval(cop);
    }
  };
  rep(i,h)rep(j,w){
    if(j+1!=w){
      swap(s[i][j],s[i][j+1]);
      solve();
      swap(s[i][j],s[i][j+1]);
    }
  }
  cout<<"NO"<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3544
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
using maxheap = priority_queue<T>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
// const int MOD = 1000000007;
const int MOD = 998244353;

vector<string> drop(vector<string> T) {
    int H = sz(T), W = sz(T[0]);
    vector<string> tmp(W);
    rep(j, W) {
        per(i, H) {
            if (T[i][j] != '.') {
                tmp[j] += T[i][j];
                T[i][j] = '.';
            }
        }
        rep(i, sz(tmp[j])) T[H - 1 - i][j] = tmp[j][i];
    }

    return T;
}

vector<string> ope(vector<string> S, int K) {
    int H = sz(S), W = sz(S[0]);
    auto T = S;
    rep(i, H) rep(j, W) {
        if (i + K <= H) {
            bool flag = true;
            rep2(x, i, i + K) {
                if (S[i][j] != S[x][j]) flag = false;
            }
            if (flag) {
                rep2(x, i, i + K) T[x][j] = '.'; //
            }
        }
        if (j + K <= W) {
            bool flag = true;
            rep2(y, j, j + K) {
                if (S[i][j] != S[i][y]) flag = false;
            }
            if (flag) {
                rep2(y, j, j + K) T[i][y] = '.'; //
            }
        }
    }

    return drop(T);
}

void solve() {
    int H, W, N;
    cin >> H >> W >> N;

    vector<string> S(H);
    rep(i, H) cin >> S[i];

    rep(i, H) rep(j, W - 1) {
        auto T = S;
        swap(T[i][j], T[i][j + 1]);
        T = drop(T);
        while (true) {
            // cout << "#\n";
            // rep(i, H) cout << T[i] << '\n';
            auto NT = ope(T, N);
            if (T == NT) break;
            T = NT;
        }
        bool flag = true;
        rep(x, H) rep(y, W) {
            if (T[x][y] != '.') flag = false;
        }
        if (flag) {
            cout << "YES\n";
            return;
        }
    }

    cout << "NO\n";
}

int main() {
    solve(); //
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 11

code_snippet: // author: hanyu
#include <bits/stdc++.h>
using namespace std;

int h, w, n;

int f(vector<string> &x) {
  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (x[i][j] != '.') res++;
    }
  }
  return res;
}

bool solve(vector<string> x) {
  for (int j = 0; j < w; j++) {
    for (int _it2 = 0; _it2 < h + 3; _it2++) {
      for (int i = h - 1; i > 0; i--) {
        if (x[i][j] == '.' && x[i - 1][j] != '.') swap(x[i][j], x[i - 1][j]);
      }
    }
  }
  //
  for (int _it = 0; _it < h * w / n + 3; _it++) {
    int res1 = f(x);
    vector<vector<int>> y(h, vector<int>(w, 0));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        set<char> st;
        if (i + n <= h) {
          for (int ii = i; ii < i + n; ii++) st.insert(x[ii][j]);
        }
        if (st.size() == 1) {
          for (int ii = i; ii < i + n; ii++) y[ii][j] = 1;
        }
        st.clear();
        if (j + n <= w) {
          for (int jj = j; jj < j + n; jj++) st.insert(x[i][jj]);
        }
        if (st.size() == 1) {
          for (int jj = j; jj < j + n; jj++) y[i][jj] = 1;
        }
      }
    }
    //
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (y[i][j] == 1) x[i][j] = '.';
      }
    }
    //
    for (int j = 0; j < w; j++) {
      for (int _it2 = 0; _it2 < h + 3; _it2++) {
        for (int i = h - 1; i > 0; i--) {
          if (x[i][j] == '.' && x[i - 1][j] != '.') swap(x[i][j], x[i - 1][j]);
        }
      }
    }
    int res2 = f(x);
    if (res1 == res2) break;
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (x[i][j] != '.') return false;
    }
  }
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

//  int h, w, n;
  cin >> h >> w >> n;

  vector<string> x(h);
  for (int i = 0; i < h; i++) cin >> x[i];

  bool ok = false;
  for (int i = 0; i < h; i++) {
    for (int j = 1; j < w; j++) {
      swap(x[i][j - 1], x[i][j]);
      if (solve(x)) {
        cout << "YES" << endl;
        return 0;
      }
      swap(x[i][j - 1], x[i][j]);
    }
  }

  cout << "NO" << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 3476
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}
ll H, W, N;
bool game(vector<string> S) {
    while (1) {
        vvb er(H, vb(W, false));
        rep(i, 30) {
            rep(hh, H - 1) {
                ll h = H - hh - 1;
                rep(w, W) {
                    if (S[h][w] == '.') {
                        swap(S[h][w], S[h - 1][w]);
                    }
                }
            }
        }
        rep(h, H) {
            char p = '&';
            ll k = 0;
            rep(w, W) {
                if (S[h][w] != '.') {
                    if (S[h][w] == p)k++;
                    else {
                        if (k >= N) {
                            for (ll i = w - k; i < w; i++) {
                                er[h][i] = 1;
                            }
                        }
                        p = S[h][w];
                        k = 1;
                    }
                }
                else {
                    if (k >= N) {
                        for (ll i = w - k; i < w; i++) {
                            er[h][i] = 1;
                        }
                    }
                    k = 0;
                    p = '.';
                }
            }
            if (k >= N) {
                for (ll i = W - k; i < W; i++) {
                    er[h][i] = 1;
                }
            }
        }
        rep(w, W) {
            char p = '&';
            ll k = 0;
            rep(h, H) {

                if (S[h][w] != '.') {
                    if (S[h][w] == p)k++;
                    else {
                        if (k >= N) {
                            for (ll i = h - k; i < h; i++) {
                                er[i][w] = 1;
                            }
                        }
                        p = S[h][w];
                        k = 1;
                    }
                }
                else {
                    if (k >= N) {
                        for (ll i = h - k; i < h; i++) {
                            er[i][w] = 1;
                        }
                    }
                    k = 0;
                    p = '.';
                }
            }
            if (k >= N) {
                for (ll i = H - k; i < H; i++) {
                    er[i][w] = 1;
                }
            }
        }
        bool C = 0;
        rep(h, H) {
            rep(w, W) {
                if (er[h][w]) {
                    S[h][w] = '.';
                    C = 1;
                }
            }
        }
        if (C == 0)break;
        
    }
    rep(h, H) {
        rep(w, W) {
            if (S[h][w] != '.')return false;
        }
    }
    return 1;
}

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


    cin >> H >> W >> N;
    vector<string> S(H);
    rep(i, H) {
        cin >> S[i];
    }



    rep(h, H) {
        rep(w, W - 1) {
            if (S[h][w] == S[h][w + 1])continue;
            swap(S[h][w], S[h][w + 1]);
            if (game(S)) {
                cout << "YES" << endl;
                return 0;
            }
            swap(S[h][w], S[h][w + 1]);
        }
    }
    cout << "NO" << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

// {{{ Templates

// clang-format off

// Macros
#define over_load_(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)
#define rep2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)
#define rep3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)
#define rep4(i, l, r, stride) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (stride))
#define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__)
#define rrep2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)
#define rrep3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)
#define rrep4(i, l, r, stride) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (stride))
#define len(x) (static_cast<int>((x).size()))
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)

// Operators
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool is_first = true; for (auto x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ")<<v.front(); v.pop(); is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ") << v.top(); v.pop(); is_first=false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { for (const auto &vec: v) { os << vec << '\n'; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_first = true; for (T x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in: v) { is >> in; } return is; }

// For debug macro
int find_comma_not_bracketed(string_view s){ stack<char> bs; string lbs = "({[", rbs = ")}]"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); }
template <typename T, typename... Ts> void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << ":" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<Ts>(rest)...); } }

// Functions
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }

// Structs
struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

// Type aliases
using ull = unsigned long long;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

// Literals
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = static_cast<int>(1e9) + 7;
constexpr char newl = '\n';

// clang-format on

// }}} Templates

namespace tools {
  using namespace std;
}

#include <vector>

namespace tools {
  template <typename ArrayType>
  vector<ArrayType> rotate_cw(const vector<ArrayType> &s) {
    int h = s.size(), w = s[0].size();
    vector<ArrayType> res(w);
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        res[i].push_back(s[h - 1 - j][i]);
      }
    }
    return res;
  }

  template <typename ArrayType>
  vector<ArrayType> rotate_ccw(const vector<ArrayType> &s) {
    int h = s.size(), w = s[0].size();
    vector<ArrayType> res(w);
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        res[i].push_back(s[j][w - 1 - i]);
      }
    }
    return res;
  }
} // namespace tools
using namespace tools;


int h, w, n;

vector<string> drop(vector<string> s) {
  vector<string> rotated = rotate_cw(s);
  rep(i, len(rotated)) {
    rep(k, len(rotated[i]) - 1) {
      rep(j, len(rotated[i]) - 1) {
        if (rotated[i][j] != '.')
          continue;

        swap(rotated[i][j], rotated[i][j + 1]);
      }
    }
  }

  return rotate_ccw(rotated);
}

vector<string> disappear(vector<string> s) {
  auto arrived = make_vector(h, w, false);
  auto erasable = make_vector(h, w, false);

  auto is_inside = [](int y, int x, int h, int w) -> bool {
    return 0 <= y and y < h and 0 <= x and x < w;
  };

  auto search_erasable = [&](int sy, int sx, char target) -> void {
    rep(dir, 4) {
      int cnt = 0;
      {
        int ny = sy;
        int nx = sx;
        for (; is_inside(ny, nx, h, w); ny += dy[dir], nx += dx[dir]) {
          if (s[ny][nx] != target)
            break;
          cnt++;
        }
      }
      if (cnt >= n) {
        int ny = sy;
        int nx = sx;
        for (; is_inside(ny, nx, h, w); ny += dy[dir], nx += dx[dir]) {
          if (s[ny][nx] != target)
            break;

          erasable[ny][nx] = true;
        }
      }
    }
  };

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '.')
        continue;

      search_erasable(i, j, s[i][j]);
    }
  }

  rep(i, h) {
    rep(j, w) {
      if (erasable[i][j]) {
        s[i][j] = '.';
      }
    }
  }
  return s;
}

void output(const vector<string> &s) {
  rep(i, h) {
    cout << s[i] << newl;
  }
}

bool is_allclear(const vector<string> &s) {
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] != '.')
        return false;
    }
  }
  return true;
}

bool solve(vector<string> s) {
  // debug("origin---------------begin");
  // output(s);
  // debug("origin---------------end");
  while (true) {
    auto dropped = drop(s);
    auto dis = disappear(dropped);
    if (is_allclear(dis)) {
      return true;
    }
    bool updated = s != dis;
    if (not updated)
      break;

    // debug("begin-----------");
    // output(dis);
    // debug("end-----------");
    s = dis;
  }
  return false;
}


int main() {
  cin >> h >> w >> n;

  vector<string> s(h);
  cin >> s;

  bool ans = false;

  rep(i, h) {
    rep(j, w - 1) {
      swap(s[i][j], s[i][j + 1]);
      ans = ans || solve(s);
      swap(s[i][j], s[i][j + 1]);
    }
  }
  ans = ans || solve(s);

  cout << (ans ? "YES" : "NO") << newl;
}
accuracy: 1
time_ms: 50
memory_kb: 3500
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(h,w,n);
    vector<string> A(h);
    in(A);
    auto check=[&](vector<string> &D,vector<vector<bool>> &used,int y,int x){
        bool a=true,b=true,c=true,e=true;
        for(int d=0;d<n;d++){
            if(y+d>=h||D[y][x]!=D[y+d][x])a=false;
            if(y-d<0||D[y][x]!=D[y-d][x])b=false;
            if(x+d>=w||D[y][x]!=D[y][x+d])c=false;
            if(x-d<0||D[y][x]!=D[y][x-d])e=false;
        }
        for(int d=0;d<n;d++){
            if(a)used[y+d][x]=true;
            if(b)used[y-d][x]=true;
            if(c)used[y][x+d]=true;
            if(e)used[y][x-d]=true;
        }
    };
    auto g=[&](vector<string> &C){
        bool e=true;
        while(e){
            e=false;
            for(int y=0;y+1<h;y++){
                for(int x=0;x<w;x++){
                    if(C[y][x]=='.')continue;
                    if(C[y+1][x]=='.'){
                        e=true;
                        swap(C[y][x],C[y+1][x]);
                    }
                }
            }
        }
        vector2d(bool,used,h,w,false);
        rep(y,h){
            rep(x,w){
                if(C[y][x]=='.')continue;
                check(C,used,y,x);
            }
        }
        bool res=false;
        rep(y,h){
            rep(x,w){
                if(used[y][x]){
                    res=true;
                    C[y][x]='.';
                }
            }
        }
        e=true;
        while(e){
            e=false;
            for(int y=0;y+1<h;y++){
                for(int x=0;x<w;x++){
                    if(C[y][x]=='.')continue;
                    if(C[y+1][x]=='.'){
                        e=true;
                        swap(C[y][x],C[y+1][x]);
                    }
                }
            }
        }
        return res;
    };
    auto f=[&](vector<string> B){
        //rep(y,h)debug(B[y]);
        //cerr<<'\n';
        while(g(B)){
            //rep(y,h)debug(B[y]);
            //cerr<<'\n';
        }
        rep(y,h){
            rep(x,w){
                if(B[y][x]!='.')return false;
            }
        }
        return true;
    };
    for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            if(A[y][x]=='.')continue;
            /*if(y+1<h){
                swap(A[y][x],A[y+1][x]);
                if(f(A)){
                    out("YES");
                    return 0;
                }
                swap(A[y][x],A[y+1][x]);
            }*/
            if(x+1<w){
                swap(A[y][x],A[y][x+1]);
                if(f(A)){
                    out("YES");
                    return 0;
                }
                swap(A[y][x],A[y][x+1]);
            }
        }
    }
    out("NO");
}
accuracy: 1
time_ms: 60
memory_kb: 3520
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint2 = modint998244353;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;
//const Int mod = 998244353;

typedef struct {
    int64_t to, weight;
} edge;

Int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};
Int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};

template<class T> 
istream &operator>>(istream &is, vector<T> &v) { 
    for (auto &e : v) {
        is >> e; 
    }
    return is; 
}

bool print_space_enable = false;
void print() { 
    std::cout << '\n'; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) std::cout << " ";
    std::cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << '\n';
}

template<typename T>
void print(vector<vector<T>> v) {
    for (size_t i = 0; i < v.size(); i++) {
        for (size_t j = 0; j < v[i].size(); j++) {
            if (j > 0) std::cout << " ";
            std::cout << v[i][j];
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}

template<class T>
vector<T> make_vec(size_t n, T val) {
    return vector<T>(n, val);
}

template<class... Ts>
auto make_vec(size_t n, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}

bool is_overflow(Int a, Int b) {
    return a > INF / b;
}

int h, w, n;

// ランレングス圧縮
vector<pair<char, int64_t>> run_length(string s) {
    vector<pair<char, int64_t>> ret;
    if (s.empty()) return ret;
    int64_t cnt = 0;
    char c = s[0];
    for (size_t i = 0; i < s.size(); i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            ret.emplace_back(c, cnt);
            c = s[i];
            cnt = 1;
        }
        if (i + 1 == s.size()) {
            ret.emplace_back(c, cnt);
        }
    }
    return ret;
}

bool f(vector<string> s) {
    bool update = true;
    while (update) {
        update = false;
        bool update2 = true;
        while (update2) {
            update2 = false;
            for (int i = h - 1; i - 1 >= 0; i--) for (int j = 0; j < w; j++) {
                if (s[i][j] == '.' and s[i - 1][j] != '.') {
                    swap(s[i][j], s[i - 1][j]);
                    update2 = true;
                    update = true;
                }
            }
        }
        auto del = make_vec(h, w, false);
        for (int i = 0; i < h; i++) {
            auto v = run_length(s[i]);
            int sum = 0;
            for (auto j : v) {
                if (j.first != '.' and j.second >= n) {
                    for (int k = 0; k < j.second; k++) {
                        del[i][sum + k] = true;
                    }
                }
                sum += j.second;
            }
        }
        for (int j = 0; j < w; j++) {
            string t = "";
            for (int i = 0; i < h; i++) {
                t += s[i][j];
            }
            auto v = run_length(t);
            int sum = 0;
            for (auto i : v) {
                if (i.first != '.' and i.second >= n) {
                    for (int k = 0; k < i.second; k++) {
                        del[sum + k][j] = true;
                    }
                }
                sum += i.second;
            }
        }
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
            if (del[i][j]) {
                update = true;
                s[i][j] = '.';
            }
        }
        
        // print(del);
        // cout << endl;
        // for (auto i : s) {
        //     print(i);
        // } cout << endl;
    }
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (s[i][j] != '.') {
            return false;
        }
    }
    return true;
}

void solve() {
    cin >> h >> w >> n;
    vector<string> s(h);
    cin >> s;
    for (int i = 0; i < h; i++) for (int j = 0; j + 1 < w; j++) {
        swap(s[i][j], s[i][j + 1]);
        if (f(s)) {
            print("YES");
            return;
        }
        swap(s[i][j], s[i][j + 1]);
    }
    print("NO");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    //Int _t; cin >> _t; while (_t--) solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3480
final_rank: 8

code_snippet: #pragma GCC target("avx,avx2")
//#pragma GCC optimize("Ofast")
//#undef LOCAL

#include <unistd.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstring>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>

namespace yosupo {

namespace internal {

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

}  // namespace internal

int bsf(unsigned int n) {
    return __builtin_ctz(n);
}
int bsf(unsigned long n) {
    return __builtin_ctzl(n);
}
int bsf(unsigned long long n) {
    return __builtin_ctzll(n);
}
int bsf(unsigned __int128 n) {
    unsigned long long low = (unsigned long long)(n);
    unsigned long long high = (unsigned long long)(n >> 64);
    return low ? __builtin_ctzll(low) : 64 + __builtin_ctzll(high);
}

int bsr(unsigned int n) {
    return 8 * (int)sizeof(unsigned int) - 1 - __builtin_clz(n);
}
int bsr(unsigned long n) {
    return 8 * (int)sizeof(unsigned long) - 1 - __builtin_clzl(n);
}
int bsr(unsigned long long n) {
    return 8 * (int)sizeof(unsigned long long) - 1 - __builtin_clzll(n);
}
int bsr(unsigned __int128 n) {
    unsigned long long low = (unsigned long long)(n);
    unsigned long long high = (unsigned long long)(n >> 64);
    return high ? 127 - __builtin_clzll(high) : 63 - __builtin_ctzll(low);
}

int popcnt(unsigned int n) {
    return __builtin_popcount(n);
}
int popcnt(unsigned long n) {
    return __builtin_popcountl(n);
}
int popcnt(unsigned long long n) {
    return __builtin_popcountll(n);
}

}  // namespace yosupo

#include <cassert>
#include <numeric>
#include <type_traits>

namespace yosupo {

namespace internal {

template <class T> using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T> using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T> using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>;

template <class T> using is_integral =
    typename std::conditional<std::is_integral<T>::value || internal::is_signed_int128<T>::value ||
                                  internal::is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T> using is_signed_int =
    typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) ||
                                  is_signed_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T> using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T> using to_unsigned =
    typename std::conditional<is_signed_int128<T>::value, make_unsigned_int128<T>,
                              typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,
                                                        std::common_type<T>>::type>::type;

template <class T> using is_integral_t = std::enable_if_t<is_integral<T>::value>;

template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace yosupo

namespace yosupo {

struct Scanner {
public:
    Scanner(const Scanner&) = delete;
    Scanner& operator=(const Scanner&) = delete;

    Scanner(FILE* fp) : fd(fileno(fp)) {}

    void read() {}
    template <class H, class... T> void read(H& h, T&... t) {
        bool f = read_single(h);
        assert(f);
        read(t...);
    }

    int read_unsafe() { return 0; }
    template <class H, class... T> int read_unsafe(H& h, T&... t) {
        bool f = read_single(h);
        if (!f) return 0;
        return 1 + read_unsafe(t...);
    }

    int close() { return ::close(fd); }

private:
    static constexpr size_t SIZE = 1 << 15;

    int fd = -1;
    std::array<char, SIZE> line;
    size_t st = 0, ed = 0;
    bool eof = false;

    bool read_single(std::string& ref) {
        if (!skip_space()) return false;
        ref = "";
        while (true) {
            char c = top();
            if (c <= ' ') break;
            ref += c;
            st++;
        }
        return true;
    }
    bool read_single(double& ref) {
        std::string s;
        if (!read_single(s)) return false;
        ref = std::stod(s);
        return true;
    }

    template <class T, std::enable_if_t<std::is_same<T, char>::value>* = nullptr>
    bool read_single(T& ref) {
        if (!skip_space(50)) return false;
        ref = top();
        st++;
        return true;
    }

    template <class T, internal::is_signed_int_t<T>* = nullptr,
              std::enable_if_t<!std::is_same<T, char>::value>* = nullptr>
    bool read_single(T& sref) {
        using U = internal::to_unsigned_t<T>;
        if (!skip_space(50)) return false;
        bool neg = false;
        if (line[st] == '-') {
            neg = true;
            st++;
        }
        U ref = 0;
        do {
            ref = 10 * ref + (line[st++] & 0x0f);
        } while (line[st] >= '0');
        sref = neg ? -ref : ref;
        return true;
    }
    template <class U, internal::is_unsigned_int_t<U>* = nullptr,
              std::enable_if_t<!std::is_same<U, char>::value>* = nullptr>
    bool read_single(U& ref) {
        if (!skip_space(50)) return false;
        ref = 0;
        do {
            ref = 10 * ref + (line[st++] & 0x0f);
        } while (line[st] >= '0');
        return true;
    }

    bool reread() {
        if (ed - st >= 50) return true;
        if (st > SIZE / 2) {
            std::memmove(line.data(), line.data() + st, ed - st);
            ed -= st;
            st = 0;
        }
        if (eof) return false;
        auto u = ::read(fd, line.data() + ed, SIZE - ed);
        if (u == 0) {
            eof = true;
            line[ed] = '\0';
            u = 1;
        }
        ed += u;
        return true;
    }

    char top() {
        if (st == ed) {
            bool f = reread();
            assert(f);
        }
        return line[st];
    }

    bool skip_space(unsigned int token_len = 0) {
        while (true) {
            while (st != ed && line[st] <= ' ') st++;
            if (ed - st > token_len) return true;
            for (auto i = st; i < ed; i++) {
                if (line[i] <= ' ') return true;
            }
            if (!reread()) return false;
        }
    }
};

struct Printer {
public:
    template <char sep = ' ', bool F = false> void write() {}
    template <char sep = ' ', bool F = false, class H, class... T> void write(const H& h, const T&... t) {
        if (F) write_single(sep);
        write_single(h);
        write<true>(t...);
    }
    template <char sep = ' ', class... T> void writeln(const T&... t) {
        write<sep>(t...);
        write_single('\n');
    }

    Printer(FILE* _fp) : fd(fileno(_fp)) {}
    ~Printer() { flush(); }

    int close() {
        flush();
        return ::close(fd);
    }

    void flush() {
        if (pos) {
            auto res = ::write(fd, line.data(), pos);
            assert(res != -1);
            pos = 0;
        }
    }

private:
    static std::array<std::array<char, 2>, 100> small;
    static std::array<unsigned long long, 20> tens;

    static constexpr size_t SIZE = 1 << 15;
    int fd;
    std::array<char, SIZE> line;
    size_t pos = 0;
    std::stringstream ss;

    template <class T, std::enable_if_t<std::is_same<char, T>::value>* = nullptr>
    void write_single(const T& val) {
        if (pos == SIZE) flush();
        line[pos++] = val;
    }

    template <class T, internal::is_signed_int_t<T>* = nullptr,
              std::enable_if_t<!std::is_same<char, T>::value>* = nullptr>
    void write_single(const T& val) {
        using U = internal::to_unsigned_t<T>;
        if (val == 0) {
            write_single('0');
            return;
        }
        if (pos > SIZE - 50) flush();
        U uval = val;
        if (val < 0) {
            write_single('-');
            uval = -uval;
        }
        write_unsigned(uval);
    }

    template <class U, internal::is_unsigned_int_t<U>* = nullptr> void write_single(U uval) {
        if (uval == 0) {
            write_single('0');
            return;
        }
        if (pos > SIZE - 50) flush();

        write_unsigned(uval);
    }

    template <class U, internal::is_unsigned_int_t<U>* = nullptr> static int calc_len(U x) {
        int i = (bsr(x) * 3 + 3) / 10;
        if (x < tens[i])
            return i;
        else
            return i + 1;
    }

    template <class U, internal::is_unsigned_int_t<U>* = nullptr,
              std::enable_if_t<8 >= sizeof(U)>* = nullptr>
    void write_unsigned(U uval) {
        size_t len = calc_len(uval);
        pos += len;

        char* ptr = line.data() + pos;
        while (uval >= 100) {
            ptr -= 2;
            memcpy(ptr, small[uval % 100].data(), 2);
            uval /= 100;
        }
        if (uval >= 10) {
            memcpy(ptr - 2, small[uval].data(), 2);
        } else {
            *(ptr - 1) = char('0' + uval);
        }
    }

    template <class U, std::enable_if_t<internal::is_unsigned_int128<U>::value>* = nullptr>
    void write_unsigned(U uval) {
        static std::array<char, 50> buf;
        size_t len = 0;
        while (uval > 0) {
            buf[len++] = char((uval % 10) + '0');
            uval /= 10;
        }
        std::reverse(buf.begin(), buf.begin() + len);
        memcpy(line.data() + pos, buf.data(), len);
        pos += len;
    }

    void write_single(const std::string& s) {
        for (char c : s) write_single(c);
    }
    void write_single(const char* s) {
        size_t len = strlen(s);
        for (size_t i = 0; i < len; i++) write_single(s[i]);
    }
    template <class T> void write_single(const std::vector<T>& val) {
        auto n = val.size();
        for (size_t i = 0; i < n; i++) {
            if (i) write_single(' ');
            write_single(val[i]);
        }
    }
};

std::array<std::array<char, 2>, 100> Printer::small = [] {
    std::array<std::array<char, 2>, 100> table;
    for (int i = 0; i <= 99; i++) {
        table[i][1] = char('0' + (i % 10));
        table[i][0] = char('0' + (i / 10 % 10));
    }
    return table;
}();
std::array<unsigned long long, 20> Printer::tens = [] {
    std::array<unsigned long long, 20> table;
    for (int i = 0; i < 20; i++) {
        table[i] = 1;
        for (int j = 0; j < i; j++) {
            table[i] *= 10;
        }
    }
    return table;
}();

}  // namespace yosupo
using namespace yosupo;

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) {
    return (n == 0) ? 1 : 10 * TEN(n - 1);
}
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL

ostream& operator<<(ostream& os, __int128_t x) {
    if (x < 0) {
        os << "-";
        x *= -1;
    }
    if (x == 0) {
        return os << "0";
    }
    string s;
    while (x) {
        s += char(x % 10 + '0');
        x /= 10;
    }
    reverse(s.begin(), s.end());
    return os << s;
}
ostream& operator<<(ostream& os, __uint128_t x) {
    if (x == 0) {
        return os << "0";
    }
    string s;
    while (x) {
        s += char(x % 10 + '0');
        x /= 10;
    }
    reverse(s.begin(), s.end());
    return os << s;
}

template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T> ostream& operator<<(ostream& os, const V<T>& v);
template <class T> ostream& operator<<(ostream& os, const deque<T>& v);
template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& a);
template <class T> ostream& operator<<(ostream& os, const set<T>& s);
template <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& m);

template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << "," << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    bool f = false;
    for (auto d : v) {
        if (f) os << ",";
        f = true;
        os << d;
    }
    return os << "]";
}

template <class T> ostream& operator<<(ostream& os, const deque<T>& v) {
    os << "[";
    bool f = false;
    for (auto d : v) {
        if (f) os << ",";
        f = true;
        os << d;
    }
    return os << "]";
}
template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& a) {
    os << "[";
    bool f = false;
    for (auto d : a) {
        if (f) os << ",";
        f = true;
        os << d;
    }
    return os << "]";
}

template <class T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << "{";
    bool f = false;
    for (auto d : s) {
        if (f) os << ",";
        f = true;
        os << d;
    }
    return os << "}";
}

template <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& s) {
    os << "{";
    bool f = false;
    for (auto p : s) {
        if (f) os << ",";
        f = true;
        os << p.first << ": " << p.second;
    }
    return os << "}";
}

struct PrettyOS {
    ostream& os;
    bool first;

    template <class T> auto operator<<(T&& x) {
        if (!first) os << ",";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                           \
    do {                                                   \
        cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                 \
        cerr << endl;                                      \
    } while (false);
#else
#define dbg(...)
#endif

Scanner sc = Scanner(stdin);
Printer pr = Printer(stdout);

bool all_same(string s) {
    bool ret = true;
    for (auto&& ch : s) {
        if (ch != s[0]) ret = false;
    }
    return ret;
}

int main() {
    int h, w, n;
    sc.read(h, w, n);
    V<string> b(h);
    for (auto&& line : b) {
        sc.read(line);
    }

    auto is_delete = [&]() {
        bool is_d = false;
        VV<int> d(h, V<int>(w, 0));
        // 行方向
        int i = 0;
        for (auto&& line : b) {
            for (int j = 0; j < w - n + 1; j++) {
                if (line[j] == '.') continue;
                if (all_same(line.substr(j, n))) {
                    is_d = true;
                    for (int k = 0; k < n; k++) {
                        d[i][j + k] = 1;
                    }
                }
            }
            i++;
        }
        // 列方向
        for (int j = 0; j < w; j++) {
            for (int i = 0; i < h - n + 1; i++) {
                if (b[i][j] == '.') continue;
                string line = "";
                for (int k = 0; k < n; k++) {
                    line += b[i + k][j];
                }
                if (all_same(line)) {
                    is_d = true;
                    for (int k = 0; k < n; k++) {
                        d[i + k][j] = 1;
                    }
                }
            }
        }

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (d[i][j] == 1) {
                    b[i][j] = '.';
                }
            }
        }

        return is_d;
    };

    auto is_completed = [&]() {
        bool ret = true;
        for (auto&& line : b) {
            for (auto&& ch : line) {
                if (ch != '.') ret = false;
            }
        }
        return ret;
    };

    V<string> ori_b = b;
    // bに操作を加える全探索
    for (int ch_h1 = 0; ch_h1 < h; ch_h1++) {
        for (int ch_w1 = 0; ch_w1 < w; ch_w1++) {
            for (int ch_w2 = 0; ch_w2 < w; ch_w2++) {
                if (ch_h1 != 0 && ch_w1 == ch_w2) continue;
                int manh_d = 0;
                manh_d = abs(ch_w1 - ch_w2);
                if (manh_d > 1) continue;
                b = ori_b;
                // if (b[ch_h1][ch_w1] == '.' || b[ch_h1][ch_w2] == '.') continue;
                swap(b[ch_h1][ch_w1], b[ch_h1][ch_w2]);
                // 落下処理
                for (int j = 0; j < w; j++) {
                    for (int k = 0; k < h; k++) {
                        // kは使わない
                        for (int i = h - 1; i > 0; i--) {
                            if (b[i][j] == '.' && b[i - 1][j] != '.') {
                                b[i][j] = b[i - 1][j];
                                b[i - 1][j] = '.';
                            }
                        }
                    }
                }
                while (is_delete()) {
                    // 落下処理
                    for (int j = 0; j < w; j++) {
                        for (int k = 0; k < h; k++) {
                            // kは使わない
                            for (int i = h - 1; i > 0; i--) {
                                if (b[i][j] == '.' && b[i - 1][j] != '.') {
                                    b[i][j] = b[i - 1][j];
                                    b[i - 1][j] = '.';
                                }
                            }
                        }
                    }
                }
                // 全部'.'ならYES
                if (is_completed()) {
                    pr.writeln("YES");
                    return 0;
                }
            }
        }
    }
    // for (auto &&line : ori_b)
    // {
    //     pr.writeln(line);
    // }

    pr.writeln("NO");
}
accuracy: 1
time_ms: 60
memory_kb: 3468
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

int H, W, N;

vector<string> fall(vector<string> &s) {
    string tmp = "";
    rep(i, W) tmp += '.';
    vector<string> next(H, tmp);

    rep(j, W) {
        queue<char> que;
        for (int i = H - 1; i >= 0; --i) {
            if (s[i][j] != '.')
                que.push(s[i][j]);
        }

        int i = H - 1;
        while (!que.empty()) {
            char c = que.front();
            que.pop();
            next[i][j] = c;
            i--;
        }
    }

    return next;
}

bool solve(vector<string> s, vector<string> pre = {}) {
    if (s == pre) {
        rep(i, H) rep(j, W) {
            if (s[i][j] != '.')
                return false;
        }
        return true;
    }

    pre = s;
    vector<vector<bool>> flag(H, vector<bool>(W));

    // 横で消えるやつの判定
    rep(i, H) {
        char c = s[i][0];
        int cur = 0;
        rep(j, W + 1) {
            if (j != W && s[i][j] == c) {
                cur++;
            } else {
                if (cur >= N)
                    rep(k, cur) flag[i][j - k - 1] = true;

                if (j != W) {
                    cur = 1;
                    c = s[i][j];
                }
            }
        }
    }

    // 縦で消えるやつの判定
    rep(j, W) {
        char c = s[0][j];
        int cur = 0;
        rep(i, H + 1) {
            if (i != H && s[i][j] == c) {
                cur++;
            } else {
                if (cur >= N)
                    rep(k, cur) flag[i - k - 1][j] = true;

                if (i != H) {
                    cur = 1;
                    c = s[i][j];
                }
            }
        }
    }

    // 消す
    rep(i, H) rep(j, W) {
        if (flag[i][j]) s[i][j] = '.';
    }

    // 落下させる
    vector<string> next = fall(s);
    return solve(next, pre);
}

int main() {
    cin >> H >> W >> N;
    vector<string> S(H);
    rep(i, H) cin >> S[i];

    auto inGrid = [&](int y, int x) -> bool {
        if (y >= 0 && y < H && x >= 0 && x < W)
            return true;
        else
            return false;
    };

    rep(y, H) rep(x, W) {
        for (int dx = -1; dx <= 1; dx += 2) {
            if (S[y][x] == '.') continue;

            int ny = y;
            int nx = x + dx;

            if (!inGrid(ny, nx)) continue;

            swap(S[y][x], S[ny][nx]);
            vector<string> t = fall(S);

            if (solve(t)) {
                cout << "YES" << endl;
                return 0;
            }
            swap(S[y][x], S[ny][nx]);
        }
    }

    cout << "NO" << endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3644
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
using namespace std;

int h,w,n;

bool game(vector<string> &s){
  vector<string> s2(h);
  copy(s.begin(),s.end(),s2.begin());
  rep(j,w){
    int d=0;
    for(int i=h-1;i>=0;i--){
      while(i-d>=0&&s2[i-d][j]=='.') d++;
      if(i-d<0) s[i][j]='.';
      else s[i][j]=s2[i-d][j];
    }
  }

  
  bool first=true;
  bool koushin=true;
  while(koushin){
    koushin=false;
    vector<string> s2(h);
    copy(s.begin(),s.end(),s2.begin());

    rep(j,w){
      int d=0;
      for(int i=h-1;i>=0;i--){
	while(i-d>=0&&s2[i-d][j]=='.') d++;
	if(i-d<0) s[i][j]='.';
	else s[i][j]=s2[i-d][j];
      }
    }
    
    rep(i,h){
      rep(j,w){
	if(s[i][j]=='.') continue;
	int cnt=1;
	while(j+cnt<w&&s[i][j]==s[i][j+cnt]) cnt++;
	if(cnt>=n){
	  rep(k,cnt) s2[i][j+k]='.';
	  koushin=true;
	}
	j+=cnt-1;
      }
    }
    rep(j,w){
      rep(i,h){
	if(s[i][j]=='.') continue;
	int cnt=1;
	while(i+cnt<h&&s[i][j]==s[i+cnt][j]) cnt++;
	if(cnt>=n){
	  rep(k,cnt) s2[i+k][j]='.';
	  koushin=true;
	}
	i+=cnt-1;
      }
    }
    
    rep(j,w){
      int d=0;
      for(int i=h-1;i>=0;i--){
	while(i-d>=0&&s2[i-d][j]=='.') d++;
	if(i-d<0) s[i][j]='.';
	else s[i][j]=s2[i-d][j];
      }
    }
    
    if(first){
      koushin=true;
      first=false;
    }
  }

  bool ret=true;
  rep(i,h) rep(j,w)
    if(s[i][j]!='.') ret=false;
  return ret;
}

    
int main(){
  cin>>h>>w>>n;
  vector<string> s(h);
  rep(i,h) cin>>s[i];

  bool ans=false;
  rep(i,h) rep(j,w-1){
    vector<string> s2(h);
    copy(s.begin(),s.end(),s2.begin());
    
    swap(s2[i][j],s2[i][j+1]);
    bool f=game(s2);
    ans=ans||f;
  }
  
  if(ans) puts("YES");
  else puts("NO");
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 2

code_snippet: #define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
#define fi first
#define se second
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long ll;
const int INTINF = INT_MAX >> 1;
static const ll LLINF = (LLONG_MAX >> 1);

int h, w, n;
string str[30];

bool input(){

	cin >> h >> w >> n;
	rep(i, h){
		cin >> str[i];
	}

	return true;
}

bool erase(){
	bool flag[30][30];
	memr(flag, 0);
	rep(i, h){
		rep(j, w){
			if(str[i][j] == '.') continue;
			int cnt = 1;
			rep2(k, j+1, w){
				if(str[i][j] == str[i][k]) cnt++;
				else break;
			}
			if(cnt >= n){
				rep2(k, j, cnt + j){
					flag[i][k] = true;
				}
			}
		}
	}
	
	rep(j, w){
		rep(i, h){
			if(str[i][j] == '.') continue;
			int cnt = 1;
			rep2(k, i+1, h){
				if(str[i][j] == str[k][j]) cnt++;
				else break;
			}
			if(cnt >= n){
				rep2(k, i, cnt + i){
					flag[k][j] = true;
				}
			}
		}
	}

	bool ret = false;
	rep(i, h){
		rep(j, w){
			if(flag[i][j]) {
				str[i][j] = '.';
				ret = true;
			}
		}
	}
	return ret;
}

void drop(){
	rep(k, 40){
		rrep(i, h){
			rep(j, w){
				if(i > 0)
				if(str[i-1][j] != '.' && str[i][j] == '.'){
					str[i][j] = str[i-1][j];
					str[i-1][j] = '.';
				}
			}
		}
	}
}

int main(){

	input();

	bool res = false;
	rep(i, h){
		rep(j, w-1){
			string str2[30];
			rep(i, h) str2[i] = str[i];
			swap(str[i][j], str[i][j + 1]);
			drop();
			while(erase()){
				// rep(i, h){
				// 	rep(j, w){
				// 		cout << str[i][j];
				// 	}
				// 	cout << endl;
				// }
				// cout << endl;
				drop();
			}
			
			bool ok = true;
			rep(i, h){
				rep(j, w){
					if(str[i][j] != '.') ok = false;
				}
			}
			if(ok) res = true;
			
			rep(i, h) str[i] = str2[i];
		}
	}
	
	string ans = res ? "YES" : "NO";
	cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3432
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int h,w,n; cin >> h >> w >> n;
    vector<string> s(h);
    rep(i,h) cin >> s[i];


    bool ok = false;
    rep(i,h)rep(j,w-1) {
        auto t = s;
        swap(t[i][j], t[i][j + 1]);

        auto drop = [&](void) {
            for(int i = h - 2; i >= 0; i--) {
                rep(j,w) {
                    if(t[i][j] != '.' && t[i + 1][j] == '.') {
                        int pos = i;
                        while(t[pos + 1][j] == '.') {
                            swap(t[pos][j], t[pos + 1][j]);
                            pos++;
                            if(pos == h - 1) break;
                        }
                    }
                }
            }
        };


        int dy[] = {1, 0};
        int dx[] = {0, 1};
        function<bool(void)> win = [&](void) -> bool {
            bool ex = false;
            rep(i,h)rep(j,w) if(t[i][j] != '.') ex = true;
            if(!ex) {
                return true;
            }

            bool flag = false;
            vector<vector<bool>> erased(h, vector<bool> (w, false));
            rep(i,h)rep(j,w) {
                if(t[i][j] == '.') continue;
                for(int k = 0; k < 2; k++) {
                    if(i + dy[k] * (n - 1) < h && j + dx[k] * (n - 1) < w) {
                        bool same = true;
                        for(int l = 0; l < n; l++) {
                            if(t[i][j] != t[i + dy[k] * l][j + dx[k] * l]) same = false;
                        }
                        if(same) {
                            flag = true;
                            for(int l = 0; l < n; l++)
                                erased[i + dy[k] * l][j + dx[k] * l] = true;
                        }
                    }
                }
            }

            if(!flag) return false;
            rep(i,h)rep(j,w) if(erased[i][j]) t[i][j] = '.';
            drop();
            return win();
        };
        drop();
        if(win()) ok = true;
    }
    cout << (ok ? "YES" : "NO") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 11

code_snippet: #line 1 "/Users/nok0/Documents/Programming/nok0/cftemp.hpp"
#include <bits/stdc++.h>
using namespace std;

#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)

// name macro
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;

// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
	for(T &i : v) is >> i;
	return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
	std::string s;
	is >> s;
	__int128_t ret = 0;
	for(int i = 0; i < s.length(); i++)
		if('0' <= s[i] and s[i] <= '9')
			ret = 10 * ret + s[i] - '0';
	a = ret * (s[0] == '-' ? -1 : 1);
	return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
	std::vector<type> name(size); \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
	std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
	std::string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)

// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
	for(int i = 0; i < int(a.size()); ++i) {
		if(i) os << " ";
		os << a[i];
	}
	return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
	std::ostream::sentry s(dest);
	if(s) {
		__uint128_t tmp = value < 0 ? -value : value;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while(tmp != 0);
		if(value < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if(dest.rdbuf()->sputn(d, len) != len) {
			dest.setstate(std::ios_base::badbit);
		}
	}
	return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
	std::cout << H << ' ';
	print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
	for(const auto &v : a)
		std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
	std::cout << H << '\n';
	printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "NO\n"; }
void err(const bool b = true) {
	if(b) {
		std::cout << "-1\n", exit(0);
	}
}

//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
	std::cerr << "{ ";
	for(const auto &v : a) {
		std::cerr << v << ", ";
	}
	std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
	std::cerr << "{\n";
	for(const auto &v : a) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
	std::cerr << "{\n";
	for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
	std::cerr << "{ ";
	for(auto &v : s) {
		view(v);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <class T>
void view(const T &e) { std::cerr << e; }
}  // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	debugger::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(false)
#else
#define debug(...) (void(0))
#endif

// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
	for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
	for(auto &v : a) v /= x;
}
std::vector<std::pair<char, int>> rle(const string &s) {
	int n = s.size();
	std::vector<std::pair<char, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and s[l] == s[r]; r++) {}
		ret.emplace_back(s[l], r - l);
		l = r;
	}
	return ret;
}
template <class T>
std::vector<std::pair<T, int>> rle(const std::vector<T> &v) {
	int n = v.size();
	std::vector<std::pair<T, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and v[l] == v[r]; r++) {}
		ret.emplace_back(v[l], r - l);
		l = r;
	}
	return ret;
}

// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
// modpow
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	a = (a % mod + mod) % mod;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}

// others
struct fast_io {
	fast_io() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} fast_io_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion

void main_();

int main() {
	main_();
	return 0;
}
#line 2 "a.cpp"

//Grid Template
int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};

void main_() {
	INT(h, w, n);
	VEC(string, s, h);
	auto inside = [&](int x, int y) {
		return x >= 0 and x < h and y >= 0 and y < w;
	};
	auto simulate = [&](vector<string> &s) -> bool {
		while(true) {
			RREP(i, h - 1) {
				REP(j, w) {
					if(s[i][j] == '.') continue;
					int t = 1;
					while(i + t < h and s[i + t][j] == '.') {
						swap(s[i + t][j], s[i + t - 1][j]);
						t++;
					}
				}
			}
			set<pii> del;
			REP(i, h) {
				REP(j, w) {
					if(s[i][j] == '.') continue;
					bool f = 0;
					REP(k, 2) {
						int nx = i + dx[k], ny = j + dy[k];
						int cnt = 1;
						while(inside(nx, ny) and s[i][j] == s[nx][ny])
							cnt++, nx += dx[k], ny += dy[k];
						if(cnt >= n) {
							nx = i, ny = j;
							while(inside(nx, ny) and s[i][j] == s[nx][ny]) {
								del.insert({nx, ny});
								nx += dx[k], ny += dy[k];
							}
						}
					}
				}
			}
			if(del.empty()) break;
			for(auto [i, j] : del) {
				s[i][j] = '.';
			}
		}
		bool ok = 1;
		REP(i, h) {
			REP(j, w) { ok &= s[i][j] == '.'; }
		}
		return ok;
	};
	bool res = 0;
	REP(i, h) {
		REP(j, w - 1) {
			auto c = s;
			swap(c[i][j], c[i][j + 1]);
			res |= simulate(c);
		}
	}
	YES(res);
}
accuracy: 1
time_ms: 10
memory_kb: 3472
final_rank: 7

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-12, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 1000000007 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;

  BIT(int n){
    N = n;
    node.resize(N+10);
  }

  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};

int h, w, n; 
bool check(vector<string> f) {
  bool update = true;
  
  for(int j=0; j<w; j++) {
    for(int i=h-1; i>=0; i--) {
      if(f[i][j] == '.' && i-1>=0 && f[i-1][j] != '.') swap(f[i][j], f[i-1][j]); 
    }
  }
  while(update) {
    update = false;
    vector<vector<bool>>era(h, vector<bool>(w, false));
    for(int i=0; i<h; i++) {
      for(int j=0; j<w; j++) {

        if(f[i][j] == '.') continue;
        // 横方向
        
        char c = f[i][j];
        {
          int l = j;
          while(l-1>=0 && f[i][l-1] == c) l--;
          int r = j;
          while(r+1<w && f[i][r+1] == c) r++;
          if(r-l+1>=n) {
            for(int k=l; k<=r; k++) era[i][k] = true;
            update = true;
          }
        }
        //縦方向
        {
          int l = i;
          while(l-1>=0 && f[l-1][j] == c) l--;
          int r = i;
          while(r+1<h && f[r+1][j] == c) r++;
          if(r-l+1>=n) {
            for(int k=l; k<=r; k++) era[k][j] = true;
            update = true;
          }
        }
      }
    }
    
    vector<string> nf(h, string(w, '.'));
    for(int j=0; j<w; j++) {
      int ptr = h-1;
      for(int i=h-1; i>=0; i--) {
        if(era[i][j]) continue;
        if(f[i][j] == '.') continue;
        nf[ptr][j] = f[i][j];
        ptr--;
      }
    }
    f = nf;
  }

  REP(i,h) REP(j,w) if(f[i][j] != '.') return false;
  return true;
}
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(7);
  
  cin >> h >> w >> n;

  vector<string> f(h);
  REP(i,h) cin >> f[i];

  bool ok = false;
  REP(i,h) {
    REP(j,w) {

      {
        int ni = i, nj = j+1;
        if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj] != '.') {
          swap(f[i][j], f[ni][nj]);
          ok |= check(f);
          swap(f[i][j], f[ni][nj]);
        }
      }
      {
        int ni = i, nj = j-1;
        if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj] != '.') {
          swap(f[i][j], f[ni][nj]);
          ok |= check(f);
          swap(f[i][j], f[ni][nj]);
        }
      }
    }
  }
  cout << (ok?"YES":"NO") << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3508
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

int h,w,n;

bool f(vector<string> s){
  REP(j,w)for(int i=h-2;i>=0;i--){
    int now=i;
    while(s[now][j]!='.'&&now+1<h&&s[now+1][j]=='.'){
      swap(s[now][j],s[now+1][j]);
      now++;
    }
  }
  while(true){
    bool c=false;
    vector<vector<bool>> tmp(h,vector<bool>(w,false));
    REP(i,h)REP(j,w-n+1){
      bool ok=true;
      REP(k,n)ok&=s[i][j]!='.'&&s[i][j]==s[i][j+k];
      if(ok)REP(k,n)tmp[i][j+k]=c=true;
    }
    REP(i,h-n+1)REP(j,w){
      bool ok=true;
      REP(k,n)ok&=s[i][j]!='.'&&s[i][j]==s[i+k][j];
      if(ok)REP(k,n)tmp[i+k][j]=c=true;
    }
    if(!c)break;
    REP(i,h)REP(j,w)if(tmp[i][j])s[i][j]='.';
    REP(j,w)for(int i=h-2;i>=0;i--){
      int now=i;
      while(s[now][j]!='.'&&now+1<h&&s[now+1][j]=='.'){
        swap(s[now][j],s[now+1][j]);
        now++;
      }
    }
  }
  REP(i,h)REP(j,w)if(s[i][j]!='.')return false;
  return true;
}

int main(){
  cin>>h>>w>>n;
  vector<string> s(h);
  string ans="NO";
  REP(i,h)cin>>s[i];
  REP(i,h)REP(j,w-1){
    swap(s[i][j],s[i][j+1]);
    if(f(s))ans="YES";
    swap(s[i][j],s[i][j+1]);
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 6

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
 
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
 
#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)
 
// name macro
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T = int>
using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using Tp = tuple<ll,ll,ll>;
 
// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
	for(T &i : v) is >> i;
	return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
	std::string s;
	is >> s;
	__int128_t ret = 0;
	for(int i = 0; i < s.length(); i++)
		if('0' <= s[i] and s[i] <= '9')
			ret = 10 * ret + s[i] - '0';
	a = ret * (s[0] == '-' ? -1 : 1);
	return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
	std::vector<type> name(size); \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
	std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
	std::string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
 
// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
	for(int i = 0; i < int(a.size()); ++i) {
		if(i) os << " ";
		os << a[i];
	}
	return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
	std::ostream::sentry s(dest);
	if(s) {
		__uint128_t tmp = value < 0 ? -value : value;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while(tmp != 0);
		if(value < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if(dest.rdbuf()->sputn(d, len) != len) {
			dest.setstate(std::ios_base::badbit);
		}
	}
	return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
	std::cout << H << ' ';
	print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
	for(const auto &v : a)
		std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
	std::cout << H << '\n';
	printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "No\n"; }
void err(const bool b = true) {
	if(b) {
		std::cout << "-1\n", exit(0);
	}
}
 
//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
	std::cerr << "{ ";
	for(const auto &v : a) {
		std::cerr << v << ", ";
	}
	std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
	std::cerr << "{\n";
	for(const auto &v : a) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
	std::cerr << "{\n";
	for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
	std::cerr << "{ ";
	for(auto &v : s) {
		view(v);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}
 
template <class T>
void view(const T &e) { std::cerr << e; }
}  // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	debugger::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(false)
#else
#define debug(...) (void(0))
#endif
 
// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
	for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
	for(auto &v : a) v /= x;
}
 
// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}

template<typename T>
T ADD(T a, T b){
	T res;
	return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

template<typename T>
T MUL(T a, T b){
	T res;
	return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

// modpow
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}
 
// others
struct fast_io {
	fast_io() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} fast_io_;

#pragma endregion

using R = long double;
constexpr R EPS=1E-11;

// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const R& r){ return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const R& a, const R &b){ return sgn(a-b); }
// a > 0 は sgn(a) > 0
// a < b は sgn(a, b) < 0
// a >= b は sgn(a, b) >= 0
// のように書く.
// return s * 10^n

//https://atcoder.jp/contests/abc191/submissions/20028529
long long x10(const string& s, size_t n) {
    if (s.front() == '-') return -x10(s.substr(1), n);
    auto pos = s.find('.');
    if (pos == string::npos) return stoll(s + string(n, '0'));
    return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));
}
 
long long ceildiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return (a + b - 1) / b;
    else return a / b;
}
 
long long floordiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return a / b;
    else return (a - b + 1) / b;
}
 
long long floorsqrt(long long x) {
    assert(x >= 0);
    long long ok = 0;
    long long ng = 1;
    while (ng * ng <= x) ng <<= 1;
    while (ng - ok > 1) {
        long long mid = (ng + ok) >> 1;
        if (mid * mid <= x) ok = mid;
        else ng = mid;
    }
    return ok;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}

const int inf = 1e9;
const ll INF = 1e18;

void main_() {
	ll h,w,n;
	cin >> h >> w >> n;
	VVEC(char,a,h,w);
	bool ok = false;
	REP(i,h){
	    REP(j,w){
	        
	        {
	            VV<char> b = a;
	            VV<ll> d(h,V<ll>(w));
	            if(j + 1 < w){
	                swap(b[i][j],b[i][j+1]);
	                bool ex = false;
	                bool first = false;
	                while(true){
	                    ex = false;
	                    bool del = false;
	                    REP(p,h){
	                        if(!first)break;
	                        char now = '0';
	                        ll len = 0;
	                        ll st = 0, en = 0;
	                        REP(q,w){
	                            if(now != b[p][q]){
	                                if(len >= n){
	                                    for(ll k = st;k < en;k++){
	                                        d[p][k] = 1;
	                                    }
	                                    del = true;
	                                }
	                                st = q;
	                                now = b[p][q];
	                                len = 1;
	                                if(now == '.'){
	                                    now = '0';
	                                    len = 0;
	                                }
	                            }
	                            else{
	                                len++;
	                            }
	                            en++;
	                        }
	                        if(len >= n){
	                            for(ll k = st;k < en;k++){
	                                d[p][k] = 1;
	                                del = true;
	                            }
	                        }
	                    }
	                    REP(p,w){
	                        if(!first)break;
	                        char now = '0';
	                        ll len = 0;
	                        ll st = 0, en = 0;
	                        REP(q,h){
	                            if(now != b[q][p]){
	                                if(len >= n){
	                                    for(ll k = st;k < en;k++){
	                                        d[k][p] = 1;
	                                    }
	                                    del = true;
	                                }
	                                st = q;
	                                now = b[q][p];
	                                len = 1;
	                                if(now == '.'){
	                                    now = '0';
	                                    len = 0;
	                                }
	                            }
	                            else{
	                                len++;
	                            }
	                            en++;
	                        }
	                        if(len >= n){
	                            for(ll k = st;k < en;k++){
	                                d[k][p] = 1;
	                                del = true;
	                            }
	                        }
	                    }
	                    REP(p,h)REP(q,w){
	                        if(d[p][q])b[p][q] = '.';
	                        d[p][q] = 0;
	                    }
	                    REP(p,w){
	                        for(ll q = h-1;q >= 0;q--){
	                            if(b[q][p] == '.'){
	                                for(ll k = q-1;k >= 0;k--){
	                                    if(b[k][p] != '.'){
	                                        swap(b[q][p],b[k][p]);
	                                        break;
	                                    }
	                                }
	                            }
	                        }
	                    }
	                    REP(p,h)REP(q,w)if(b[p][q] != '.')ex = true;
	                   
	                    if(!first)del =true;
	                    first = true;
	                    if(!del)break;
	                }
	                
	                if(!ex)ok = true;
	            }
	        }
	        
	    }
	}
	if(ok)cout << "YES" << endl;
	else cout << "NO" << endl;
}
 
int main() {
	int t = 1;
	//cin >> t;
	while(t--) main_();
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3492
final_rank: 9

code_snippet: //たくさん誤読しちゃいました
#include <bits/stdc++.h>
using namespace std;

int main() {
    int H, W, N;
    cin >> H >> W >> N;
    vector S(H, vector<char>(W));
    for (int i = H - 1; i >= 0; i--) {
        for (int j = 0; j < W; j++) {
            cin >> S[i][j];
        }
    }
    const int dx[2] = {0, 1}, dy[2] = {1, 0};
    auto maine = [&](vector<vector<char>> now) {
        vector nxt(H, vector(W, '.'));
        for (int y = 0; y < W; y++) {
            int cnt = 0;
            for (int x = 0; x < H; x++) {
                if (now[x][y] != '.')
                    nxt[cnt++][y] = now[x][y];
            }
        }
        swap(nxt, now);
        while (true) {
            vector book(H, vector(W, false));
            for (int x = 0; x < H; x++) {
                for (int y = 0; y < W; y++) {
                    auto c = now[x][y];
                    if (c == '.')
                        continue;
                    for (int i = 0; i < 2; i++) {
                        if (x + dx[i] * N <= H && y + dy[i] * N <= W) {
                            bool flag = true;
                            for (int j = 0; j < N; j++) {
                                if (c != now[x + dx[i] * j][y + dy[i] * j])
                                    flag = false;
                            }
                            if (flag) {
                                for (int j = 0; j < N; j++) {
                                    book[x + dx[i] * j][y + dy[i] * j] = true;
                                }
                            }
                        }
                    }
                }
            }
            vector nxt(H, vector(W, '.'));
            for (int y = 0; y < W; y++) {
                int cnt = 0;
                for (int x = 0; x < H; x++) {
                    if (now[x][y] != '.' && !book[x][y])
                        nxt[cnt++][y] = now[x][y];
                }
            }

            if (now == nxt)
                break;
            swap(nxt, now);
        }
        for (auto&& V : now) {
            for (auto&& c : V) {
                if (c != '.')
                    return false;
            }
        }
        return true;
    };

    for (int x = 0; x < H; x++) {
        for (int y = 0; y < W; y++) {
            int xx = x, yy = y + 1;
            if (xx < H && yy < W) {
                swap(S[x][y], S[xx][yy]);
                if (maine(S)) {
                    cout << "YES" << endl;
                    return 0;
                }
                swap(S[x][y], S[xx][yy]);
            }
        }
    }
    cout << "NO" << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3464
final_rank: 5