//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(cnt<(n*m)&&tl<=35)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(st[j][k]=='.'&&b[j][k]<=s[i])
                        st[j][k]=ch;
                    if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,val) memset(x,val,sizeof(x))
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD = 1000000007;
//const int INF =1010000000;
#define int long long int
const ll INF = 0x3f3f3f3f3f3f3f3f;

int32_t main()
{
    IOS;
    int n,m;int p;
    cin>>n>>m>>p;
    string st[n];
   // int a[n][m];
   // memset(,0,sizeof(a));
    int s[p+1]; int cnt=0;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=0;i<n;i++)
        cin>>st[i];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(st[i][j]!='.')
                cnt++;
    char ch;int i1,j1;pair<int,int>p1;int tl=0;
    while(cnt<(n*m)&&tl<=40)
    {
        for(int i=1;i<=p;i++)
        {
            int b[n][m];bool visited[n][m];
            memset(visited,0,sizeof(visited));
            //memset(b,INF,sizeof(b));
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    b[j][k]=INF;
            ch=(char)(i+'0');
            // cout<<ch<<endl;
            queue<pair<int,int> >q;
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    if(st[j][k]==ch)
                    { b[j][k]=0;
                        q.push(m1(j,k));
                        visited[j][k]=1;
                       // if(i==2)
                        //{
                       //     cout<<j<<" "<<k<<endl;
                       // }
                    }

            while(!q.empty())
            {
                p1=q.front();
                q.pop();
                i1=p1.first;j1=p1.second;
                if(i1>=1&&(!visited[i1-1][j1])&&(st[i1-1][j1]=='.'||st[i1-1][j1]==ch))
                {
                    b[i1-1][j1]=1+b[i1][j1];
                    visited[i1-1][j1]=1;
                    q.push(m1(i1-1,j1));
                }
                if(j1>=1&&(!visited[i1][j1-1])&&(st[i1][j1-1]=='.'||st[i1][j1-1]==ch))
                {
                    b[i1][j1-1]=1+b[i1][j1];
                    visited[i1][j1-1]=1;
                    q.push(m1(i1,j1-1));
                }
                if(i1<n-1&&(!visited[i1+1][j1])&&(st[i1+1][j1]=='.'||st[i1+1][j1]==ch))
                {
                    b[i1+1][j1]=1+b[i1][j1];
                    visited[i1+1][j1]=1;
                    q.push(m1(i1+1,j1));
                }
                if(j1<m-1&&(!visited[i1][j1+1])&&(st[i1][j1+1]=='.'||st[i1][j1+1]==ch))
                {
                    b[i1][j1+1]=1+b[i1][j1];
                    visited[i1][j1+1]=1;
                    q.push(m1(i1,j1+1));
                }
            }
            cnt=0;ch=i+'0';
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {

                    if(b[j][k]<=s[i])
                        st[j][k]=ch;
                   if(st[j][k]!='.')
                        cnt++;
                }
            }
            if(cnt>=(n*m))
                break;

        }
        tl++;
    }
    int ans[p+1];
    for(int i=1;i<=p;i++)
        ans[i]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(st[i][j]!='#'&&st[i][j]!='.')
            {
                ans[st[i][j]-'0']++;
            }
        }
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15];
char ch[1005][1005];
int mn[1005][1005];
int d[10][1005][1005], id[1005][1005];
queue <pair<int, int>> q[10];

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void bfs(int id) {
    while (!q[id].empty()) {
        int x = q[id].front().first, y = q[id].front().second;
        q[id].pop();
        for (int i = 0; i < 4; i++) {
            if (!is(x + tx[i], y + ty[i]))
                continue;
            if (d[id][x][y] + 1 < d[id][x + tx[i]][y + ty[i]]) {
                q[id].push({x + tx[i], y + ty[i]});
                d[id][x + tx[i]][y + ty[i]] = d[id][x][y] + 1;
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            for (int k = 1; k <= p; k++) {
                d[k][i][j] = INF;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            mn[i][j] = INF;
            if (isdigit(ch[i][j])) {
                q[ch[i][j] - '0'].push({i, j});
                d[ch[i][j] - '0'][i][j] = 0;
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        bfs(i);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            for (int k = 1; k <= p; k++) {
                if (ch[i][j] == '#')
                    continue;
                if (d[k][i][j] == INF)
                    continue;
                if (d[k][i][j] / s[k] + (d[k][i][j] % s[k] != 0) < mn[i][j]) {
                    mn[i][j] = d[k][i][j] / s[k] + (d[k][i][j] % s[k] != 0);
                    id[i][j] = k;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ct[id[i][j]]++;
        }
    }
    for (int i = 1; i <= p; i++) {
        cout << ct[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10], t[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        if (clc == 0) {
            continue;
        }
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            q.push({cx, cy});
            d[cx][cy] = stp;
            id[cx][cy] = id[x][y];
            ans[id[x][y]]++;
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        vec.clear();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                t[ch[i][j] - '0'].push_back({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        for (auto it : t[i])
            q.push(it);
    }
    bfs();
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10], t[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        if (b.front().second == 0)
            return;
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            vec.push_back({cx, cy});
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        vec.clear();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
        for (auto to : vec) {
            if (d[to.first][to.second] <= d[x][y] + 1)
                continue;
            d[to.first][to.second] = d[x][y] + 1;
            id[to.first][to.second] = id[x][y];
            ans[id[x][y]]++;
            q.push({to.first, to.second});
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                t[ch[i][j] - '0'].push_back({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        for (auto it : t[i])
            q.push(it);
    }
    bfs();
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int it[1005][1005];
bool us;
queue <pair<pair<int, int>, int>> q;
vector <pair<int, int>> g[10];

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void bfs(int id, int ct) {
    int rs = g[id].size();
    for (auto it : g[id]) {
        q.push({it, 0});
    }
    ans[id] -= g[id].size();
    g[id].clear();
    while (!q.empty()) {
        int x = q.front().first.first, y = q.front().first.second;
        int dist = q.front().second;
        q.pop();
        ans[id]++;
        if (dist == s[id]) {
            g[id].push_back({x, y});
            continue;
        }
        for (int i = 0; i < 4; i++) {
            if (!is(x + tx[i], y + ty[i]))
                continue;
            int cx = x + tx[i], cy = y + ty[i];
            if (it[cx][cy])
                continue;
            it[cx][cy] = it[x][y];
            q.push({{cx, cy}, dist + 1});
        }
    }
    if (g[id].size() != 0)
        us = 1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                ans[ch[i][j] - '0']++;
                g[ch[i][j] - '0'].push_back({i, j});
                it[i][j] = ch[i][j] - '0';
            }
        }
    }
    int ct = 0;
    while (1) {
        ct++;
        us = 0;
        for (int i = 1; i <= p; i++) {
            bfs(i, ct);
        }
        if (!us)
            break;
    }
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        if (b.front().second == 0)
            return;
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            vec.push_back({cx, cy});
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        vec.clear();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
        for (auto to : vec) {
            d[to.first][to.second] = d[x][y] + 1;
            id[to.first][to.second] = id[x][y];
            ans[id[x][y]]++;
            q.push({to.first, to.second});
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                q.push({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    bfs();
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10], t[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        if (clc == 0) {
            q.push({x, y});
            continue;
        }
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            if (id[x][y] != 0)
                continue;
            d[cx][cy] = stp;
            id[cx][cy] = id[x][y];
            ans[id[x][y]]++;
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                t[ch[i][j] - '0'].push_back({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        for (auto it : t[i])
            q.push(it);
    }
    bfs();
//    for (int i = 1; i <= n; i++) {
//        for (int j = 1; j <= m; j++) {
//            cout << id[i][j];
//        }
//        cout << "\n";
//    }
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}
/**
4 4 4
1 2 3 8
2..3
....
....
1..4
2223
2233
1133
1144
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10], t[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        if (clc == 0) {
            q.push({x, y});
            continue;
        }
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            if (id[cx][cy] != 0)
                continue;
            d[cx][cy] = stp;
            id[cx][cy] = id[x][y];
            ans[id[x][y]]++;
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                t[ch[i][j] - '0'].push_back({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        for (auto it : t[i])
            q.push(it);
    }
    bfs();
//    for (int i = 1; i <= n; i++) {
//        for (int j = 1; j <= m; j++) {
//            cout << id[i][j];
//        }
//        cout << "\n";
//    }
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}
/**
4 4 4
1 2 3 8
2..3
....
....
1..4
2223
2233
1133
1144
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int dp[1005][1005];
bool us;
queue <pair<int, int>> q;
vector <pair<int, int>> g[10];

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void bfs(int id, int ct) {
    int rs = g[id].size();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (auto it : g[id]) {
        q.push(it);
        d[it.first][it.second] = 0;
    }
    ans[id] -= g[id].size();
    g[id].clear();
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        ans[id]++;
        if (d[x][y] == s[id]) {
            g[id].push_back({x, y});
            continue;
        }
        for (int i = 0; i < 4; i++) {
            if (!is(x + tx[i], y + ty[i]))
                continue;
            if (d[x][y] + 1 < d[x + tx[i]][y + ty[i]] && dp[x + tx[i]][y + ty[i]] > ct) {
                q.push({x + tx[i], y + ty[i]});
                d[x + tx[i]][y + ty[i]] = d[x][y] + 1;
                dp[x + tx[i]][y + ty[i]] = ct;
            }
        }
    }
    if (g[id].size() != 0)
        us = 1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
                dp[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                ans[ch[i][j] - '0']++;
                g[ch[i][j] - '0'].push_back({i, j});
                dp[i][j] = 0;
            }
        }
    }
    int ct = 0;
    while (1) {
        ct++;
        us = 0;
        for (int i = 1; i <= p; i++) {
            bfs(i, ct);
        }
        if (!us)
            break;
    }
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15];
char ch[1005][1005];
int d[1005][1005];
int dp[1005][1005];
bool us;
queue <pair<int, int>> q;
vector <pair<int, int>> g[10];

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void bfs(int id, int ct) {
    int rs = g[id].size();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (auto it : g[id]) {
        q.push(it);
        d[it.first][it.second] = 0;
    }
    g[id].clear();
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        g[id].push_back({x, y});
        if (d[x][y] == s[id])
            continue;
        for (int i = 0; i < 4; i++) {
            if (!is(x + tx[i], y + ty[i]))
                continue;
            if (d[x][y] + 1 < d[x + tx[i]][y + ty[i]] && dp[x + tx[i]][y + ty[i]] > ct) {
                q.push({x + tx[i], y + ty[i]});
                d[x + tx[i]][y + ty[i]] = d[x][y] + 1;
                dp[x + tx[i]][y + ty[i]] = ct;
            }
        }
    }
    if (g[id].size() != rs)
        us = 1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
                dp[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                g[ch[i][j] - '0'].push_back({i, j});
                dp[i][j] = 0;
            }
        }
    }
    int ct = 0;
    while (1) {
        ct++;
        us = 0;
        for (int i = 1; i <= p; i++) {
            bfs(i, ct);
        }
        if (!us)
            break;
    }
    for (int i = 1; i <= p; i++) {
        cout << g[i].size() << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mod = 1e9 + 7;
const int tx[] = {1, -1, 0, 0}, ty[] = {0, 0, 1, -1};

int n, m, p, ct[15];
int s[15], ans[15];
char ch[1005][1005];
int d[1005][1005];
int id[1005][1005];
queue <pair<int, int>> q;
vector <pair<int, int>> g[10], t[10];
vector <pair<int, int>> vec;

bool is(int x, int y) {
    if (x > n || y > m || x <= 0 || y <= 0)
        return 0;
    if (ch[x][y] == '#')
        return 0;
    return 1;
}

void fnd(int x, int y, int ct, int stp) {
    queue<pair<pair<int, int>, int>> b;
    b.push({{x, y}, ct});
    while (!b.empty()) {
        int clc = b.front().second;
        int x = b.front().first.first, y = b.front().first.second;
        b.pop();
        if (clc == 0) {
            q.push({x, y});
            continue;
        }
        for (int i = 0; i < 4; i++) {
            int cx = x + tx[i], cy = y + ty[i];
            if (!is(cx, cy))
                continue;
            if (d[cx][cy] <= stp)
                continue;
            d[cx][cy] = stp;
            id[cx][cy] = id[x][y];
            ans[id[x][y]]++;
            b.push({{cx, cy}, clc - 1});
        }
    }
}

void bfs() {
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        vec.clear();
        fnd(x, y, s[id[x][y]], d[x][y] + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> p;
    for (int i = 1; i <= p; i++) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            d[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch[i][j];
            if (isdigit(ch[i][j])) {
                t[ch[i][j] - '0'].push_back({i, j});
                d[i][j] = 0;
                ans[ch[i][j] - '0']++;
                id[i][j] = ch[i][j] - '0';
            }
        }
    }
    for (int i = 1; i <= p; i++) {
        for (auto it : t[i])
            q.push(it);
    }
    bfs();
    for (int i = 1; i <= p; i++) {
        cout << ans[i] << " ";
    }
}


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

#define fi(i,a,b)   for(long long i=a;i<=b;i++)
#define fr(i,a)     for(long long i=0;i<a;i++)
#define fd(i,a,b)   for(long long i=b;i>=a;i--)
#define clr(x)      memset(x,0,sizeof(x))
#define cln(x)      memset(x,-1,sizeof(x))
#define __          printf(" ")
#define _           printf("\n")
#define _o          printf("1\n")
#define stree       long long lft=node<<1,rht=(node<<1)|1,mid=(s+e)>>1
#define mod         1000000007
#define read()      freopen("in.txt","r",stdin);
#define write()     freopen("out.txt","w",stdin);

main(){
    vector<long long>vec[100010];
    long long n,m,k,ans=0,x;
    scanf("%lld %lld %lld",&n,&m,&k);

    fi(i,1,n){
        scanf("%lld",&x);
        vec[x].push_back(i);
    }

    fi(i,1,m){
        long long Size=vec[i].size(),lft=0,rht=0,mx=0;
        while(rht<Size){
            if(vec[i][rht]-vec[i][lft]+1-k<=rht-lft+1) {
                mx=max(rht-lft+1,mx);
                rht++;
            }
            else lft++;
        }
        ans=max(ans,mx);
    }
    printf("%lld\n",ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

typedef long long int lld;

typedef pair < int, int > pii;

const int MaxN = 200000 + 5;
const int MaxM = 100000 + 5;

int N, M, K, Colors[MaxN], Cnt[MaxM];

int main ( void )
{
    scanf ( "%d %d %d", &N, &M, &K );

    for ( int i = 0; i < N; i++ )
    {
        scanf ( "%d", &Colors[i] );

        Colors[i]--;
    }

    int idx = 0, idy = 0, Sol = 0;

    Cnt[Colors[0]]++;

    while ( idx < N )
    {
        while ( ( idy + 1 < N ) && ( idy - idx + 1 - Cnt[Colors[idx]] <= K ) )
        {
            idy++;

            Cnt[Colors[idy]]++;
        }

        if ( Cnt[Colors[idx]] > Sol ) Sol = Cnt[Colors[idx]];

        Cnt[Colors[idx]]--;

        idx++;
    }

    printf ( "%d\n", Sol );

    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

typedef long long int lld;

typedef pair < int, int > pii;

const int MaxN = 200000 + 5;

int N, M, K, Color;
vector < int > Pos[MaxN];

int main ( void )
{
    scanf ( "%d %d %d", &N, &M, &K );

    for ( int i = 0; i < N; i++ )
    {
        scanf ( "%d", &Color );

        Color--;

        Pos[Color].push_back ( i );
    }

    int Sol = 0;

    for ( int i = 0; i < M; i++ )
    {
        if ( Pos[i].empty ( ) ) continue;

        int idx = 0, Size = Pos[i].size ( );

        if ( Pos[i][Size - 1] - Pos[i][0] - Size + 1 - 0 <= K )
        {
            if ( Size > Sol ) Sol = Size;

            continue;
        }

        while ( idx < Size )
        {
            int idy = idx;

            while ( ( idy < Size ) && ( Pos[i][idy] - Pos[i][idx] - idy + idx <= K ) ) idy++;

            if ( idy - idx + 1 > Sol ) Sol = idy - idx + 1;

            idx = idy;
        }
    }

    printf ( "%d\n", Sol );

    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

typedef long long int lld;

typedef pair < int, int > pii;

const int MaxN = 200000 + 5;

int N, M, K, Color;
vector < int > Pos[MaxN];

int main ( void )
{
    scanf ( "%d %d %d", &N, &M, &K );

    for ( int i = 0; i < N; i++ )
    {
        scanf ( "%d", &Color );

        Color--;

        Pos[Color].push_back ( i );
    }

    int Sol = 0;

    for ( int i = 0; i < M; i++ )
    {
        if ( Pos[i].empty ( ) ) continue;

        int idx = 0, Size = Pos[i].size ( );

        while ( idx < Size )
        {
            int idy = idx;

            while ( idy < Size )
            {
                int Diff = Pos[i][idy] - Pos[i][idx] + 1;
                int Cnt = idy - idx + 1;

                if ( Diff - Cnt > K ) break;

                idy++;
            }

            if ( idy >= Size ) idy--;

            if ( idy - idx + 1 > Sol ) Sol = idy - idx + 1;

            idx = idy + 1;
        }
    }

    printf ( "%d\n", Sol );

    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

typedef long long int lld;

typedef pair < int, int > pii;

const int MaxN = 200000 + 5;

int N, M, K, Color;
vector < int > Pos[MaxN];

int main ( void )
{
    scanf ( "%d %d %d", &N, &M, &K );

    for ( int i = 0; i < N; i++ )
    {
        scanf ( "%d", &Color );

        Color--;

        Pos[Color].push_back ( i );
    }

    int Sol = 0;

    for ( int i = 0; i < M; i++ )
    {
        if ( Pos[i].empty ( ) ) continue;

        int idx = 0, Size = Pos[i].size ( );

        while ( idx < Size )
        {
            int idy = idx;

            while ( idy < Size )
            {
                int DiffPos = Pos[i][idy] - Pos[i][idx] + 1;
                int Cnt = idy - idx + 1;

                if ( DiffPos - Cnt > K ) break;

                idy++;
            }

            if ( idy - idx > Sol ) Sol = idy - idx;

            idx++;
        }
    }

    printf ( "%d\n", Sol );

    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

typedef long long int lld;

typedef pair < int, int > pii;

const int MaxN = 200000 + 5;

int N, M, K, Color;
vector < int > Pos[MaxN];

int main ( void )
{
    scanf ( "%d %d %d", &N, &M, &K );

    for ( int i = 0; i < N; i++ )
    {
        scanf ( "%d", &Color );

        Color--;

        Pos[Color].push_back ( i );
    }

    int Sol = 0;

    for ( int i = 0; i < M; i++ )
    {
        if ( Pos[i].empty ( ) ) continue;

        int idx = 0, Size = Pos[i].size ( );

        while ( idx < Size )
        {
            int idy = idx;

            while ( idy < Size )
            {
                int DiffPos = Pos[i][idy] - Pos[i][idx] + 1;
                int Cnt = idy - idx + 1;

                if ( DiffPos - Cnt > K ) break;

                idy++;
            }

            int Remove = 0;

            if ( idy == Size ) Remove = 1;

            if ( idy - idx + 1 - Remove > Sol ) Sol = idy - idx + 1 - Remove;

            idx = idy;
        }
    }

    printf ( "%d\n", Sol );

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MM 110000

vector<int>num[MM];

int n, m, k, t, ans;

int main()

{

//	freopen("in.txt", "r", stdin);

	scanf ("%d%d%d", &n, &m, &k);

	for (int i=1; i<=n; i++) scanf("%d", &t), num[t].push_back(i);

	for (int i=1; i<=m; i++){

		t=0;

		for(int j=0; j<num[i].size();j++){

			while(num[i][j]-num[i][t]>j-t+k)t++;

			ans=max(ans, j-t+1);

		}

	}

	cout<<ans<<endl;

}


#include<bits/stdc++.h>

#define x first

#define y second

#define NN 200011

using namespace std;

typedef pair<int, int> pii;

pii col[NN];

int main(){

	int n,i,j,m,k,pos;

	cin>>n>>m>>k;

	for(i=0; i<n; i++) {

		scanf("%d", &col[i].x);

		col[i].y=i;

	}

	sort(col, col+n);

	int ans=0;

	for(i=0; i<n; ){

		for(j=i; j<n; j++) if(col[j].x!=col[i].x) break;

		pos=j-1;

		for(j=i; j<=pos; j++){

			int st=j, ed=pos, mid;

			while(ed>st+1){

				mid=(st+ed)/2;

				if(col[mid].y-col[j].y>mid-j+k) ed=mid;

				else st=mid;

			}

			if(col[ed].y-col[j].y<=ed-j+k) st=ed;

			ans=max(ans, st-j+1);

		}

		i=pos+1;

	}

	cout<<ans<<endl;

	return 0;

}


#include <bits/stdc++.h>

#define x first

#define y second

#define eps 1e-9

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define sq(_) ((_)*(_))

#define cb(_) ((_)*(_)*(_))

#define bit(x, y) (((x)>>(y))&1)

#define bctz(x) (__builtin_ctz(x))

#define bclz(x) (__builtin_clz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define bclzl(x) (__builtin_clzll(x))

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T> inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}

template<class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1), gn(x2), gn(x3);}

template<class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1), gn(x2), gn(x3), gn(x4);}

template<class T> inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T> inline void printsp(T x) {print(x); putchar(' ');}

template<class T> inline void println(T x) {print(x); putchar('\n');}

template<class T1, class T2> inline void print(T1 x1, T2 x2){printsp(x1), println(x2);}

template<class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3){printsp(x1), printsp(x2), println(x3);}

template<class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4){printsp(x1), printsp(x2), printsp(x3), println(x4);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#ifndef ONLINE_JUDGE

void debug() {cout << endl;}

template<typename T, typename... Args>void debug(T&& head, Args&&... tail) {cout << head << ' ';debug(tail...);}

#endif

#define NN 100010

VI pos[NN];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m, k, x;

	gn(n, m, k);

	for(int i=0; i<n; i++) {

		gn(x);

		pos[x].pb(i);

	}

	int ans=0;

	for(int i=1; i<=m; i++) {

		int id=0;

		for(int j=0; j<pos[i].size(); j++) {

			while(pos[i][j]-pos[i][id]>j-id+k) {

				id++;

			}

			smax(ans, j-id+1);

		}

	}

	println(ans);

}


#include<bits/stdc++.h>

#define NN 100011

using namespace std;

vector<int> col[NN];

int main(){

	int n,i,j,m,k,u;

	cin>>n>>m>>k;

	for(i=0; i<n; i++){

		scanf("%d", &u);

		col[--u].push_back(i);

	}

	int ans=0;

	for(i=0; i<m; i++){

		int pos=0;

		for(j=0; j<col[i].size(); j++){

			while(col[i][j]-col[i][pos]>j-pos+k) pos++;

			ans=max(ans, j-pos+1);

		}

	}

	cout<<ans<<endl;

	return 0;

}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

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

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

int main()
{
	int n, m, k, x;
	GET3(n, m, k);
	vector<vector<int> > V(m+1);

	FOR (i, 1, n) {
		GET(x);
		V[x].PB(i);
	}

	int best = 1;

	EACH (v, V) {
		int p, f = 1;
		int a = 0;
		int uk = 0;
		deque<int> d;
		deque<int> A;
		EACH (i, *v) {
			if (f) {
				f = 0;
				a = 1;
				p = *i;
				continue;
			}
			if (p + 1 == *i) {
				remax(best, ++a);
			} else {
				d.PB(*i - (p + 1));
				A.PB(A.empty()?a:a-A.back());
				uk += d.back();
				while (uk > k) {
					uk -= d.front();
					a -= A.front();
					d.pop_front();
					A.pop_front();
				}

				remax(best, ++a);
			}
			p = *i;
		}
	}

	printf("%d\n", best);
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

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

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

int main()
{
	int n, m, k, x;
	GET3(n, m, k);
	vector<vector<int> > V(m+1);

	FOR (i, 1, n) {
		GET(x);
		V[x].PB(i);
	}

	int best = 1;

	EACH (v, V) {
		int p, f = 1;
		int a = 0;
		int uk = 0;
		deque<int> d;
		deque<int> A;
		EACH (i, *v) {
			if (f) {
				f = 0;
				a = 1;
				p = *i;
				continue;
			}
			if (p + 1 == *i) {
				remax(best, ++a);
			} else {
				d.PB(*i - (p + 1));
				A.PB(a);
				uk += d.back();
				while (uk > k) {
					uk -= d.front();
					a -= A.front() - a;
					d.pop_front();
					A.pop_front();
				}

				remax(best, ++a);
			}
			p = *i;
		}
	}

	printf("%d\n", best);
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

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

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

int main()
{
	int n, m, k, x;
	GET3(n, m, k);
	vector<vector<int> > V(m+1);

	FOR (i, 1, n) {
		GET(x);
		V[x].PB(i);
	}

	int best = 1;

	EACH (v, V) {
		int p, f = 1;
		int a = 0;
		int uk = 0;
		deque<int> d;
		deque<int> A;
		EACH (i, *v) {
			if (f) {
				f = 0;
				a = 1;
				p = *i;
				continue;
			}
			if (p + 1 == *i) {
				remax(best, ++a);
			} else {
				d.PB(*i - (p + 1));
				A.PB(a);
				uk += d.back();
				while (uk > k) {
					uk -= d.front();
					a -= A.front();
					d.pop_front();
					A.pop_front();
				}
				remax(best, ++a);
			}
			p = *i;
		}
	}

	printf("%d\n", best);
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

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

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

int main()
{
	int n, m, k, x;
	GET3(n, m, k);
	vector<vector<int> > V(m+1);

	FOR (i, 1, n) {
		GET(x);
		V[x].PB(i);
	}

	int best = 1;

	EACH (v, V) {
		int p, f = 1;
		int a = 0;
		int uk = 0;
		deque<int> d;
		deque<int> A;
		EACH (i, *v) {
			if (f) {
				f = 0;
				a = 1;
				A.PB(1);
				p = *i;
				continue;
			}
			if (p + 1 == *i) {
				++A.back();
				remax(best, ++a);
			} else {
				d.PB(*i - (p + 1));
				uk += d.back();
				while (uk > k) {
					uk -= d.front();
					a -= A.front();
					d.pop_front();
					A.pop_front();
				}
				A.PB(1);
				remax(best, ++a);
			}
			p = *i;
		}
	}

	printf("%d\n", best);
}


#include <cstdio>
#include <cstdlib>

struct node
{
  int type;
  int index; //ÔÀ´Î»ÖÃ
};

node data[1001000];

int cmp(const void* a,const void* b)
{
  if(((node*)a)->type != ((node*)b)->type)
  {
   return ((node*)a)->type - ((node*)b)->type;
  }
  else
    return ((node*)a)->index - ((node*)b)->index;
}

int main()
{
   int N,M,K,i,j;
   scanf("%d%d%d",&N,&M,&K);
   for(i=0;i<N;i++)
   {
     scanf("%d",&data[i].type);
     data[i].index = i;
   }
   qsort(data,N,sizeof(node),cmp);

   int ans = 1;
   int prevj,prevdist;
   for(i=0;i<N;i++)
   {
     int dist = 0;
     j=i+1;
     if(i>0 && data[i].type == data[i-1].type)
     {
       j = prevj;
       dist = prevdist;
       //printf("%d %d\n",j,dist);
     }
     for(;data[j].type==data[i].type&&j<N;j++)
     {
       int tmp = (data[j].index - data[j-1].index-1);
       if(dist+tmp>K)
        break;
       else
        dist+=tmp;
     }
     prevj = j-1;
     prevdist = dist-(data[i].index-data[i-1].index+1);
     if(ans < j-i)
      ans = j-i;
   }

   printf("%d\n",ans);
   //scanf("%d",&i);
   return 0;
}


#include <cstdio>
#include <cstdlib>

struct node
{
  int type;
  int index; //ÔÀ´Î»ÖÃ
};

node data[100100];

int cmp(const void* a,const void* b)
{
  if(((node*)a)->type != ((node*)b)->type)
  {
   return ((node*)a)->type - ((node*)b)->type;
  }
  else
    return ((node*)a)->index - ((node*)b)->index;
}

int main()
{
   int N,M,K,i,j;
   scanf("%d%d%d",&N,&M,&K);
   for(i=0;i<N;i++)
   {
     scanf("%d",&data[i].type);
     data[i].index = i;
   }
   qsort(data,N,sizeof(node),cmp);

   int ans = 1;
   for(i=0;i<N;i++)
   {
     int dist = 0;
     for(j=i+1;data[j].type==data[i].type&&j<N;j++)
     {
       int tmp = (data[j].index - data[j-1].index-1);
       if(dist+tmp>K)
        break;
       else
        dist+=tmp;
     }
     if(ans < j-i)
      ans = j-i;
   }

   printf("%d\n",ans);
  // scanf("%d",&i);
   return 0;
}


#include <cstdio>
#include <cstdlib>

struct node
{
  int type;
  int index; //ÔÀ´Î»ÖÃ
};

node data[1001000];

int cmp(const void* a,const void* b)
{
  if(((node*)a)->type != ((node*)b)->type)
  {
   return ((node*)a)->type - ((node*)b)->type;
  }
  else
    return ((node*)a)->index - ((node*)b)->index;
}

int main()
{
   int N,M,K,i,j;
   scanf("%d%d%d",&N,&M,&K);
   for(i=0;i<N;i++)
   {
     scanf("%d",&data[i].type);
     data[i].index = i;
   }
   qsort(data,N,sizeof(node),cmp);

   int ans = 1;
   int prevj,prevdist;
   for(i=0;i<N;i++)
   {
     int dist = 0;
     j=i+1;
     if(i>0 && data[i].type == data[i-1].type)
     {
       j = prevj;
       dist = prevdist;
     }

     for(;data[j].type==data[i].type&&j<N;j++)
     {
       int tmp = (data[j].index - data[j-1].index-1);
       if(dist+tmp>K)
        break;
       else
        dist+=tmp;
     }
     prevj = j;
     prevdist = dist-(data[i+1].index-data[i].index-1);
     if(ans < j-i)
      ans = j-i;
   }

   printf("%d\n",ans);
   //scanf("%d",&i);
   return 0;
}


#include <cstdio>
#include <cstdlib>

struct node
{
  int type;
  int index; //ÔÀ´Î»ÖÃ
};

node data[1001000];

int cmp(const void* a,const void* b)
{
  if(((node*)a)->type != ((node*)b)->type)
  {
   return ((node*)a)->type - ((node*)b)->type;
  }
  else
    return ((node*)a)->index - ((node*)b)->index;
}

int main()
{
   int N,M,K,i,j;
   scanf("%d%d%d",&N,&M,&K);
   for(i=0;i<N;i++)
   {
     scanf("%d",&data[i].type);
     data[i].index = i;
   }
   qsort(data,N,sizeof(node),cmp);

   int ans = 1;
   for(i=0;i<N;i++)
   {
     int dist = 0;
     for(j=i+1;data[j].type==data[i].type&&j<N;j++)
     {
       int tmp = (data[j].index - data[j-1].index-1);
       if(dist+tmp>K)
        break;
       else
        dist+=tmp;
     }
     if(ans < j-i)
      ans = j-i;
   }

   printf("%d\n",ans);
  // scanf("%d",&i);
   return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

//const int MAXM=100*1000+9;

struct val_ind
{
    int val, ind;
};

int solve(val_ind* A, int n, int k)
{
    int result=0;
    int gaps_used=0;

    int i=0, j=i;
    while(i<n)
    {
        while(j+1<n && (A[j+1].ind-A[j].ind-1)+gaps_used<=k)
        {
            gaps_used+=A[j+1].ind-A[j].ind-1;
            j++;
        }

        result=max(result, j-i+1);
        if(i+1>=n) break;

        gaps_used-=A[i+1].ind-A[i].ind;
        i++;
        if(j<i) j=i;
    }

    return result;
}

bool cmp(const val_ind& A, const val_ind& B)
{
    return A.val<B.val || (A.val==B.val && A.ind<B.ind);
}

const int MAXN=2*100*1000+9;
int n, m, k;
val_ind A[MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;
    for(int i=0; i<n; i++)
    {
        cin >> A[i].val;
        A[i].ind=i;
    }

    sort(A, A+n, cmp);
    int iter_L=0, iter_R;

    int result=0;
    while(iter_L<n)
    {
        iter_R=iter_L;
        while(iter_R+1<n && A[iter_R+1].val==A[iter_L].val) iter_R++;

        result=max(result, solve(A+iter_L, iter_R-iter_L+1, k));

        iter_L=iter_R+1;
    }
    cout << result;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

//const int MAXM=100*1000+9;

struct val_ind
{
    int val, ind;
};

int solve(val_ind* A, int n, int k)
{
    int result=0;
    int gaps_used=0;

    int i=0, j=i;
    while(i<n)
    {
        while(j+1<n && (A[j+1].ind-A[j].ind-1)+gaps_used<=k)
        {
            gaps_used+=A[j+1].ind-A[j].ind-1;
            j++;
        }
        //cout << i << ' ' << j << ' ' << gaps_used << endl;

        result=max(result, j-i+1);
        if(i+1>=n) break;

        gaps_used-=(A[i+1].ind-A[i].ind-1);
        i++;
        if(j<i)
        {
            j=i;
            gaps_used=0;
        }
    }

    //cout << "Result: " << result << endl;
    //cout << "val: " << A[0].val << endl;
    return result;
}

bool cmp(const val_ind& A, const val_ind& B)
{
    return A.val<B.val || (A.val==B.val && A.ind<B.ind);
}

const int MAXN=2*100*1000+9;
int n, m, k;
val_ind A[MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;
    for(int i=0; i<n; i++)
    {
        cin >> A[i].val;
        A[i].ind=i;
    }

    sort(A, A+n, cmp);
    int iter_L=0, iter_R;

    int result=0;
    while(iter_L<n)
    {
        iter_R=iter_L;
        while(iter_R+1<n && A[iter_R+1].val==A[iter_L].val) iter_R++;

        result=max(result, solve(A+iter_L, iter_R-iter_L+1, k));

        iter_L=iter_R+1;
    }
    cout << result;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

//const int MAXM=100*1000+9;

struct val_ind
{
    int val, ind;
};

int solve(val_ind* A, int n, int k)
{
    int result=0;
    int gaps_used=0;

    int i=0, j=i;
    while(i<n)
    {

        while(j+1<n && (A[j+1].ind-A[j].ind-1)+gaps_used<=k)
        {
            gaps_used+=A[j+1].ind-A[j].ind-1;
            j++;
        }

        result=max(result, j-i+1);
        if(i+1>=n) break;

        gaps_used-=(A[i+1].ind-A[i].ind-1);
        i++;
        if(j<i) j=i;
    }

    //cout << "Result: " << result;
    return result;
}

bool cmp(const val_ind& A, const val_ind& B)
{
    return A.val<B.val || (A.val==B.val && A.ind<B.ind);
}

const int MAXN=2*100*1000+9;
int n, m, k;
val_ind A[MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;
    for(int i=0; i<n; i++)
    {
        cin >> A[i].val;
        A[i].ind=i;
    }

    sort(A, A+n, cmp);
    int iter_L=0, iter_R;

    int result=0;
    while(iter_L<n)
    {
        iter_R=iter_L;
        while(iter_R+1<n && A[iter_R+1].val==A[iter_L].val) iter_R++;

        result=max(result, solve(A+iter_L, iter_R-iter_L+1, k));

        iter_L=iter_R+1;
    }
    cout << result;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size()-1,mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<=k)
                f=mid+1;
            else
                l=mid-1;
        }
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>80>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size()-1,mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<=k)
                f=mid+1;
            else
                l=mid-1;
        }
        if(su[v][mid]-su[v][i]>k)
            mid--;
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size(),mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<k)
                f=mid+1;
            else if(su[v][mid]-su[v][i]>k)
                l=mid-1;
            else
                break;
        }
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size()-1,mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<=k)
                f=mid+1;
            else
                l=mid-1;
        }
        if(su[v][mid]-su[v][i]>k)
            mid--;
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size()-1,mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<k)
                f=mid+1;
            else if(su[v][mid]-su[v][i]>k)
                l=mid-1;
            else
                break;
        }
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size()-1,mid=(f+l)/2;
        while(f<=l)
        {
            mid=(f+l)/2;
            if(su[v][mid]-su[v][i]<=k)
                f=mid+1;
            else
                l=mid-1;
        }
        if(su[v][mid]-su[v][i]>k)
            mid--;
        best=max(best,mid-i);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
        {

            su[i].push_back(s[i][0].first);
            for(int j=1;j<s[i].size();j++)
                su[i].push_back(su[i][j-1]+s[i][j].first);
        }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size(),mid=(f+l)/2;
        while(f<l-1)
        {
            int mid=(f+l)/2;
            if(su[v][mid]-su[v][f]<k)
                f=mid+1;
            else if(su[v][mid]-su[v][f]>k)
                l=mid-1;
            else
                break;
        }
        best=max(best,f-i+1);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i);
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<bits/stdc++.h>

using namespace std;

const int Maxn=2*1000*100+10;

typedef unsigned long long ull;

int n,m,a[Maxn],k,b[Maxn];
vector<pair<int,int> > s[Maxn];
vector<ull> su[Maxn];

inline void input()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
        cin>>a[i];
}

int find_best(int v)
{
    int best=0;
    for(int i=0;i<s[v].size();i++)
    {
        int f=i,l=s[v].size(),mid=(f+l)/2;
        while(f<l-1)
        {
            int mid=(f+l)/2;
            if(su[v][mid]-su[v][f]<k)
                f=mid+1;
            else if(su[v][mid]-su[v][f]>k)
                l=mid-1;
            else
                break;
        }
        best=max(best,mid-i+1);
    }
    return best;
}

inline void solve()
{
    for(int i=0;i<n;i++)
    {
        int c=a[i]-1;
        if(s[c].size())
            s[c].push_back(make_pair(i-s[c][s[c].size()-1].second-1,i));
        else
            s[c].push_back(make_pair(0,i));
    }
    for(int i=0;i<m;i++)
    {
        su[i].push_back(s[i][0].first);
        for(int j=1;j<s[i].size();j++)
            su[i].push_back(su[i][j-1]+s[i][j].first);
    }
    for(int i=0;i<m;i++)
        if(s[i].size())
            b[i]=find_best(i)+1;
}

int main()
{
    input();
    solve();
    cout<<*max_element(b,b+m);
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()<=ans)
            continue;
        for(j=0;j<ve[i].size()-ans;j++){
            int sum=k,now=1;
            for(p=j+1;p<ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]-1);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(now>ans)
                ans=now;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()<=ans)
            continue;
        p=0;
        for(j=1;j<ve[i].size();j++){
            while(ve[i][j]-ve[i][p]+1-(j-p+1)>k)
                p++;
            if(j-p+1>ans)
                ans=j-p+1;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()==0)
            continue;
        for(j=0;j<ve[i].size();j++){
            int sum=k,now=1;
            for(p=j+1;p<ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]-1);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(now>ans)
                ans=now;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()==0)
            continue;
        for(j=0;j<=ve[i].size();j++){
            int sum=k,now=1;
            for(p=j+1;p<=ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(now>ans)
                ans=now;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
int main(){
    int i,j,p,q,n,m,k,tem;
    vector<int>ve[100100];
    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()==0)
            continue;
        for(j=0;j<=ve[i].size();j++){
            int sum=k,now=1;
            for(p=j+1;p<=ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(sum>ans)
                ans=sum;
        }
    }
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()<=ans)
            continue;
        p=0;
        for(j=ans-1;j<ve[i].size();j++){
            while(ve[i][j]-ve[i][p]+1-(j-p+1)>k)
                p++;
            if(j-p+1>ans)
                ans=j-p+1;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
vector<int> ve[100100];
int main(){
    int i,j,p,q,n,m,k,tem;

    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()<=ans)
            continue;
        for(j=0;j<ve[i].size();j++){
            int sum=k,now=1;
            for(p=j+1;p<ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]-1);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(now>ans)
                ans=now;
        }
    }
    printf("%d\n",ans);
}


#include<cstdio>
#include<vector>
using namespace std;
int main(){
    int i,j,p,q,n,m,k,tem;
    vector<int> ve[100100];
    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&tem);
        ve[tem].push_back(i);
    }
    int ans=1;
    for(i=1;i<=m;i++){
        if(ve[i].size()==0)
            continue;
        for(j=0;j<=ve[i].size();j++){
            int sum=k,now=1;
            for(p=j+1;p<=ve[i].size();p++){
                sum-=(ve[i][p]-ve[i][p-1]);
                if(sum>=0)
                    now++;
                else
                    break;
            }
            if(sum>ans)
                ans=sum;
        }
    }
    printf("%d\n",ans);
}


#include <cstdio>
#include <deque>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

const int N = 400050;

vector<pair<int, int> > vec[N];
deque<pair<int, int> > que;

int main ()
{
#ifdef LOCAL
	freopen("in", "r", stdin);
#endif
	int n, m, k, res(0);
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; ++i)
	{
		int a; scanf("%d", &a);
		if (vec[a].empty()) vec[a].push_back(make_pair(i, i));
		else if (vec[a].back().second == i - 1) ++vec[a].back().second;
		else vec[a].push_back(make_pair(i, i));
	}
	for (int i = 0; i < N; ++i) if (!vec[i].empty())
	{
		que.clear();
		int curk = 0, cur = 0, cd, crd;
		for (int j = 0; j < vec[i].size(); ++j)
		{
			cd = (j == 0) ? 0 : vec[i][j].first - vec[i][j - 1].second - 1;
			crd = vec[i][j].second - vec[i][j].first + 1;
			while (!que.empty() && curk + cd > k)
			{
				curk -= que.front().first;
				cur -= que.front().second;
				que.pop_front();
			}
			if (curk + cd > k)
			{
				curk = cd; cur = crd;
			}
			else
			{
				if (cur)
					que.push_back(make_pair(cd, vec[i][j - 1].second -
						vec[i][j - 1].first + 1));
				cur += crd; curk += cd;
			}
			res = max(res, cur);
		}
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <deque>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

const int N = 400050;

vector<pair<int, int> > vec[N];
deque<pair<int, int> > que;

int main ()
{
#ifdef LOCAL
	freopen("in", "r", stdin);
#endif
	int n, m, k, res(0);
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; ++i)
	{
		int a; scanf("%d", &a);
		if (vec[a].empty()) vec[a].push_back(make_pair(i, i));
		else if (vec[a].back().second == i - 1) ++vec[a].back().second;
		else vec[a].push_back(make_pair(i, i));
	}
	for (int i = 0; i < N; ++i) if (!vec[i].empty())
	{
		que.clear();
		int curk = 0, cur = 0, cd, crd;
		for (int j = 0; j < vec[i].size(); ++j)
		{
			cd = (j == 0) ? 0 : vec[i][j].first - vec[i][j - 1].second - 1;
			crd = vec[i][j].second - vec[i][j].first + 1;
			while (!que.empty() && curk + cd > k)
			{
				curk -= que.front().first;
				cur -= que.front().second;
				que.pop_front();
			}
			if (curk + cd > k)
			{
				curk = 0; cur = crd;
			}
			else
			{
				if (cur)
					que.push_back(make_pair(cd, vec[i][j - 1].second -
						vec[i][j - 1].first + 1));
				cur += crd; curk += cd;
			}
			res = max(res, cur);
		}
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <deque>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

const int N = 400050;

vector<pair<int, int> > vec[N];
deque<pair<int, int> > que;

int main ()
{
#ifdef LOCAL
	freopen("in", "r", stdin);
#endif
	int n, m, k, res(0);
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; ++i)
	{
		int a; scanf("%d", &a);
		if (vec[a].empty()) vec[a].push_back(make_pair(i, i));
		else if (vec[a].back().second == i - 1) ++vec[a].back().second;
		else vec[a].push_back(make_pair(i, i));
	}
	for (int i = 0; i < N; ++i) if (!vec[i].empty())
	{
		que.clear();
		int curk = 0, cur = 0, cd, crd;
		for (int j = 0; j < vec[i].size(); ++j)
		{
			cd = (j == 0) ? 0 : vec[i][j].first - vec[i][j - 1].second - 1;
			crd = vec[i][j].second - vec[i][j].first + 1;
			while (!que.empty() && curk + cd > k)
			{
				curk -= que.front().first;
				cur -= que.front().second;
				que.pop_front();
			}
			if (cur) que.push_back(make_pair(cd, vec[i][j - 1].second - vec[i][j - 1].first + 1));
			cur += crd; curk += cd;
			res = max(res, cur);
		}
	}
	printf("%d\n", res);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const double eps = 1e-8, pi = acos(-1.0), inf = 1e200;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s) - 1; i >= (int)(t); i--)
#define repv(i, v)  for (int i = 0; i < (int)v.size(); i++)
#define repi(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
inline int sgn(double x) { return x < -eps ? -1 : x > eps; }
typedef long long LL;
typedef map<string, int> MSI;
typedef vector<int> VI;
const int MAXN = 111111;
int a[MAXN];
int prev[MAXN], next[MAXN], last[MAXN], first[MAXN];
int l, r;

int main() {
    int n, m, k;
    while (cin >> n >> m >> k) {
        memset(last, -1, sizeof(last));
        rep(i, 0, n) {
            cin >> a[i];
            a[i]--;
            prev[i] = last[a[i]];
            if (last[a[i]] != -1) {
                next[last[a[i]]] = i;
            } else {
                first[a[i]] = i;
            }
            last[a[i]] = i;
        }
        int ans = 1;
        rep(c, 0, m) {
            int l = first[c], r = l, cnt = 0;
            while (l != last[c]) {
                while (r != last[c] && next[r] - l - (cnt + 1) <= k) {
                    r = next[r];
                    cnt++;
                    ans = max(ans, cnt + 1);
                }
                //cout << c << ' ' << l << ' ' << r << ' ' << endl;
                l = next[l];
                cnt--;
            }
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const double eps = 1e-8, pi = acos(-1.0), inf = 1e200;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s) - 1; i >= (int)(t); i--)
#define repv(i, v)  for (int i = 0; i < (int)v.size(); i++)
#define repi(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
inline int sgn(double x) { return x < -eps ? -1 : x > eps; }
typedef long long LL;
typedef map<string, int> MSI;
typedef vector<int> VI;
const int MAXN = 211111;
int a[MAXN];
int prev[MAXN], next[MAXN], last[MAXN], first[MAXN];
int l, r;

int main() {
    int n, m, k;
    while (scanf("%d%d%d", &n, &m, &k) != EOF) {
        memset(last, -1, sizeof(last));
        rep(i, 0, n) {
            scanf("%d", &a[i]);
            a[i]--;
            prev[i] = last[a[i]];
            if (last[a[i]] != -1) {
                next[last[a[i]]] = i;
            } else {
                first[a[i]] = i;
            }
            last[a[i]] = i;
        }
        int ans = 1;
        rep(c, 0, m) {
            if (last[c] == -1) continue;
            int l = first[c], r = l, cnt = 0;
            while (l != last[c]) {
                while (r != last[c] && next[r] - l - (cnt + 1) <= k) {
                    r = next[r];
                    cnt++;
                    ans = max(ans, cnt + 1);
                }
                //cout << c << ' ' << l << ' ' << r << ' ' << endl;
                l = next[l];
                cnt--;
            }
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const double eps = 1e-8, pi = acos(-1.0), inf = 1e200;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s) - 1; i >= (int)(t); i--)
#define repv(i, v)  for (int i = 0; i < (int)v.size(); i++)
#define repi(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
inline int sgn(double x) { return x < -eps ? -1 : x > eps; }
typedef long long LL;
typedef map<string, int> MSI;
typedef vector<int> VI;
const int MAXN = 211111;
int a[MAXN];
int prev[MAXN], next[MAXN], last[MAXN], first[MAXN];
int l, r;

int main() {
    int n, m, k;
    while (cin >> n >> m >> k) {
        memset(last, -1, sizeof(last));
        rep(i, 0, n) {
            cin >> a[i];
            a[i]--;
            prev[i] = last[a[i]];
            if (last[a[i]] != -1) {
                next[last[a[i]]] = i;
            } else {
                first[a[i]] = i;
            }
            last[a[i]] = i;
        }
        int ans = 1;
        rep(c, 0, m) {
            int l = first[c], r = l, cnt = 0;
            while (l != last[c]) {
                while (r != last[c] && next[r] - l - (cnt + 1) <= k) {
                    r = next[r];
                    cnt++;
                    ans = max(ans, cnt + 1);
                }
                //cout << c << ' ' << l << ' ' << r << ' ' << endl;
                l = next[l];
                cnt--;
            }
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <vector>
using namespace std;

#define MAX 100000
int prev[MAX] = {0};
vector<int> cost[MAX];

int main(){
    int n, m, k;
    cin >> n >> m >> k;
    for( int i=1; i<=n; i++ ){
        int color;
        cin >> color;
        color --;
        cost[color].push_back( prev[color]==0?0:i-prev[color]-1 );
        prev[color] = i;
    }
/*
    for( int i=0; i<m; i++ ){
        for( int j=0; j<cost[i].size(); j++ )
            cout << cost[i][j] << ' ';
        cout << endl;
    }
    cout << "+++++++" << endl;
*/
    int max = 1;
    for( int i=0; i<m; i++ ){
        int start = 0, end = 0;
        int current = 1, paid = 0;
        while( end+1 < cost[i].size() ){
            while( end+1 < cost[i].size() && paid+cost[i][end+1]<=k ){
                current++;
                paid += cost[i][end+1];
                end++;
            }
            if( current > max )
                max = current;
            if( end+1 == cost[i].size() ){
                break;
            }else{
                current++;
                paid += cost[i][end+1];
                end++;
                while( paid>k ){
                    current--;
                    start++;
                    paid -= cost[i][start];
                }
            }
        }

    }
    cout << max << endl;
}


#include <iostream>
#include <vector>
using namespace std;

#define MAX 100000
int prev[MAX] = {0};
vector<int> cost[MAX];

int main(){
    int n, m, k;
    cin >> n >> m >> k;
    for( int i=1; i<=n; i++ ){
        int color;
        cin >> color;
        color --;
        cost[color].push_back( prev[color]==0?0:i-prev[color]-1 );
        prev[color] = i;
    }
/*
    for( int i=0; i<m; i++ ){
        for( int j=0; j<cost[i].size(); j++ )
            cout << cost[i][j] << ' ';
        cout << endl;
    }
    cout << "+++++++" << endl;
*/
    int max = 1;
    for( int i=0; i<m; i++ ){
        int start = 0, end = 0;
        int current = 1, paid = 0;
        while( true ){
            while( end+1 < cost[i].size() && paid+cost[i][end+1]<=k ){
                current++;
                paid += cost[i][end+1];
                end++;
            }
            if( current > max )
                max = current;
            if( end+1 == cost[i].size() ){
                break;
            }else{
                while( paid+cost[i][end+1]>k ){
                    current--;
                    start++;
                    paid -= cost[i][start];
                }
            }
        }

    }
    cout << max << endl;
}


#include <iostream>
#include <vector>
using namespace std;

#define MAX 100000
int prev[MAX] = {0};
vector<int> cost[MAX];

int main(){
    int n, m, k;
    cin >> n >> m >> k;
    for( int i=1; i<=n; i++ ){
        int color;
        cin >> color;
        color --;
        cost[color].push_back( prev[color]==0?0:i-prev[color]-1 );
        prev[color] = i;
    }
/*
    for( int i=0; i<m; i++ ){
        for( int j=0; j<cost[i].size(); j++ )
            cout << cost[i][j] << ' ';
        cout << endl;
    }
    cout << "+++++++" << endl;
*/
    int max = 1;
    for( int i=0; i<m; i++ ){
        int start = 0, end = 0;
        int current = 1, paid = 0;
        while( true ){
            while( end+1 < cost[i].size() && paid+cost[i][end+1]<=k ){
                current++;
                paid += cost[i][end+1];
                end++;
            }
            if( current > max )
                max = current;
            if( end+1 == cost[i].size() ){
                break;
            }else{
                current++;
                paid += cost[i][end+1];
                end++;
                while( paid>k ){
                    current--;
                    start++;
                    paid -= cost[i][start];
                }
            }
        }

    }
    cout << max << endl;
}


#include <iostream>
#include <vector>
using namespace std;

#define MAX 100000
int prev[MAX] = {0};
vector<int> cost[MAX];

int main(){
    int n, m, k;
    cin >> n >> m >> k;
    for( int i=1; i<=n; i++ ){
        int color;
        cin >> color;
        color --;
        cost[color].push_back( prev[color]==0?0:i-prev[color]-1 );
        prev[color] = i;
    }
/*
    for( int i=0; i<m; i++ ){
        for( int j=0; j<cost[i].size(); j++ )
            cout << cost[i][j] << ' ';
        cout << endl;
    }
    cout << "+++++++" << endl;
*/
    int max = 1;
    for( int i=0; i<m; i++ ){
        int start = 0, end = 0;
        int current = 1, paid = 0;
        while( true ){
            while( end+1 < cost[i].size() && paid+cost[i][end+1]<=k ){
                current++;
                paid += cost[i][end+1];
                end++;
            }
            if( current > max )
                max = current;
            if( end+1 == cost[i].size() ){
                break;
            }else{
                while( paid+cost[i][end+1]>k && start < cost[i].size() ){
                    current--;
                    start++;
                    paid -= cost[i][start];
                }
            }
        }

    }
    cout << max << endl;
}


/*
 * http://www.codeforces.com/problemset/problem/180/E
 * In Porgress
 */

#include <iostream>

using namespace std;
int n, m, k;

int main() {

	//n cubes
	//m colors
	//k delete as many
	int h[200100], count[200100];
	cin >> n >> m >> k;

	for ( int i=0; i < n; i++){
		cin >> h[i];
		count[i] = 0;
	}

	// Inputs n, m ,k 10 3 2
	// Colored cubes 1(b) 2 1 1(f) 3 2 1 1 2 2

	int ans = 0;
	for (int back=0, front=0; back < n; --count[h[back++]]) {
		//Move front pointer as until you exceed 'k' for color

		while ( (front < n) && (count[h[back]]+k >= (front-back)) ) {
			++count[h[front++]];
		}

		if ( ans < count[h[back]]) { ans = count[h[back]];}
	}

	cout << ans << endl;

}


/*
 * http://www.codeforces.com/problemset/problem/180/E
 * In Porgress
 */

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main() {

	//n cubes
	//m colors
	//k delete as many
	int n, m, k;
	cin >> n >> m >> k;

	int h[n], seq[n];

	for ( int i=0; i < n; i++)
		cin >> seq[i];

	int maxscore = 0;

	for ( int color=1; color <= m ; ++color) {
		for ( int i=0; i < n; i++) {
			if (seq[i] != color) h[i]=0; else h[i] = color;
		}
		for (int i=0; i < n; ++i) {
			int score = 0;
			int d = k;

			if (i >0){
				while ( (h [i] == color) ) ++i;
				while ( (h [i] == 0) ) ++i;
			}

			for ( int j=i; j <  n; ++j) {
				if (h[j] == color) {
					++score;
				}
				else {
					--d;
				}

				if (d<0) break;

			}
			//cout << "\nScore: " <<  score << ", d: " << d << endl;

			if (maxscore < score) maxscore = score;
		}
	}

	//printf ( "n=%d, m=%d, k=%d\n", n, m, k);
	cout << maxscore << endl;

}


/*
 * http://www.codeforces.com/problemset/problem/180/E
 * In Porgress
 */

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main() {

	//n cubes
	//m colors
	//k delete as many
	int n, m, k;
	cin >> n >> m >> k;

	int h[n], seq[n];

	for ( int i=0; i < n; i++)
		cin >> h[i];

	int maxscore = 0;
	//printf ( "n=%d, m=%d, k=%d\n", n, m, k);

	for ( int color=1; color <= m ; ++color) {
		/*
		for ( int i=0; i < n; i++) {
			if (h[i] != color) seq[i]=0; else seq[i] = color;
			cout << seq[i];
		}
		cout << endl;
		*/

		int next_index = 0;

		for (int i=next_index; i < n; ++i) {
			int score = 0;
			int d = k;

			while ( (h[i] != color) && (i < n) ) { ++i; }

			/*
			if (i >0){
				while ( (h [i] == color) ) ++i;
				while ( (h [i] != color) ) ++i;
			}
			*/

			for ( int j=i; j <  n; ++j) {
				if (h[j] == color) {
					++score;
				}
				else {
					--d;
					if (d == k-1) next_index = j;
				}

				if (d<0) break;

			}
			//cout << "\nScore: " <<  score << ", d: " << d << endl;

			if (maxscore < score) maxscore = score;
		}
	}

	cout << maxscore << endl;

}


# include<map>
# include<set>
# include<queue>
# include<stack>
# include<deque>
# include<math.h>
# include<vector>
# include<bitset>
# include<stdio.h>
# include<ctype.h>
# include<iostream>
# include<stdlib.h>
# include<string.h>
# include<algorithm>
# define si size
# define er erase
# define sc scanf
# define be begin
# define fr first
# define se second
# define pr printf
# define pb push_back
# define mp make_pair
# define INF 100000000
# define MN 1000002
# define Int long long int
using namespace std;
int n, m, k, a[MN], i, j, l, r, p, ans = 1, d[MN][5];
vector< vector<int> >v(200000);
main()
{
//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
sc("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
{
    sc("%d",&a[i]);
    v[a[i]].pb(i);
}
for(i=1;i<=m;i++)
{
    l=r=0;
    p=0;
    for(j=0;j<(int)v[i].si()-1;j++)
    {
        d[r][0] = v[i][j];
        d[r][1] = v[i][j+1];

        p += v[i][j+1] - v[i][j] - 1;
        r++;

        //pr("%d ",v[i][j]);
        //pr("p -> %d ", p);

        while(l<r&&p>k)
        {
            p-=d[l][1] - d[l][0] - 1;
            //pr("p= %d ",p);
            l++;
        }
        if(p<=k)
        {
            ans = max(ans, j+1 - l + 1);
        }
        //pr("%d\n", ans);
    }
    //pr("ans %d\n",ans);
}
pr("%d\n",ans);
return 0;
}


# include<map>
# include<set>
# include<queue>
# include<stack>
# include<deque>
# include<math.h>
# include<vector>
# include<bitset>
# include<stdio.h>
# include<ctype.h>
# include<iostream>
# include<stdlib.h>
# include<string.h>
# include<algorithm>
# define si size
# define er erase
# define sc scanf
# define be begin
# define fr first
# define se second
# define pr printf
# define pb push_back
# define mp make_pair
# define INF 100000000
# define MN 300002
# define Int long long int
using namespace std;
int n, m, k, a[MN], i, j, l, r, p, ans = 1, d[MN][5];
vector< vector<int> >v(MN);
main()
{
//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
sc("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
{
    sc("%d",&a[i]);
    v[a[i]].pb(i);
}
for(i=1;i<=m;i++)
{
    l=r=0;
    p=0;
    for(j=0;j<v[i].si()-1;j++)
    {
        d[r][0] = v[i][j];
        d[r][1] = v[i][j+1];
        d[r][2] = j;
        p += v[i][j+1] - v[i][j] - 1;
        r++;
        //pr("%d ",v[i][j]);
        //pr("p -> %d ", p);
        while(l<r&&p>k)
        {
            p-=d[l][1] - d[l][0] - 1;
            //pr("p= %d ",p);
            l++;
        }
        if(p<=k)
        {
            ans = max(ans, j+1 - l + 1);
        }
        //pr("%d\n", ans);
    }
    //pr("ans %d\n",ans);
}
pr("%d\n",ans);
return 0;
}


# include<map>
# include<set>
# include<queue>
# include<stack>
# include<deque>
# include<math.h>
# include<vector>
# include<bitset>
# include<stdio.h>
# include<ctype.h>
# include<iostream>
# include<stdlib.h>
# include<string.h>
# include<algorithm>
# define si size
# define er erase
# define sc scanf
# define be begin
# define fr first
# define se second
# define pr printf
# define pb push_back
# define mp make_pair
# define INF 100000000
# define MN 100002
# define Int long long int
using namespace std;
int n, m, k, a[MN], i, j, l, r, p, ans = 1, d[MN][5];
vector< vector<int> >v(MN);
main()
{
//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
sc("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
{
    sc("%d",&a[i]);
    v[a[i]].pb(i);
}
for(i=1;i<=m;i++)
{
    l=r=0;
    p=0;
    for(j=0;j<v[i].si()-1;j++)
    {
        d[r][0] = v[i][j];
        d[r][1] = v[i][j+1];
        d[r][2] = j;
        p += v[i][j+1] - v[i][j] - 1;
        r++;
        //pr("%d ",v[i][j]);
        //pr("p -> %d ", p);
        while(l<r&&p>k)
        {
            p-=d[l][1] - d[l][0] - 1;
            //pr("p= %d ",p);
            l++;
        }
        if(p<=k)
        {
            ans = max(ans, j+1 - l + 1);
        }
        //pr("%d\n", ans);
    }
    //pr("ans %d\n",ans);
}
pr("%d\n",ans);
return 0;
}


# include<map>
# include<set>
# include<queue>
# include<stack>
# include<deque>
# include<math.h>
# include<vector>
# include<bitset>
# include<stdio.h>
# include<ctype.h>
# include<iostream>
# include<stdlib.h>
# include<string.h>
# include<algorithm>
# define si size
# define er erase
# define sc scanf
# define be begin
# define fr first
# define se second
# define pr printf
# define pb push_back
# define mp make_pair
# define INF 100000000
# define MN 100002
# define Int long long int
using namespace std;
int n, m, k, a[MN], i, j, l, p, ans = 1;
vector< vector<int> >v(MN);
queue< pair<int, int> >q;
main()
{
//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
sc("%d%d%d",&n,&m,&k);
for(i=0;i<n;i++)
{
    sc("%d",&a[i]);
    v[a[i]].pb(i);
}
for(i=1;i<=m;i++)
{
    p=0;
    q.push(mp(0, 0));
    for(j=1;j<v[i].si();j++)
    {
        //pr("%d \n",v[i][j]);
        while(!q.empty()&&p+(v[i][j]-v[i][j-1]-1)>k)
        {
            p-=q.front().fr;
            q.pop();
        }
        q.push(mp((v[i][j]-v[i][j-1]-1), j-1));
        if(p+(v[i][j]-v[i][j-1]-1)<=k)
        {
            ans = max(ans, j - q.front().se + 1);
            p+=(v[i][j]-v[i][j-1]-1);
        }
    }
    //pr("ans %d\n",ans);
}
pr("%d\n",ans);
return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans=1;
vector < int > a[100000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<int(a[i].size())-1;j++)
        {
            t+=a[i][j+1]-a[i][j]-1;
            f++;
            while(t>k)
            {
                f--;
                t-=a[i][s+1]-a[i][s]-1;
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<a[i].size()-1;j++)
        {
            t+=a[i][j+1]-a[i][j]-1;
            f++;
            while(t>k)
            {
                f--;
                t=t-a[i][s+1]-a[i][s]-1;
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<a[i].size();j++)
        {
            t+=a[i][j+1]-a[i][j]-1;
            f++;
            while(t>k)
            {
                f--;
                t=t-a[i][s+1]-a[i][s]-1;
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<a[i].size();j++)
        {
            t+=a[i][j+1]-a[i][j];
            f++;
            if(t>k)
            {
                f--;
                t=t-a[i][s+1]-a[i][s];
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<int(a[i].size())-1;j++)
        {
            t+=a[i][j+1]-a[i][j]-1;
            f++;
            while(t>k)
            {
                f--;
                t-=a[i][s+1]-a[i][s]-1;
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<a[i].size();j++)
        {
            t+=a[i][j+1]-a[i][j];
            f++;
            while(t>k)
            {
                f--;
                t=t-a[i][s+1]-a[i][s];
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

long long int n,m,k,l,f,s,t,ans=1;
vector < int > a[10000];

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>l;
        a[l-1].push_back(i);
    }
    for(int i=0;i<m;i++)
    {
        s=0;
        t=0;
        f=1;
        for(int j=0;j<int(a[i].size())-1;j++)
        {
            t+=a[i][j+1]-a[i][j]-1;
            f++;
            while(t>k)
            {
                f--;
                t-=a[i][s+1]-a[i][s]-1;
                s++;
            }
            if(f>ans)
                ans=f;
        }
    }
    cout<<ans;
    return 0;
}