#include "bits/stdc++.h"
 
using namespace std;
 
using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;
 
int H, W, N;
vector<string> v;
vector<vector<bool>> used(30, vector<bool>(30, false));
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
 
bool fall()
{
    bool f = false;
    for (int i = H - 2; i >= 0; i--)
    {
        for (int j = 0; j < W; j++)
        {
            if (v[i][j] == '.')
                continue;
            int x = i;
            while (x <= H - 2 && v[x + 1][j] == '.')
            {
                f = true;
                swap(v[x][j], v[x + 1][j]);
                x++;
            }
        }
    }
    return f;
}
 
bool erase()
{
    bool f = false;
    vector<vector<bool>> e(H, vector<bool>(W, false));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j <= W - N; j++)
        {
            if (v[i][j] == '.')
                continue;
            int cnt = 0;
            for (int k = 0; j + k < W; k++)
            {
                if (v[i][j + k] != v[i][j])
                    break;
                cnt++;
            }
            if (cnt < N)
                continue;
            f = true;
            for (int k = 0; k < cnt; k++)
            {
                e[i][j + k] = true;
            }
        }
    }
    for (int i = 0; i <= H - N; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (v[i][j] == '.')
                continue;
            int cnt = 0;
            for (int k = 0; i + k < H; k++)
            {
                if (v[i + k][j] != v[i][j])
                    break;
                cnt++;
            }
            if (cnt < N)
                continue;
            f = true;
            for (int k = 0; k < cnt; k++)
            {
                e[i + k][j] = true;
            }
        }
    }
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (e[i][j])
                v[i][j] = '.';
        }
    }
    return f;
}
 
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
 
    cin >> H >> W >> N;
    vector<string> tmp(H);
    for (int i = 0; i < H; i++)
        cin >> tmp[i];
    /*
    v = tmp;
    swap(v[3][2], v[3][3]);
    for(int i=0;i<H;i++){for(int j=0;j<W;j++){cout<<v[i][j];}cout<<endl;};
    cout<<endl;
    erase();
    for(int i=0;i<H;i++){for(int j=0;j<W;j++){cout<<v[i][j];}cout<<endl;};
    cout<<endl;
    fall();
    for(int i=0;i<H;i++){for(int j=0;j<W;j++){cout<<v[i][j];}cout<<endl;};
    cout<<endl;
    return 0;
*/
    bool f = false;
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W - 1; j++)
        {
            if (tmp[i][j] == tmp[i][j + 1])
                continue;
            v = tmp;
            swap(v[i][j], v[i][j + 1]);
            bool update = true;
            while (update)
            {
                update = fall();
                update |= erase();
                update |= fall();
            }
            bool cl = true;
            for (int h = 0; h < H; h++)
            {
                for (int w = 0; w < W; w++)
                {
                    if (v[h][w] != '.')
                        cl = false;
                }
            }
            if (cl)
                f = true;
        }
    }
    if (f)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}


Measured memory_bytes: 3240