#include using namespace std; typedef long long ll; typedef pair p_ll; template void debug(T itr1, T itr2) { auto now = itr1; while(now=0; i--) const ll MOD = pow(10,9)+7; const ll LLINF = pow(2,61)-1; const int INF = pow(2,30)-1; vector fac; void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; } ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; } ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; } ll gcd(ll a, ll b) { if (a> H >> W >> K; string c[H]; rep(i,H) cin >> c[i]; int s[H][W][26] = {}; rep(i,H) rep(j,W) fill_n(s[i][j],26,INF); queue q[26]; rep(i,H) rep(j,W) { ll num = c[i][j]-'a'; s[i][j][num] = 1; q[num].push(make_pair(i,j)); } rep(i,26) { // cout << (char)('a'+i) << endl; while (q[i].size()) { p_ll now = q[i].front(); q[i].pop(); ll nx = now.first, ny = now.second; // cout << now.first << "," << now.second << endl; if (nx!=0&&s[nx-1][ny][i]==INF) { s[nx-1][ny][i] = s[nx][ny][i] + 1; q[i].push(make_pair(nx-1,ny)); } if (ny!=0&&s[nx][ny-1][i]==INF) { s[nx][ny-1][i] = s[nx][ny][i] + 1; q[i].push(make_pair(nx,ny-1)); } if (nx!=0&&ny!=0&&s[nx-1][ny-1][i]==INF) { s[nx-1][ny-1][i] = s[nx][ny][i] + 1; q[i].push(make_pair(nx-1,ny-1)); } } } // rep(k,3) { rep(i,H) { rep(j,W) cout << s[i][j][k] << " "; cout << endl; } cout << endl; } ll result = 0; rep(i,H) rep(j,W) { sort(s[i][j],s[i][j]+26); ll plus = s[i][j][K-1]<=min(H-i,W-j) ? min(min(H-i,W-j),(K==26?LLINF:s[i][j][K]-1))-s[i][j][K-1]+1 : 0; result += plus; // cout << i << "," << j << "->" << plus << endl; // debug(s[i][j],s[i][j]+26); } cout << result << endl; return 0; }