#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false; }

struct UnionFind {
    vector<int> par;
    vector<int> rank;
    
    UnionFind(int n = 1) {
        init(n);
    }
    
    void init(int n = 1) {
        par.resize(n); rank.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0;
    }
    
    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        return true;
    }
};

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    ll h, w;
    cin>>h>>w;
    vector<string> grid(h);
    cin>>grid;

    UnionFind ds(h * w);
    rep(i, h) rep(j, w) {
        if (i - 1 >= 0 && grid[i][j] != grid[i - 1][j]) ds.merge(i * w + j, (i - 1) * w + j);
        if (i + 1 < h && grid[i][j] != grid[i + 1][j]) ds.merge(i * w + j, (i + 1) * w + j);
        if (j + 1 < w && grid[i][j] != grid[i][j + 1]) ds.merge(i * w + j, i * w + j + 1);
        if (j - 1 >= 0 && grid[i][j] != grid[i][j - 1]) ds.merge(i * w + j, i * w + j - 1);
    }

    vl bs(h * w);
    vl ws(h * w);
    rep(i, h) rep(j, w) {
        if (grid[i][j] == '#') bs[ds.root(i * w + j)]++;
        else ws[ds.root(i * w + j)]++;
    }
    ll ans = 0;
    rep(i, h * w) {
        ans += bs[i] * ws[i];
    }
    cout<<ans<<endl;
}
#include <bits/stdc++.h>                        
#define pb push_back                            
#define mp make_pair                            
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)((x).size())
#define jonathan ios_base::sync_with_stdio(0)
#define livingston cin.tie(0)
using namespace std;                            
                                                
void yes() {cout << "Yes" << endl;}                                               
void no() {cout << "No" << endl;}                                                        
                                                
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};         
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; 

const int INF = 1e9 + 9;
const long long LINF = 1e18 + 8;
const double EPS = 1e-9;
const long long MOD = 1e9 + 7; //998244353
const double PI = acos(-1);

vector<int> g[100010];
bool used[100010];

void dfs(int v) {
    used[v] = true;
    for (auto to : g[v]) {
        if (!used[to]) {
            dfs(to);
        }
    }
}

void solve() {
    int n, k; cin >> n >> k;
    k = min(n, k);
    vector<int> a(n + 1);
    vector<int> d(n + 1, INF);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int res = (a[1] != 1);
    a[1] = 1;
    for (int i = 1; i <= n; i++) {
        g[a[i]].push_back(i);
    }
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < sz(g[v]); i++) {
            int to = g[v][i];
            if (d[to] > d[v] + 1) {
                d[to] = d[v] + 1;
                q.push(to);
            }
        }
    }

    vector<int> s(n);
    for (int i = 0; i < n; i++) {
        s[i] = i + 1;
    }
    sort(all(s), [&](int x, int y) {
        return d[x] > d[y];
    });

    for (int i = 0; i < n; i++) {
        int v = s[i];
        if (used[v]) continue;
        for (int j = 0; j < k - 1; j++) {
            v = a[v];
        }
        dfs(v);
        if (a[v] != 1) {
            res++;
        }
    }

    cout << res << endl;
}   

int main() {
    jonathan; 
    livingston;
    solve();
    return 0;
}