#include <bits/stdc++.h>

#define el '\n'
#define fi first
#define sec second
#define pb push_back
#define int long long
#define pii pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i++)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; i--)

using namespace std;

const int INF = (int)1e18 + 462010;
const int MAX_N = 1e5;

int n, m, K;
vector<vector<int>> a, P;
vector<int> cnt, st;
int ver = 0;

void ins(int x){
    if(st[x] != ver){
        st[x] = ver;
        cnt[x] = 0;
    }
    cnt[x]++;
}

int get(int x){
    return st[x] == ver ? cnt[x] : 0;
}

void Input(){
    cin >> n >> m >> K;
    a.assign(n + 1, vector<int>(m + 1, 0));
    FOR(i, 1, n) FOR(j, 1, m) cin >> a[i][j];
}

void Prepare(){
    if(n > m){
        vector<vector<int>> b(m + 1, vector<int>(n + 1, 0));
        FOR(i, 1, n) FOR(j, 1, m) b[j][i] = a[i][j];
        swap(a, b);
        swap(n, m);
    }

    P.assign(n + 1, vector<int>(m + 1, 0));
    FOR(i, 1, n) FOR(j, 1, m){
        int val = a[i][j] == -1 ? 0 : a[i][j] % K;
        int cur = P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1] + val;
        cur %= K;
        if(cur < 0) cur += K;
        P[i][j] = cur;
    }

    cnt.assign(K + 5, 0);
    st.assign(K + 5, 0);
}

void Solve(){
    int ans = 0;

    FOR(u, 1, n){
        vector<char> badc(m + 1, 0);

        FOR(v, u, n){
            FOR(j, 1, m) if(a[v][j] == -1) badc[j] = 1;

            ver++;
            ins(0);

            FOR(j, 1, m){
                int s = P[v][j] - P[u - 1][j];
                if(s < 0) s += K;

                if(badc[j]){
                    ver++;
                    ins(s);
                }
                else{
                    ans += get(s);
                    ins(s);
                }
            }
        }
    }

    cout << ans << el;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    Input();
    Prepare();
    Solve();

    return 0;
}