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

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod;
int md(ll a) { if (a >= mod) { return a%mod; } else { return a; } }
int add(int a, int b) { return md((ll)a+b); }
int sub(int a, int b) { return md(md((ll)a-b)%mod+mod); }
int mult(int a, int b) { return md((ll)a*b); }
int sq(int a) { return md((ll)a*a); }

bool compo[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            compo[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
        while (n%i == 0) { n /= i; }
    }
    if (n != 1) { ans /= n; ans *= (n-1); }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv(expo(10, h)));
}

vi g[MAX];
int n;
bool ok[MAX];
int sztot = 0;
int sz[MAX];

void dfs(int st, int p) {
    sz[st] = 1; sztot++;
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        dfs(i, st);
        sz[st] += sz[i];
    }
}

int cent(int st, int p) {
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        if (sz[i] > sztot / 2) { return cent(i, st); }
    }
    return st;
}

int par[MAX];
int dcent[MAX];
void decomp(int st, int p) {
    par[st] = p; dcent[st] = dcent[p] + 1;
    ok[st] = false;
    for (int i : g[st]) {
        if (!ok[i]) { continue; }
        sztot = 0;
        dfs(i, -1);
        int c = cent(i, -1);
        decomp(c, st);
    }
}

int ans = 0;
int L = 2;
vector<pii> g2[MAX];
int d[MAX];

int down[MAX];
int up[MAX];

int comp[MAX];
int ccprev = 0;
int cc = 0;
unordered_map<int, int> maps[MAX];
unordered_map<int, int> comb;
vi nonzero;
vi all;
void dfs2(int st, int p) {
    // cout << "dfs: " << st << endl;
    all.pb(st);
    d[st] = d[p] + 1;
    if (d[st] != 0) { nonzero.pb(st); }
    comp[st] = cc;
    // if (!x) { ans += dist[solve(down[st], d[st])]; }
    maps[cc][up[st]]++;
    for (pii i : g2[st]) {
        if (i.f == p || !ok[i.f]) { continue; }
        down[i.f] = add(mult(down[st], 10), i.s);
        // cout << "down of " << i.f << " is " << down[i.f] << endl;
        up[i.f] = add(up[st], mult(i.s, expo(10, d[st])));
        dfs2(i.f, st);
    }
}

void process(int n) {
    ok[n] = false;
    all.clear();
    for (int i : nonzero) { d[i] = 0; }
    nonzero.clear();

    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n);
        cc++;
    }
    comb.clear();
    comb[0]++;
    FOR(i, ccprev, cc) {
        for (pii j : maps[i]) {
            comb[j.f] += j.s;
        }
    }
    ccprev = cc;

    // cout << "--process--"<< n << endl;
    // for (auto i : comb) { cout << i.f << " " << i.s << endl; }
    // cout << "-----------" << endl;
    for (int i : all) {
        ans += comb[solve(down[i], d[i])];
        // cout << i << " " << down[i] << " " << d[i] << " " << solve(down[i], d[i]) << endl;
        ans -= maps[comp[i]][solve(down[i], d[i])];
        if (up[i] == 0) { ans++; }
        // cout << "down.." << " " << i << " " << down[i] << endl;
        // cout << ans << endl;
    }

}
main() {
    F0R(i, MAX) { maps[i].max_load_factor(0.25); }
    comb.reserve(1024); comb.max_load_factor(0.25);
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod);
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb(v); g[v].pb(u);
        g2[u].pb({v, w}); g2[v].pb({u, w});
    }
    dfs(0, -1);
    int r = cent(0, -1);
    decomp(r, -1);
    F0R(i, MAX) { ok[i] = true; }
    //remember to reset dist appropritaely
    vector<pii> cents;
    FOR(i, 0, n) { cents.pb({dcent[i], i}); }
    // for (auto i : cents) { cout << i.f << " " << i.s << endl;}
    sort(cents.begin(), cents.end());
    F0R(i, n) {
        process(cents[i].s);
    }
    cout << ans << endl;
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod, n;
int md(ll a) { if (a >= mod) { return a%mod; } else { return a; } }
int add(int a, int b) { return md((ll)a+b); }
int sub(int a, int b) { return md(md((ll)a-b)%mod+mod); }
int mult(int a, int b) { return md((ll)a*b); }
int sq(int a) { return md((ll)a*a); }

int pow10[MAX];
int inv10[MAX];

bool compo[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            compo[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
        while (n%i == 0) { n /= i; }
    }
    if (n != 1) { ans /= n; ans *= (n-1); }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}

int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv10[h]);
}

int ops = 0;

vector<pii> g[MAX];
bool ok[MAX];

/* pre-dfs */
int totsz = 0;
int sz[MAX];
void predfs(int u, int p) {
    ops++;
    sz[u] = 1; totsz++;
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        predfs(i.f, u); sz[u] += sz[i.f];
    }
}

/* find the centroid */
int cent(int u, int p) {
    ops++;
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        if (sz[i.f] > totsz / 2) { return cent(i.f, u); }
    }
    return u;
}

map<int, ll> ct;
ll ans = 0;

void dfs1(int u, int p, ll dn, ll up, int d, int pass, int r) {
    if (dn%mod == 0 && !r) { ans--; }
    if (up%mod == 0 && !r) { ans++; }
    if (pass == 0) { ans += ct[solve(dn, d)]; }
    if (pass == 1) { ct[up]++; }
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        dfs1(i.f, u, add(mult(10, dn), i.s), add(mult(i.s, pow10[d]), up), d+1, pass, r);
    }
}

/* do the centroid decomposition */
void decomp(int u, int p) {
    totsz = 0;
    predfs(u, -1);
    int c = cent(u, -1);
    ok[c] = false;
    ct[0] = 1;
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) {
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 0, 0);
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 1, 1);
        }
    }
    ct.clear();
    ct[0] = 1;
    reverse(g[c].begin(), g[c].end());
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) {
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 0, 1);
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 1, 1);
        }
    }
    ct.clear();
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) { decomp(i.f, c); }
    }
}

int main() {
    scanf("%d", &n); scanf("%d", &mod);
    sieve(); phimod = phi(mod);
    pow10[0] = 1; inv10[0] = 1;
    int inv10v = inv(10);
    FOR(i, 1, MAX) {
        pow10[i] = mult(10, pow10[i-1]);
        inv10[i] = mult(inv10v, inv10[i-1]);
    }
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%d %d %d", &u, &v, &w);
        g[u].pb({v, w}); g[v].pb({u, w});
    }
    decomp(1, -1);
    printf("%lld", ans);
}


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

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod;
int add(int a, int b) { return ((ll)a+b)%mod; }
int sub(int a, int b) { return (((ll)a-b)%mod+mod)%mod; }
int mult(int a, int b) { return ((ll)a*b)%mod; }
int sq(int a) { return ((ll)a*a)%mod; }
bool comp[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            comp[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
    }
    if (p) { return n-1; }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv(expo(10, h)));
}

vi g[MAX];
int n;
bool ok[MAX];
int sztot = 0;
int sz[MAX];

void dfs(int st, int p) {
    sz[st] = 1; sztot++;
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        dfs(i, st);
        sz[st] += sz[i];
    }
}

int cent(int st, int p) {
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        if (sz[i] > sztot / 2) { return cent(i, st); }
    }
    return st;
}

int par[MAX];
int dcent[MAX];
void decomp(int st, int p) {
    par[st] = p; dcent[st] = dcent[p] + 1;
    ok[st] = false;
    for (int i : g[st]) {
        if (!ok[i]) { continue; }
        sztot = 0;
        dfs(i, -1);
        int c = cent(i, -1);
        decomp(c, st);
    }
}

map<int, int> dist;
int ans = 0;
int L = 2;
vector<pii> g2[MAX];
int d[MAX];
int down[MAX];
int up[MAX];
vi seen;

void dfs2(int st, int p, int x) {
    if (x == 0) { seen.pb(st); d[st] = d[p] + 1; }
    if (!x) {
        // cout << "down: " << down[st] << endl;
        // cout << "searching for: " << solve(down[st], d[st]) << endl;
        ans += dist[solve(down[st], d[st])];
    }
    else { dist[up[st]]++; }
    for (pii i : g2[st]) {
        if (i.f == p || !ok[i.f]) { continue; }
        down[i.f] = add(mult(down[st], 10), i.s);
        up[i.f] = add(up[st], mult(i.s, expo(10, d[st])));
        dfs2(i.f, st, x);
    }
}

void process(int n) {
    seen.clear();
    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout<<"process--"<<n<<endl;
    d[n] = 0;
    dist.clear();
    dist[0] = 1;
    ok[n] = false;
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }
    for (int i : seen) {
        if (down[i]%mod == 0) { ans--; }
        if (up[i]%mod == 0) { ans++; }
    }

    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout << "ans: " << ans << endl;
    // cout << "do the reverse!" << endl;
    dist.clear();
    dist[0] = 1;
    ok[n] = false;
    reverse(g2[n].begin(), g2[n].end());
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }

}

main() {
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod);
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb(v); g[v].pb(u);
        g2[u].pb({v, w}); g2[v].pb({u, w});
    }
    // cout<<"--------------------------"<<endl;
    dfs(0, -1);
    int r = cent(0, -1);
    decomp(r, -1);
    F0R(i, MAX) { ok[i] = true; }
    //remember to reset dist appropritaely
    vector<pii> cents;
    FOR(i, 0, n) { cents.pb({dcent[i], i}); }
    sort(cents.begin(), cents.end());
    F0R(i, n) {
        process(cents[i].s);
    }
    cout << ans << endl;
}
/*
15
1 2
3 2
4 2
2 5
5 6
6 7
5 8
8 9
9 10
10 11
10 12
9 13
13 14
13 15

*/


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

#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod, n;
int md(ll a) { if (a >= mod) { return a%mod; } else { return a; } }
int add(int a, int b) { return md((ll)a+b); }
int sub(int a, int b) { return md(md((ll)a-b)%mod+mod); }
int mult(int a, int b) { return md((ll)a*b); }
int sq(int a) { return md((ll)a*a); }

bool compo[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            compo[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
        while (n%i == 0) { n /= i; }
    }
    if (n != 1) { ans /= n; ans *= (n-1); }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv(expo(10, h)));
}

int ops = 0;

vector<pii> g[MAX];
bool ok[MAX];

/* pre-dfs */
int totsz = 0;
int sz[MAX];
void predfs(int u, int p) {
    ops++;
    sz[u] = 1; totsz++;
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        predfs(i.f, u); sz[u] += sz[i.f];
    }
}

/* find the centroid */
int cent(int u, int p) {
    ops++;
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        if (sz[i.f] > totsz / 2) { return cent(i.f, u); }
    }
    return u;
}

map<int, int> ct;

ll ans = 0;

void dfs1(int u, int p, ll dn, ll up, int d, int pass, int r) {
    if (dn%mod == 0 && !r) { ans--; }
    if (up%mod == 0 && !r) { ans++; }
    if (pass == 0) { ans += ct[solve(dn, d)]; }
    if (pass == 1) { ct[up]++; }
    F0R(j, g[u].size()) {
        pii i = g[u][j];
        if (i.f == p || !ok[i.f]) { continue; }
        dfs1(i.f, u, add(mult(10, dn), i.s), add(mult(i.s, expo(10, d)), up), d+1, pass, r);
    }
}

/* do the centroid decomposition */
void decomp(int u, int p) {
    totsz = 0;
    predfs(u, -1);
    int c = cent(u, -1);
    ok[c] = false;
    ct[0] = 1;
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) {
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 0, 0);
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 1, 1);
        }
    }
    ct.clear();
    ct[0] = 1;
    reverse(g[c].begin(), g[c].end());
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) {
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 0, 1);
            dfs1(i.f, c, i.s%mod, i.s%mod, 1, 1, 1);
        }
    }
    ct.clear();
    F0R(j, g[c].size()) {
        pii i = g[c][j];
        if (ok[i.f]) { decomp(i.f, c); }
    }
}

main() {
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod);
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb({v, w}); g[v].pb({u, w});
    }
    decomp(1, -1);
    cout << ans << endl;
}


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

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod;
int add(int a, int b) { return ((ll)a+b)%mod; }
int sub(int a, int b) { return (((ll)a-b)%mod+mod)%mod; }
int mult(int a, int b) { return ((ll)a*b)%mod; }
int sq(int a) { return ((ll)a*a)%mod; }
bool comp[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            comp[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
    }
    if (p) { return n-1; }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv(expo(10, h)));
}

vi g[MAX];
int n;
bool ok[MAX];
int sztot = 0;
int sz[MAX];

void dfs(int st, int p) {
    sz[st] = 1; sztot++;
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        dfs(i, st);
        sz[st] += sz[i];
    }
}

int cent(int st, int p) {
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        if (sz[i] > sztot / 2) { return cent(i, st); }
    }
    return st;
}

int par[MAX];
int dcent[MAX];
void decomp(int st, int p) {
    par[st] = p; dcent[st] = dcent[p] + 1;
    ok[st] = false;
    for (int i : g[st]) {
        if (!ok[i]) { continue; }
        sztot = 0;
        dfs(i, -1);
        int c = cent(i, -1);
        decomp(c, st);
    }
}

map<int, int> dist;
int ans = 0;
int L = 2;
vector<pii> g2[MAX];
int d[MAX];
int down[MAX];
int up[MAX];
vi seen;

void dfs2(int st, int p, int x) {
    if (x == 0) { seen.pb(st); d[st] = d[p] + 1; }
    if (!x) {
        // cout << "down: " << down[st] << endl;
        // cout << "searching for: " << solve(down[st], d[st]) << endl;
        int query = solve(down[st], d[st]);
        if (dist.find(query) != dist.end()) { ans += dist[query]; }
    }
    else { dist[up[st]]++; }
    for (pii i : g2[st]) {
        if (i.f == p || !ok[i.f]) { continue; }
        down[i.f] = add(mult(down[st], 10), i.s);
        up[i.f] = add(up[st], mult(i.s, expo(10, d[st])));
        dfs2(i.f, st, x);
    }
}

void process(int n) {
    seen.clear();
    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout<<"process--"<<n<<endl;
    d[n] = 0;
    dist.clear();
    dist[0] = 1;
    ok[n] = false;
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }
    for (int i : seen) {
        if (down[i]%mod == 0) { ans--; }
        if (up[i]%mod == 0) { ans++; }
    }

    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout << "ans: " << ans << endl;
    // cout << "do the reverse!" << endl;
    dist.clear();
    dist[0] = 1;
    reverse(g2[n].begin(), g2[n].end());
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }

}

main() {
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod);
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb(v); g[v].pb(u);
        g2[u].pb({v, w}); g2[v].pb({u, w});
    }
    // cout<<"--------------------------"<<endl;
    dfs(0, -1);
    int r = cent(0, -1);
    decomp(r, -1);
    F0R(i, MAX) { ok[i] = true; }
    //remember to reset dist appropritaely
    vector<pii> cents;
    FOR(i, 0, n) { cents.pb({dcent[i], i}); }
    sort(cents.begin(), cents.end());
    F0R(i, n) {
        process(cents[i].s);
    }
    cout << ans << endl;
}
/*
15
1 2
3 2
4 2
2 5
5 6
6 7
5 8
8 9
9 10
10 11
10 12
9 13
13 14
13 15

*/


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

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod;
int add(int a, int b) { return ((ll)a+b)%mod; }
int sub(int a, int b) { return (((ll)a-b)%mod+mod)%mod; }
int mult(int a, int b) { return ((ll)a*b)%mod; }
int sq(int a) { return ((ll)a*a)%mod; }
bool comp[MAX];
int iexpo[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            comp[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (comp[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
    }
    if (p) { return n-1; }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
void invexpo() { F0R(i, MAX) { iexpo[i] = inv(expo(10, i)); } }
int solve(int dv, int h) {
    return mult(sub(0, dv), iexpo[h]);
}

vi g[MAX];
int n;
bool ok[MAX];
int sztot = 0;
int sz[MAX];

void dfs(int st, int p) {
    sz[st] = 1; sztot++;
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        dfs(i, st);
        sz[st] += sz[i];
    }
}

int cent(int st, int p) {
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        if (sz[i] > sztot / 2) { return cent(i, st); }
    }
    return st;
}

int par[MAX];
int dcent[MAX];
void decomp(int st, int p) {
    par[st] = p; dcent[st] = dcent[p] + 1;
    ok[st] = false;
    for (int i : g[st]) {
        if (!ok[i]) { continue; }
        sztot = 0;
        dfs(i, -1);
        int c = cent(i, -1);
        decomp(c, st);
    }
}

unordered_map<int, int> dist;
int ans = 0;
int L = 2;
vector<pii> g2[MAX];
int d[MAX];
int down[MAX];
int up[MAX];
vi seen;

void dfs2(int st, int p, int x) {
    if (x == 0) { seen.pb(st); d[st] = d[p] + 1; }
    if (!x) {
        // cout << "down: " << down[st] << endl;
        // cout << "searching for: " << solve(down[st], d[st]) << endl;
        ans += dist[solve(down[st], d[st])];
    }
    else { dist[up[st]]++; }
    for (pii i : g2[st]) {
        if (i.f == p || !ok[i.f]) { continue; }
        down[i.f] = add(mult(down[st], 10), i.s);
        up[i.f] = add(up[st], mult(i.s, expo(10, d[st])));
        dfs2(i.f, st, x);
    }
}

void process(int n) {
    seen.clear();
    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout<<"process--"<<n<<endl;
    d[n] = 0;
    dist.clear();
    dist[0] = 1;
    ok[n] = false;
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }
    for (int i : seen) {
        if (down[i]%mod == 0) { ans--; }
        if (up[i]%mod == 0) { ans++; }
    }

    // for (auto i : dist) { cout << i.f << " " << i.s << endl; }
    // cout << "ans: " << ans << endl;
    // cout << "do the reverse!" << endl;
    dist.clear();
    dist[0] = 1;
    ok[n] = false;
    reverse(g2[n].begin(), g2[n].end());
    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n, 0);
        dfs2(i.f, n, 1);
    }

}

main() {
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod); invexpo();
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb(v); g[v].pb(u);
        g2[u].pb({v, w}); g2[v].pb({u, w});
    }
    // cout<<"--------------------------"<<endl;
    dfs(0, -1);
    int r = cent(0, -1);
    decomp(r, -1);
    F0R(i, MAX) { ok[i] = true; }
    //remember to reset dist appropritaely
    vector<pii> cents;
    FOR(i, 0, n) { cents.pb({dcent[i], i}); }
    sort(cents.begin(), cents.end());
    F0R(i, n) {
        process(cents[i].s);
    }
    cout << ans << endl;
}


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

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) FOR(i, 0, a)

const int MAX = 100005;
const int MOD = 1000000007;

int mod, phimod;
int add(int a, int b) { return ((ll)a+b)%mod; }
int sub(int a, int b) { return (((ll)a-b)%mod+mod)%mod; }
int mult(int a, int b) { return ((ll)a*b)%mod; }
int sq(int a) { return (ll)a*a%mod; }
bool compo[MAX];
void sieve() {
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        for (int j=2*i; j<MAX; j+=i) {
            compo[j] = true;
        }
    }
}

int phi(int n) {
    bool p = true;
    int ans = n;
    FOR(i, 2, MAX) {
        if (compo[i]) { continue; }
        if (n%i == 0) { ans /= i; ans *= (i-1); p = false; }
    }
    if (p) { return n-1; }
    return ans;
}

int expo(int a, int b) {
    if (b == 0) { return 1; }
    if (b%2) { return mult(a, sq(expo(a, b/2))); }
    else { return sq(expo(a, b/2)); }
}
int inv(int a) { return expo(a, phimod-1); }
int solve(int dv, int h) {
    return mult(sub(0, dv), inv(expo(10, h)));
}

vi g[MAX];
int n;
bool ok[MAX];
int sztot = 0;
int sz[MAX];

void dfs(int st, int p) {
    sz[st] = 1; sztot++;
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        dfs(i, st);
        sz[st] += sz[i];
    }
}

int cent(int st, int p) {
    for (int i : g[st]) {
        if (i == p || !ok[i]) { continue; }
        if (sz[i] > sztot / 2) { return cent(i, st); }
    }
    return st;
}

int par[MAX];
int dcent[MAX];
void decomp(int st, int p) {
    par[st] = p; dcent[st] = dcent[p] + 1;
    ok[st] = false;
    for (int i : g[st]) {
        if (!ok[i]) { continue; }
        sztot = 0;
        dfs(i, -1);
        int c = cent(i, -1);
        decomp(c, st);
    }
}

int ans = 0;
int L = 2;
vector<pii> g2[MAX];
int d[MAX];

int down[MAX];
int up[MAX];

int comp[MAX];
int ccprev = 0;
int cc = 0;
unordered_map<int, int> maps[MAX];
unordered_map<int, int> comb;
vi nonzero;
vi all;
void dfs2(int st, int p) {
    // cout << "dfs: " << st << endl;
    all.pb(st);
    d[st] = d[p] + 1;
    if (d[st] != 0) { nonzero.pb(st); }
    comp[st] = cc;
    // if (!x) { ans += dist[solve(down[st], d[st])]; }
    maps[cc][up[st]]++;
    for (pii i : g2[st]) {
        if (i.f == p || !ok[i.f]) { continue; }
        down[i.f] = add(mult(down[st], 10), i.s);
        // cout << "down of " << i.f << " is " << down[i.f] << endl;
        up[i.f] = add(up[st], mult(i.s, expo(10, d[st])));
        dfs2(i.f, st);
    }
}

void process(int n) {
    ok[n] = false;
    all.clear();
    for (int i : nonzero) { d[i] = 0; }
    nonzero.clear();

    for (pii i : g2[n]) {
        if (!ok[i.f]) { continue; }
        down[i.f] = i.s%mod;
        up[i.f] = i.s%mod;
        dfs2(i.f, n);
        cc++;
    }
    comb.clear();
    comb[0]++;
    FOR(i, ccprev, cc) {
        for (pii j : maps[i]) {
            comb[j.f] += j.s;
        }
    }
    ccprev = cc;

    // cout << "--process--"<< n << endl;
    // for (auto i : comb) { cout << i.f << " " << i.s << endl; }
    // cout << "-----------" << endl;
    for (int i : all) {
        ans += comb[solve(down[i], d[i])];
        // cout << i << " " << down[i] << " " << d[i] << " " << solve(down[i], d[i]) << endl;
        ans -= maps[comp[i]][solve(down[i], d[i])];
        if (up[i] == 0) { ans++; }
        // cout << "down.." << " " << i << " " << down[i] << endl;
        // cout << ans << endl;
    }

}
main() {
    scanf("%I64d", &n); scanf("%I64d", &mod);
    sieve(); phimod = phi(mod);
    F0R(i, MAX) { ok[i] = true; }
    F0R(i, n-1) {
        int u, v, w; scanf("%I64d %I64d %I64d", &u, &v, &w);
        g[u].pb(v); g[v].pb(u);
        g2[u].pb({v, w}); g2[v].pb({u, w});
    }
    dfs(0, -1);
    int r = cent(0, -1);
    decomp(r, -1);
    F0R(i, MAX) { ok[i] = true; }
    //remember to reset dist appropritaely
    vector<pii> cents;
    FOR(i, 0, n) { cents.pb({dcent[i], i}); }
    // for (auto i : cents) { cout << i.f << " " << i.s << endl;}
    sort(cents.begin(), cents.end());
    F0R(i, n) {
        process(cents[i].s);
    }
    cout << ans << endl;
}


#include<stdio.h>
#include<string.h>
int map[20010][11],number[20010],num[10];
int main()
{
    int n,m,k,i,j,x,y,sum=0;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        sum=0;
        memset(number,0,sizeof(number));
        memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
        for(j=0;j<m;j++)
        scanf("%d",&map[i][j]);
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&x,&y);
            number[x-1]++;
            num[y-1]++;
        }
        for(i=0;i<n;i++)
        {for(j=0;j<m;j++)
        {
            if(map[i][j])
            sum+=num[j];
        }
        if(i==0)printf("%d",sum-number[i]);
        else printf(" %d",sum-number[i]);
        sum=0;
        }
        printf("\n");
    }
    return 0;
}


#include <stdio.h>

#define N 20001
#define M 11

int emplo[N][M] = {0};

int chats[M] = {0};

int st[N] = {0};

int main(void)
{
	int res = 0;
	int x, y;
	int temp;
	int n, m, k;
	int i, j;

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

	for (i = 1; i <= n; ++i)
	{
		for (j = 1; j <= m; ++j)
		{
			scanf("%i", &temp);

			emplo[i][j] = temp;
		}
	}

	for (i = 1; i <= k; ++i)
	{
		scanf("%i %i", &x, &y);
		st[x] += 1;

		chats[y] += 1;
	}

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

		for (j = 1; j <= m; ++j)
		{
			if (emplo[i][j] == 1)
				res += chats[j];
		}

		res -= st[i];

		printf("%i ", res);
	}

	printf("\n");

	return 0;
}


#include<stdio.h>
#include<string.h>
int jl[20005][15];
int sum1[20005][15];
int sum2[100005];
int main()
{
    int n,m,k;
    int i,j;
    int x,y;
    int sum[20005];
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(sum,0,sizeof(sum));
        memset(jl,0,sizeof(jl));
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&jl[i][j]);
        for(i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            sum2[y]++;
            sum1[x][y]++;
        }
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                if(jl[i][j]==1)
                    sum[i]+=sum2[j]-sum1[i][j];
            }
        for(i=1;i<=n;i++)
        {
            printf("%d",sum[i]);
            if(i!=n)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int jl[20005][15];
int main()
{

    int n,m,k;
    int i,j;
    int x,y;
    int sum[20005];
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(sum,0,sizeof(sum));
        memset(jl,0,sizeof(jl));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&jl[i][j]);
        for(i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            for(j=1;j<=n;j++)
            {
                if(jl[j][y]==1&&j!=x)
                    sum[j]++;
            }
        }
        for(i=1;i<=n;i++)
        {
            printf("%d",sum[i]);
            if(i!=n)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

long int flog[20001];
int top[11];

int main( void ) {
     int n, m, k;

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

     bool r[20001][11];
     int ar[11] = {0};

     int i, j;
     for( i = 1; i <= n; i++ )
          for( j = 1; j <= m; j++ )
               scanf("%d", &r[i][j]);

     int x, y;
     for( i = 0; i < k; i++ ) {
          scanf("%i %i", &x, &y);

          ar[y]++;

/*          for( j = 0; j < top[y]; j++ )*/
/*               flog[r[y][j]]++;*/

          flog[x]--;
     }

     for( i = 1; i <= n; i++ ) {
         for( j = 1; j <= m; j++ )
               flog[i] += ar[j]*r[i][j];
          printf("%li ", flog[i]);
     }

     putchar('\n');

     return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

unsigned int flog[20001];
int top[11];

int main( void ) {
     int n, m, k;

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

     int r[11][20001];

     int i, j, c;
     for( i = 1; i <= n; i++ )
          for( j = 1; j <= m; j++ ) {
               scanf("%d", &c);

               r[j][top[j]] = i;
               top[j] += c;
          }

     int x, y;
     for( i = 0; i < k; i++ ) {
          scanf("%i %i", &x, &y);

          for( j = 0; j < top[y]; j++ )
               flog[r[y][j]]++;

          flog[x]--;
     }

     printf("%u", flog[1]);
     for( i = 2; i <= n; i++ )
          printf(" %u", flog[i]);
     putchar('\n');

     return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main( void ) {
     int n, m, k;

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

     const int Max = 20000;
     int r[11][Max+1];
     int top[11] = { 0 };

     int i, j, c;
     for( i = 1; i <= n; i++ )
          for( j = 1; j <= m; j++ ) {
               scanf("%d", &c);

               if( c ) r[j][top[j]++] = i;
          }

     unsigned long long int log[Max+1];
     memset( log, 0, sizeof(log) );

     int x, y;
     for( i = 0; i < k; i++ ) {
          scanf("%i %i", &x, &y);

          for( j = 0; j < top[y]; j++ )
               log[r[y][j]]++;

          log[x]--;
     }

     printf("%llu", log[1]);
     for( i = 2; i <= n; i++ )
          printf(" %llu", log[i]);
     printf("\n");

     return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main( void ) {
     int n, m, k;

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

     const int Max = 20000;
     bool emp[Max+1][11];

     int i, j;
     for( i = 1; i <= n; i++ )
          for( j = 1; j <= m; j++ ) scanf("%d", &emp[i][j]);

     unsigned long long int log[Max+1];
     memset( log, 0, sizeof(log) );

     int x, y;
     for( i = 0; i < k; i++ ) {
          scanf("%i %i", &x, &y);

          for( j = 1; j <= n; j++ )
               if( emp[j][y] && x != j ) log[j]++;
     }

     printf("%llu", log[1]);
     for( i = 2; i <= n; i++ )
          printf(" %llu", log[i]);
     printf("\n");

     return 0;
}


#include <stdio.h>

#define N	20000
#define M	10

int main() {
	static int aa[N][M], xx[N], yy[M];
	int n, m, k, i, j, y;

	scanf("%d%d%d", &n, &m, &k);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			scanf("%d", &aa[i][j]);
	while (k-- > 0) {
		scanf("%d%d", &i, &j);
		i--, j--;
		xx[i]++;
		yy[j]++;
	}
	for (i = 0; i < n; i++) {
		y = 0;
		for (j = 0; j < m; j++)
			if (aa[i][j] > 0)
				y += yy[j];
		printf("%d ", y - xx[i]);
	}
	printf("\n");
	return 0;
}


#include <stdio.h>

#define N	20000
#define M	10

int main() {
	static int aa[N][M], kk0[N][M], kk1[M];
	int n, m, k, i, j;

	scanf("%d%d%d", &n, &m, &k);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			scanf("%d", &aa[i][j]);
	while (k-- > 0) {
		scanf("%d%d", &i, &j);
		i--, j--;
		kk0[i][j]++;
		kk1[j]++;
	}
	for (i = 0; i < n; i++) {
		int cnt;

		cnt = 0;
		for (j = 0; j < m; j++)
			if (aa[i][j])
				cnt += kk1[j] - kk0[i][j];
		printf("%d ", cnt);
	}
	printf("\n");
	return 0;
}


#include<stdio.h>
long int M[200005][15];
int main(){
	long int n,k;
	int m;
	scanf("%ld %d %ld",&n,&m,&k);
	for(long int i =1 ; i <= n; i++)
		for(int j =1 ; j <= m ; j++)
			scanf("%ld",&M[i][j]);

	long int x;
	int y;
	for(long int i = 0 ; i < k ;i++){
		scanf("%ld %d",&x,&y);
		for(long int j = 1 ; j <= n; j++)
			if (M[j][y] != 0 && j != x)
				M[j][0] += 1;
	}

	for(long int i = 1 ; i <= n; i++)
		printf("%ld ",M[i][0]);
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <string.h>
long a[20005][15],num[15],count[20005];
int main()
{
    long n,m,k,i,j,x,y,tot;
    scanf("%ld %ld %ld",&n,&m,&k);
    memset(a,0,sizeof(a));
    memset(num,0,sizeof(num));
    memset(count,0,sizeof(count));
    for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
        scanf("%ld",&a[i][j]);
    for (i=1;i<=k;i++)
    {
        scanf("%ld %ld",&x,&y);
        num[y]++;count[x]++;
    }
    for (i=1;i<=n;i++)
    {
        tot=0;
        for (j=1;j<=m;j++)
         if (a[i][j]==1) tot+=num[j];
        tot-=count[i];
        printf("%ld ",tot);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<ctype.h>
#include<string.h>
int arr[20000+5][15];
int users[20000+5]={0};
int chats[15]={0};
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++)
            scanf("%d",&arr[i][j]);
    }
    for(i=0;i<k;i++){
        int temp1,temp2;
        scanf("%d%d",&temp1,&temp2);
        users[temp1]++;
        chats[temp2]++;
    }
    for(i=0;i<n-1;i++){
        int sum=0;
        for(j=0;j<m;j++){
            if(arr[i][j]){
                sum+=chats[j+1];
            }
        }
        sum-=users[i+1];
        printf("%d ",sum);
    }
    int sum=0;
    for(j=0;j<m;j++){
            if(arr[n-1][j]){
                sum+=chats[j+1];
            }
    }
    sum-=users[n];
    printf("%d\n",sum);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

int n, m, k, counter[12], fuck[20010], ar[20010][12];

int main(){
    int i, j, l, x, y;

    while (scanf("%d %d %d", &n, &m, &k) != EOF){
        for (i = 1; i <= n; i++){
            for (j = 1; j <= m; j++){
                scanf("%d", &ar[i][j]);
            }
        }

        clr(counter), clr(fuck);
        while (k--){
            scanf("%d %d", &x, &y);
            counter[y]++;
            fuck[x]++;
        }

        for (i = 1; i <= n; i++){
            int res = 0;
            for (j = 1; j <= m; j++){
                if (ar[i][j]) res += counter[j];
            }

            res -= fuck[i];
            if (i > 1) putchar(32);
            printf("%d", res);
        }
        puts("");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int chats[100+10]={0};
int employees[100000]={0};
int log[20000+10][10+10];

int main()
{
    int x,y;
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int i ,j ;
    for(i=0 ; i<n ; i++)
    {
        for(j=0; j<m ; j++)
        {
            scanf("%d",&log[i][j]);
        }
    }
    for(i=0;i<k;i++)
    {
        scanf("%d%d",&x,&y);
        chats[y-1]++;
        employees[x-1]++;
    }
    for(i=0;i<n;i++)
    {
        int res = 0;
        for(j=0 ; j<m ; j++)
        {
            if(log[i][j])
                res += chats[j];
        }
        printf("%d ",res-employees[i]);
    }
    return 0;
}


#include <stdio.h>

int main()
{
	int n,m,k;
	scanf("%d %d %d",&n,&m,&k);
	int ar[n][m],a,b;
	for(a=0;a<n;a++)
	for(b=0;b<m;b++)
		scanf("%d",&ar[a][b]);
	int emp,chat;
	int not[n],chats[10];
	int emp_chat[n][10];
	for(a=0;a<n;a++)
	{
		not[a] = 0;
		for(b=0;b<10;b++) { emp_chat[a][b] = 0; chats[b] = 0; }
	}
	for(a=0;a<k;a++)
	{
		scanf("%d %d",&emp,&chat);
		emp_chat[emp-1][chat-1]++;
		chats[chat-1]++;
	}
	for(a=0;a<n;a++)
	{
		for(b=0;b<10;b++)
			if(ar[a][b]) {
				not[a] += chats[b];
				not[a] -= emp_chat[a][b];
				}
	}
	for(a=0;a<n;a++) printf("%d ",not[a]);
}


#include <stdio.h>

int main()
{
	int n,m,k;
	scanf("%d %d %d",&n,&m,&k);
	int ar[n][m],a,b;
	for(a=0;a<n;a++)
	for(b=0;b<m;b++)
		scanf("%d",&ar[a][b]);
	int emp,chat;
	int not[n]; // emp
	for(a=0;a<n;a++) not[a] = 0;
	for(a=0;a<k;a++)
	{
		scanf("%d %d",&emp,&chat);
		for(b=0;b<n;b++)
		{
			if(b == emp-1) continue;
			if(ar[b][chat-1]) not[b]++;
		}
	}
	for(a=0;a<n;a++) printf("%d ",not[a]);
}


#include <stdio.h>
#define NM 20001
int n, m, k;
int c[NM][11];
int chat[11];
long long noti[NM];

int main(void) {
    int i, j, a, x, y;
    scanf("%d %d %d",&n,&m,&k);
    for (i=1; i<=n; i++){
        for (j=1; j <=m; j++){
            scanf("%d",&a);
            if(a) c[i][j] = 1;
        }
    }
    while (k--){
        scanf("%d %d",&x, &y);
        chat[y]++;
        noti[x]--;
    }
    for (i=1; i<=n; i++){
        for (j=1; j <=m; j++){
            if (c[i][j]){
                noti[i] += chat[j];
            }
        }
    }
    for (i=1; i<=n; i++){
        printf("%I64d ",noti[i]);
    }
    return 0;
}


#include<stdio.h>
int main()
{
int n,m,min,max,t[110],temp,i,count=0,flag1=0,flag2=0,flag=0;
scanf("%d %d %d %d",&n,&m,&min,&max);
temp=n-m;
for(i=0;i<m;i++)
scanf("%d",&t[i]);
for(i=0;i<m;i++)
{
    if(t[i]>max||t[i]<min)
    {
        flag=1;
        break;

    }
   if(t[i]==min||t[i]==max)
   {
       if(t[i]==min&&flag1==0)
       {
        count++;
        flag1=1;
       }
       if(t[i]==max&&flag2==0)
       {
           count++;
           flag2=1;
       }
   }
}
if(flag==1)
printf("Incorrect");
else
{
if(temp>=2-count)
    printf("Correct");
else
    printf("Incorrect");
}
return 0;
}


import java.io.BufferedReader;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Set;
import java.util.AbstractMap;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author AlexFetisov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int countPeople = in.nextInt();
        int countChats = in.nextInt();
        int countLog = in.nextInt();
        boolean[][] inChat = new boolean[countPeople][countChats];
        for (int i = 0; i < countPeople; ++i) {
            for (int j = 0; j < countChats; ++j) {
                int x = in.nextInt();
                inChat[i][j] = (x == 1);
            }
        }
        TreeCounter<Integer>[] fromPeople = new TreeCounter[countChats];
        for (int i = 0; i < countChats; ++i) {
            fromPeople[i] = new TreeCounter<Integer>();
        }
        for (int i = 0; i < countLog; ++i) {
            int from = in.nextInt()-1;
            int to = in.nextInt()-1;
            fromPeople[to].add(from);
        }
        int[] result = new int[countPeople];
        for (int room = 0; room < countChats; ++room) {
            int total = (int) fromPeople[room].size();
            for (int person = 0; person < countPeople; ++person) {
                if (inChat[person][room]) {
                    int am = 0;
                    if (fromPeople[room].containsKey(person)) {
                        am = fromPeople[room].get(person);
                    }
                    result[person] += total - am;
                }
            }
        }
        out.println(ArrayUtils.toString(result));
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer stt;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    public String nextString() {
        while (stt == null || !stt.hasMoreTokens()) {
            stt = new StringTokenizer(nextLine());
        }
        return stt.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

}

class TreeCounter<K> {

    private final TreeMap<K, Integer> map;
    long size;

    public TreeCounter() {
        map = new TreeMap<K, Integer>();
        size = 0;
    }

    public long size() {
        return size;
    }

    public boolean containsKey(K key) {
        return map.containsKey(key);
    }

    public Integer get(K key) {
        return map.get(key);
    }

    public int add(K key, int count) {
        Integer value = map.get(key);
        if (value == null) {
            value = 0;
        }
        value += count;
        map.put(key, value);
        size += count;
        return value;
    }

    public int add(K key) {
        return add(key, 1);
    }

}

class ArrayUtils {

    public static String toString(int[] a) {
        StringBuilder result = new StringBuilder("");
        for (int x : a) result.append(x).append(' ');
        return result.substring(0, result.length()-1);
    }

}


import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(in.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        ArrayList<Integer>[] adj = new ArrayList[n+1];
        for (int i = 0; i <= n; i++) adj[i] = new ArrayList();
        for (int i = 1; i <= n; i++) {
            st = new StringTokenizer(in.readLine());
            for (int j = 1; j <= m; j++) {
                if (st.nextToken().equals("1")) {
                    adj[i].add(j);
                }
            }
        }
        int[] grp = new int[m+1];
        int[] sent = new int[n+1];
        for (int i = 0; i < k; i++) {
            st = new StringTokenizer(in.readLine());
            int x = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            grp[c]++;
            sent[x]--;
        }

        for (int i = 1; i <= n; i++) {
            if (i > 1) {
                System.out.print(' ');
            }
            int sum = 0;
            for (int j : adj[i]) {
                sum += grp[j];
            }
            sum += sent[i];
            System.out.print(sum);
        }
        System.out.print('\n');
    }

}
/*

 */


import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int g=in.nextInt();
        int k=in.nextInt();
        int[][] adj=new int[n][g];
        int[] groups=new int[g];
        int[] emp=new int[n];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<g;j++)
            {
                adj[i][j]=in.nextInt();
            }
        }
        for(int i=0;i<k;i++)
        {
            int a=in.nextInt()-1;
            int b=in.nextInt()-1;
            emp[a]--;
            groups[b]++;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<g;j++)
            {
                if(adj[i][j]==0) continue;
                emp[i]+=groups[j];
            }
            System.out.print(emp[i]+" ");
        }
        System.out.println();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PrintWriter printWriter = new PrintWriter(System.out);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        int[][] matrSmejn = new int[m][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[j][i] = scanner.nextInt();
            }
        }

        int[] counts = new int[n];

        for (int i = 0; i < k; i++) {
            int user = scanner.nextInt() - 1;
            int chat = scanner.nextInt() - 1;

            for (int j = 0; j < n; j++) {
                if (j != user) counts[j] += matrSmejn[chat][j];
            }
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);

        PrimitiveIterator.OfInt scanner = bufferedReader.lines().flatMapToInt(s -> {
            String[] split = s.split(" ");
            int[] arr = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                arr[i] = Integer.parseInt(split[i]);
            }
            return IntStream.of(arr);
        }).iterator();

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        int[][] matrSmejn = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[i][j] = scanner.nextInt();
            }
        }

        int[] counts = new int[n];

        int[] cheatStat = new int[m];
        for (int i = 0; i < k; i++) {
            int user = scanner.nextInt() - 1;
            int chat = scanner.nextInt() - 1;

            cheatStat[chat] += 1;
            counts[user] -= 1;
        }

        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int l = 0; l < m; l++) {
                sum += matrSmejn[i][l] * cheatStat[l];
            }
            res[i] = sum;
        }

        for (int i = 0; i < n; i++) {
            counts[i] += res[i];
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PrintWriter printWriter = new PrintWriter(System.out);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        byte[][] matrSmejn = new byte[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[i][j] = scanner.nextByte();
            }
        }

        int[] counts = new int[n];

        for (int i = 0; i < k; i++) {
            int user = scanner.nextByte() - 1;
            int chat = scanner.nextByte() - 1;

            for (int j = 0; j < n; j++) {
                if (j != user) counts[j] += matrSmejn[j][chat];
            }
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PrintWriter printWriter = new PrintWriter(System.out);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        int[][] matrSmejn = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[i][j] = scanner.nextInt();
            }
        }

        int[] counts = new int[n];

        for (int i = 0; i < k; i++) {
            int user = scanner.nextInt() - 1;
            int chat = scanner.nextInt() - 1;

            for (int j = 0; j < n; j++) {
                if (j != user) counts[j] += matrSmejn[j][chat];
            }
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PrimitiveIterator;
import java.util.Scanner;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) throws IOException {
//        Scanner scanner = new Scanner(System.in);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);

        PrimitiveIterator.OfInt iterator = bufferedReader.lines().flatMapToInt(s -> {
            String[] split = s.split(" ");
            int[] arr = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                arr[i] = Integer.parseInt(split[i]);
            }
            return IntStream.of(arr);
        }).iterator();

        int n = iterator.nextInt();
        int m = iterator.nextInt();
        int k = iterator.nextInt();

        int[][] matrSmejn = new int[m][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[j][i] = iterator.nextInt();
            }
        }

        int[] counts = new int[n];

        for (int i = 0; i < k; i++) {
            int user = iterator.nextInt() - 1;
            int chat = iterator.nextInt() - 1;

            for (int j = 0; j < n; j++) {
                if (j != user) counts[j] += matrSmejn[chat][j];
            }
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;

/**
 * @author iusiumbeli_ni (nickus)
 */
public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);

        PrimitiveIterator.OfInt scanner = bufferedReader.lines().flatMapToInt(s -> {
            String[] split = s.split(" ");
            int[] arr = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                arr[i] = Integer.parseInt(split[i]);
            }
            return IntStream.of(arr);
        }).iterator();

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        int[][] matrSmejn = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrSmejn[i][j] = scanner.nextInt();
            }
        }

        int[][] stat = new int[m][n];
        for (int i = 0; i < k; i++) {
            int user = scanner.nextInt() - 1;
            int chat = scanner.nextInt() - 1;

            stat[chat][user] += 1;
        }

        int[][] res = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int sum = 0;
                for (int l = 0; l < m; l++) {
                    sum += matrSmejn[i][l] * stat[l][j];
                }
                res[i][j] = sum;
            }
        }

        int[] counts = new int[n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j)
                    counts[i] += res[i][j];
            }
        }

        for (int count : counts) {
            printWriter.print(count + " ");
        }
        printWriter.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Solve4 {

    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter(System.out);
        new Solve4().solve(pw);
        pw.flush();
        pw.close();
    }

    public void solve(PrintWriter pw) throws IOException {
        FastReader sc = new FastReader();
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        ArrayList<Integer>[] in = new ArrayList[m + 1];
        for (int i = 1; i <= m; i++) {
            in[i] = new ArrayList();
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (sc.nextInt() == 1) {
                    in[j].add(i);
                }
            }
        }
        int[] cnt = new int[n + 1];
        for (int i = 0; i < k; i++) {
            int x = sc.nextInt(),y=sc.nextInt();
            for (int j = 0; j < in[y].size(); j++) {
                if(in[y].get(j)==x) continue;
                cnt[in[y].get(j)]++;
            }
        }
        pw.print(cnt[1]);
        for (int i = 2;i<=n; i++) {
            pw.print(" "+cnt[i]);
        }
        pw.println();

    }

    static class FastReader {

        StringTokenizer st;
        BufferedReader br;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public boolean hasNext() throws IOException {
            String s = br.readLine();
            if (s == null || s.isEmpty()) {
                return false;
            }
            st = new StringTokenizer(s);
            return true;
        }

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s.isEmpty()) {
                    return null;
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Solve4 {

    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter(System.out);
        new Solve4().solve(pw);
        pw.flush();
        pw.close();
    }

    public void solve(PrintWriter pw) throws IOException {
        FastReader sc = new FastReader();
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        int[][] a = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int[] cnt = new int[m + 1];
        int[] ans = new int[n + 1];
        for (int i = 1; i <= k; i++) {
            int x = sc.nextInt(), y = sc.nextInt();
            cnt[y]++;
            ans[x]--;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (a[i][j] == 1) {
                    ans[i] += cnt[j];
                }
            }
        }
        pw.print(ans[1]);
        for (int i = 2; i <= n; i++) {
            pw.print(" " + ans[i]);
        }
        pw.println();
    }

    static class FastReader {

        StringTokenizer st;
        BufferedReader br;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public boolean hasNext() throws IOException {
            String s = br.readLine();
            if (s == null || s.isEmpty()) {
                return false;
            }
            st = new StringTokenizer(s);
            return true;
        }

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s.isEmpty()) {
                    return null;
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }
    }
}


import java.io.*;
import java.util.*;

public class CF413B {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		int[][] aa = new int[n][m];
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			for (int j = 0; j < m; j++)
				aa[i][j] = Integer.parseInt(st.nextToken());
		}
		int[] xx = new int[n];
		int[] yy = new int[m];
		while (k-- > 0) {
			st = new StringTokenizer(br.readLine());
			int i = Integer.parseInt(st.nextToken()) - 1;
			int j = Integer.parseInt(st.nextToken()) - 1;
			xx[i]++;
			yy[j]++;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; i++) {
			int y = 0;
			for (int j = 0; j < m; j++)
				if (aa[i][j] > 0)
					y += yy[j];
			sb.append(y - xx[i] + " ");
		}
		System.out.println(sb);
	}
}


import java.util.*;
import java.io.*;
import java.lang.*;

public class B413 {

  public static void main(String[] args) {

    int n,m,k;
    int[] chats;
    int[][] rooms;
    int[] count;

    Scanner s = new Scanner(System.in);
    n = s.nextInt();
    m = s.nextInt();
    k = s.nextInt();
    chats = new int[m];
    Arrays.fill(chats, 0);
    count = new int[n];
    Arrays.fill(count, 0);
    rooms = new int[n][m];

    for(int i=0; i<n; i++)
      for(int j=0; j<m; j++)
        rooms[i][j] = s.nextInt();

    for(int i=0; i<k; i++){
      count[s.nextInt()-1]--;
      chats[s.nextInt()-1]++;
    }

    for(int i=0; i<n; i++)
      for(int j=0; j<m; j++)
        count[i] += rooms[i][j]*chats[j];

    System.out.print(count[0]);
    for(int i=1;i<n; i++)
      System.out.print(" " + count[i]);
    System.out.println();
  }
}


import java.io.*;
import java.net.URL;
import java.util.*;

public class Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), " :");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) throws IOException {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    public static void main(String[] args) {
        new Template().run();
        // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    void solve() throws IOException {
        int n = readInt();
        int m = readInt();
        int k = readInt();
        int[] writeCount = new int[n];
        int[] confCount = new int[m];
        int[][] a = new int[n][];
        for (int i = 0; i < n; i++) {
            a[i] = readIntArray(m);
        }
        while (k-- > 0) {
            int x = readInt() - 1;
            int y = readInt() - 1;
            writeCount[x]++;
            confCount[y]++;
        }
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int j = 0; j < m; j++) {
                if (a[i][j] == 1)
                    sum += confCount[j];
            }
            sum -= writeCount[i];
            out.print(sum + " ");
        }
    }

}


import java.io.*;
import java.util.*;
public class chatting
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s[]=br.readLine().trim().split(" ");
        int n=Integer.parseInt(s[0]);
        int m=Integer.parseInt(s[1]);
        int k=Integer.parseInt(s[2]);
        int id[][]=new int[n+1][m+1];
        for(int i=0;i<n;i++)
        {
            s=br.readLine().trim().split(" ");
            for(int j=1;j<=m;j++)
            {
                id[i+1][j]=Integer.parseInt(s[j-1]);
            }
        }
        int count[]=new int[m+1];
        int arr[]=new int[n+1];
        for(int i=1;i<=k;i++)
        {
            s=br.readLine().trim().split(" ");
            arr[Integer.parseInt(s[0])]--;
            count[Integer.parseInt(s[1])]++;
        }
        for(int i=1;i<=n;i++)
        {
            int tot=0;
            for(int j=1;j<=m;j++)
            {
                if(id[i][j]==1)
                tot+=count[j];
            }
            tot+=arr[i];
            System.out.print(tot+" ");
        }
        System.out.println();
    }
}


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Alex
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, OutputWriter out){
	    int nemployees = in.ri(), mchats = in.ri(), kevents = in.ri();
	    int[][] groups = IOUtils.readIntTable(in, nemployees, mchats);
	    int[][] chats = IOUtils.readIntTable(in, kevents, 2);
	    int[] selfchats = new int[nemployees];
	    int[] groupchatted = new int[mchats];
	    for(int[] chat : chats) {
		    int emp = chat[0] - 1, togroup = chat[1] - 1;
		    selfchats[emp]++;
		    groupchatted[togroup]++;
	    }
	    int[] res = new int[nemployees];
	    for(int employ = 0; employ < nemployees; employ++) {
		    for(int g = 0; g < mchats; g++) {
			    if (groups[employ][g] == 1) res[employ] += groupchatted[g];
		    }
	    }
	    for(int i = 0; i < res.length; i++) res[i] -= selfchats[i];
	    for(int i : res) out.print(i + " ");
    }
}

class InputReader {
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int ri(){
		return readInt();
	}

	public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	public static boolean isWhitespace(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void close() {
		writer.close();
	}
}

class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.readInt();
		return array;
	}
	public static int[][] readIntTable(InputReader in, int rowCount, int columnCount) {
		int[][] table = new int[rowCount][];
		for (int i = 0; i < rowCount; i++)
			table[i] = readIntArray(in, columnCount);
		return table;
	}
}


//package codeforces.practice.oneVone.Nov03_2020;

import java.io.*;
import java.util.*;

public class A {
    static InputReader in;
    static PrintWriter out;

    public static void main(String[] args) {
        //initReaderPrinter(true);
        initReaderPrinter(false);
        //solve(in.nextInt());
        solve(1);
    }

    static void solve(int testCnt) {
        for (int testNumber = 0; testNumber < testCnt; testNumber++) {
            int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
            int[][] a = new int[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    a[i][j] = in.nextInt();
                }
            }
            int[] ans = new int[n];
            int[] exclude = new int[n];
            int[] chatCnt = new int[m];
            for(int i = 0; i < k; i++) {
                int x = in.nextInt() - 1;
                int y = in.nextInt() - 1;
                chatCnt[y]++;
                exclude[x]++;
            }
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    if(a[i][j] == 1) {
                        ans[i] += chatCnt[j];
                    }
                }
                ans[i] -= exclude[i];
            }
            for(int v : ans) {
                out.print(v + " ");
            }
            out.println();
        }
        out.close();
    }

    static void initReaderPrinter(boolean test) {
        if (test) {
            try {
                in = new InputReader(new FileInputStream("src/input.in"));
                out = new PrintWriter(new FileOutputStream("src/output.out"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            in = new InputReader(System.in);
            out = new PrintWriter(System.out);
        }
    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        InputReader(InputStream stream) {
            try {
                br = new BufferedReader(new InputStreamReader(stream), 32768);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        Integer[] nextIntArray(int n) {
            Integer[] a = new Integer[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        Long[] nextLongArray(int n) {
            Long[] a = new Long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextLong();
            }
            return a;
        }
    }
}


import java.util.Comparator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        // https://codeforces.com/problemset/problem/413/B
        FastReader sc = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt(), a[][] = new int[n][m], ki, kj, ans[] = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < k; i++) {
            ki = sc.nextInt() - 1;
            kj = sc.nextInt() - 1;
            for (int j = 0; j < n; j++) {
                if (a[j][kj] == 1 && j != ki) {
                    ans[j]++;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            pw.print(ans[i]+" ");
            pw.flush();
        }
        pw.close();
    }

    static class FastReader {

        StringTokenizer st;
        BufferedReader br;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public boolean hasNext() throws IOException {
            String s = br.readLine();
            if (s == null || s.isEmpty()) {
                return false;
            }
            st = new StringTokenizer(s);
            return true;
        }

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s.isEmpty()) {
                    return null;
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }
    }
}


import java.util.Comparator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        // https://codeforces.com/problemset/problem/413/B
        FastReader sc = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt(), a[][] = new int[n][m], ki, kj,p[]=new int[n], c[]=new int[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < k; i++) {
            ki = sc.nextInt() - 1;
            kj = sc.nextInt() - 1;
            p[ki]++;
            c[kj]++;
        }
        for (int i = 0; i < n; i++) {
            int ans=0;
            for (int j = 0; j < m; j++) {
                if (a[i][j]==1) {
                    ans+=c[j];
                }
            }
            pw.print((ans-p[i])+" ");
            pw.flush();
        }
        pw.close();
    }

    static class FastReader {

        StringTokenizer st;
        BufferedReader br;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public boolean hasNext() throws IOException {
            String s = br.readLine();
            if (s == null || s.isEmpty()) {
                return false;
            }
            st = new StringTokenizer(s);
            return true;
        }

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s.isEmpty()) {
                    return null;
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }
    }
}


import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;

public class Main {

	public static void main(String[] args) throws IOException {

		//BufferedReader cin = new BufferedReader(new FileReader("test.txt"));
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));

		String line;
		StringTokenizer st;

		line = cin.readLine();
		st = new StringTokenizer(line);

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());

		//ArrayList<Integer> list = new ArrayList<Integer>(n);
		int[][] matrix = new int[n][m];
		int[] chat = new int[m];//#notification for each chat
		for(int i=0;i<m;i++)chat[i]=0;
		int[] empl = new int[n];//#message each sent
		for(int i=0;i<n;i++)empl[i]=0;

		int t;
		for(int i=0;i<n;i++){
			line = cin.readLine();
			st = new StringTokenizer(line);//Mistake!!!!!! line position matters!
			for(int j=0;j<m;j++){//Mistake!!!!!!!!!!!!! j++, not i++!!!!!!!
				t=Integer.parseInt(st.nextToken());
				matrix[i][j]=t;
			}
		}
		int a,b;
		for(int i=0;i<k;i++){
			line = cin.readLine();
			st = new StringTokenizer(line);
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			empl[a-1]++;
			chat[b-1]++;
		}

		int[] sum=new int[n];
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<n;i++){
			sum[i]=0;
			for(int j=0;j<m;j++){//Mistake!!!!!!!!!!!!!!!same here!!!!!!!!!!!!!!!
				if(matrix[i][j]==1)sum[i]+=chat[j];
			}
			sum[i]-=empl[i];

			if(i>0)sb.append(' ');
			sb.append(sum[i]);

		}

		System.out.println(sb);

		cin.close();
	}

}


import java.util.Scanner;

public class B {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n, m, k;
		int[][] a = new int[20100][11];
//		int[] b = new int[200100];
		int[] rcv = new int[11];
		int[][] sent = new int[20100][11];
		while(sc.hasNext()){
			n = sc.nextInt(); m = sc.nextInt(); k=sc.nextInt();
			for(int i=0; i<n; i++)
				for(int j=0; j<m; j++)
					a[i][j]=sc.nextInt();
			for(int i=0; i<m; i++) rcv[i]=0;
			for(int i=0; i<n; i++) for(int j=0; j<m; j++)sent[i][j]=0;

			for(int i=0; i<k; i++){
				int x = sc.nextInt()-1, y = sc.nextInt()-1;
				rcv[y]++;
				sent[x][y]++;
			}

			for(int i=0; i<n; i++){
				int tsum = 0;
				for(int j=0; j<m; j++) if(a[i][j]==1){
					tsum+=rcv[j];
					tsum-=sent[i][j];
				}
				System.out.print(tsum);
				System.out.print(i==n-1 ? "\n" : " ");
			}
		}
	}

}


import java.util.*;
import java.io.*;

public class sol_413B {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		int[][] map = new int[n][m];
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				map[i][j]= sc.nextInt();
		}
		int[] sol = new int[n];
		for(int i=0;i<k;i++)
		{
			int e = sc.nextInt();
			int me = sc.nextInt();
			e--;
			me--;
			for(int j=0;j<n;j++)
			{
				//System.out.println(e+" "+j);
				if(e!=j && map[j][me]==1)
					sol[j]++;
			}
		}
		for(int x:sol)
			System.out.print(x+" ");
		System.out.println();
	}
}


import java.util.*;
import java.io.*;
import static java.lang.System.out;
public class sol_413B {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		LinkedList<Integer>[] map = new LinkedList[n];
		int[] groups = new int[m];
		int[] send = new int[n];
		for(int i=0;i<n;i++)
		{
			map[i] = new LinkedList<Integer>();
			for(int j=0;j<m;j++)
				if(sc.nextInt()==1)
					map[i].add(j);
		}
		int[] sol = new int[n];
		for(int i=0;i<k;i++)
		{
			int e = sc.nextInt();
			int me = sc.nextInt();
			e--;
			me--;
			groups[me]++;
			send[e]++;
		}
		for(int i=0;i<n;i++)
		{
			int s = 0;
			for(int x:map[i])
			{
				s+= groups[x];
			}
			s-= send[i];
			System.out.print(s+" ");
		}
		System.out.println();
	}
}


import java.util.*;
import java.io.*;

import static java.lang.System.out;
public class sol_413B {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		LinkedList<Integer> map[] = (LinkedList<Integer>[])new LinkedList[m];
		for(int i=0;i<m;i++)
			map[i] = new LinkedList<Integer>();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(sc.nextInt()==1)
				{
					map[j].add(i);
				}
			}
		}
		int[] sol = new int[n];
		for(int i=0;i<k;i++)
		{
			int e = sc.nextInt();
			int me = sc.nextInt();
			e--;
			me--;
			for(int x:map[me])
				sol[x]++;
			sol[e]--;

		}
		for(int x:sol)
			System.out.print(x+" ");
		System.out.println();
	}
}


import java.util.Scanner;

public class b {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // staff, chats, log events
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();

        int[][] membership = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                // member => chat
                membership[i][j] = sc.nextInt();
            }
        }

        int[] members = new int[n], chats = new int[m];

        for (int i = 0; i < k; i++) {
            int x = sc.nextInt() - 1, y = sc.nextInt() - 1;

            chats[y]++; members[x]--;
        }

        sc.close();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (membership[i][j] > 0) {
                    members[i] += chats[j];
                }
            }

            System.out.print(members[i]);
            System.out.print(i + 1 < n ? " " : "\n");
        }
    }
}


import java.util.Scanner;

public class b {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // staff, chats, log events
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();

        int[][] membership = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                // member => chat
                membership[i][j] = sc.nextInt();
            }
        }

        int[][] log = new int[k][2]; // member ID and chat ID

        for (int i = 0; i < k; i++) {
            log[i][0] = sc.nextInt() - 1;
            log[i][1] = sc.nextInt() - 1;
        }

        sc.close();

        int[] notifications = new int[n];

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < n; j++) {
                if (membership[j][log[i][1]] > 0 && j != log[i][0]) {
                    notifications[j]++;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            System.out.print(notifications[i]);
            System.out.print(i + 1 < n ? " " : "\n");
        }
    }
}


import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        int[][] a = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = sc.nextInt();
        int[] c = new int[m];
        int[] res = new int[n];
        for (int i = 0; i < k; i++) {
            int x = sc.nextInt() - 1, y = sc.nextInt() - 1;
            res[x]--;
            c[y]++;
        }
        String[] out = new String[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == 1) {
                    res[i] += c[j];
                }
            }
            out[i] = res[i] + "";
        }
        System.out.println(String.join(" ", out));

    }
}


import java.util.*;
// scanner.nextInt();
public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

		int n = scanner.nextInt();
                int m = scanner.nextInt();
                int k = scanner.nextInt();

                int[][] chats = new int[n][m];

		//  String[] arr = {"a","b","c", "d","e", "f" , "g" , "h", "i" , "j" , "k" , "l", "m", "n", "o", "p", "q" , "r", "s", "t", "u" , "v", "w" ,"x", "y", "z"};

                for(int j = 0; j <n; ++j)
                {
                    for(int l = 0; l < m; ++l)
                    {
                        chats[j][l] = scanner.nextInt();
                    }

                }

                int[] noti = new int[n];

                for(int j = 0; j < k; ++j)
                {
                    int em =  scanner.nextInt();
                    int chat =  scanner.nextInt();

                    for(int l = 0; l < n; ++l)
                    {
                        if(em - 1 != l && chats[l][chat-1] != 0) ++noti[l];
                    }
                }

                for(int i = 0; i < n; ++i) System.out.print(noti[i] + " ");

		/*
                boolean res = false;

                String s = res ? "YES" : "nO";
                */
    }
}


import java.util.*;
// scanner.nextInt();
public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

		int n = scanner.nextInt();
                int m = scanner.nextInt();
                int k = scanner.nextInt();

                int[][] chats = new int[n][m];

		//  String[] arr = {"a","b","c", "d","e", "f" , "g" , "h", "i" , "j" , "k" , "l", "m", "n", "o", "p", "q" , "r", "s", "t", "u" , "v", "w" ,"x", "y", "z"};

                for(int j = 0; j <n; ++j)
                {
                    for(int l = 0; l < m; ++l)
                    {
                        chats[j][l] = scanner.nextInt();
                    }

                }

                int[] noti = new int[n];
                int[] msj = new int[m];

                for(int j = 0; j < k; ++j)
                {
                    int em =  scanner.nextInt();
                    int chat =  scanner.nextInt();

                    ++noti[em-1];
                    ++msj[chat-1];

                }

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

                    for(int j = 0; j < m; ++j)
                    {
                        if (chats[i][j] != 0)
                        {
                            res += msj[j];
                        }
                    }

                    res -= noti[i];
                    System.out.print(res + " ");
                }

		/*
                boolean res = false;

                String s = res ? "YES" : "nO";
                */
    }
}


import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    static void solve() throws Exception {
        int n = nextInt();
        int m = nextInt();
        int k = nextInt();
        int[][] a = new int[n+1][m+1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = nextInt();
            }
        }
        int[] cnt = new int[m+1];
        int[] res = new int[n+1];
        for (int i = 1; i <= k; i++) {
            int x = nextInt();
            int y = nextInt();
            res[x]--;
            cnt[y]++;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (a[i][j] == 1) {
                    res[i] += cnt[j];
                }
            }
            out.print(res[i] + " ");
        }
        out.println();
    }

    static int sqr(int x) {
        return x*x;
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static BigInteger nextBigInteger() throws IOException {
        return new BigInteger(next());
    }

    static String next() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    static String nextLine() throws IOException {
        tok = new StringTokenizer("");
        return in.readLine();
    }

    static boolean hasNext() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return false;
            }
            tok = new StringTokenizer(s);
        }
        return true;
    }

    public static void main(String args[]) {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            //in = new BufferedReader(new FileReader("1.in"));
            //out = new PrintWriter(new FileWriter("1.out"));
            solve();
            in.close();
            out.close();
        } catch (Throwable e) {
            e.printStackTrace();
            java.lang.System.exit(1);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {public static void main(String[] args) throws Exception {new Solve();}}

class Solve { public Solve() throws Exception {solve();}

    void solve() throws Exception {
        int n = NI();
        int m = NI();
        int k = NI();
        String[] a = new String[n];
       // boolean[][] a = new boolean[n][m];
        for (int i = 0; i < n; i++)
             a[i] = NLn();
        int[] res = new int[n];
        for (int i = 0; i < k; i++) {
            int c = NI() - 1;
            int t = NI() - 1;
            for (int j = 0; j < n; j++)
                if (a[j].charAt(t * 2) == '1' && j != c)
                    res[j]++;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++)
            sb.append(res[i]).append(" ");
        System.out.println(sb.toString());
    }

    class Cell {
        int i;
        boolean blocked;
    }

    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");
    static Scanner sc = new Scanner(System.in);
    int min(int i1,int i2){return i1<i2?i1:i2;}
    long min(long i1,long i2){return i1<i2?i1:i2;}
    int max(int i1,int i2){return i1>i2?i1:i2;}
    long max(long i1,long i2){return i1>i2?i1:i2;}
    String NS()throws Exception{while(!st.hasMoreTokens())st=new StringTokenizer(stdin.readLine());return st.nextToken();}
    String NLn() throws Exception {return stdin.readLine();}
    int NI()throws Exception{return Integer.parseInt(NS());}
    long NL()throws Exception{return Long.parseLong(NS());}
    double ND()throws Exception{return Double.parseDouble(NS());}
    int abs(int x){return x<0?-x:x;}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {public static void main(String[] args) throws Exception {new Solve();}}

class Solve { public Solve() throws Exception {solve();}

    void solve() throws Exception {
        int n = NI();
        int m = NI();
        int k = NI();
        boolean[][] a = new boolean[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = NI() == 1;
        int[] res = new int[n];
        for (int i = 0; i < k; i++) {
            int c = NI() - 1;
            int t = NI() - 1;
            for (int j = 0; j < n; j++)
                if (a[j][t] && j != c)
                    res[j]++;
        }
        for (int i = 0; i < n; i++)
            System.out.print(res[i] + " ");
        System.out.println();
    }

    class Cell {
        int i;
        boolean blocked;
    }

    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");
    static Scanner sc = new Scanner(System.in);
    int min(int i1,int i2){return i1<i2?i1:i2;}
    long min(long i1,long i2){return i1<i2?i1:i2;}
    int max(int i1,int i2){return i1>i2?i1:i2;}
    long max(long i1,long i2){return i1>i2?i1:i2;}
    String NS()throws Exception{while(!st.hasMoreTokens())st=new StringTokenizer(stdin.readLine());return st.nextToken();}
    String NLn() throws Exception {return stdin.readLine();}
    int NI()throws Exception{return Integer.parseInt(NS());}
    long NL()throws Exception{return Long.parseLong(NS());}
    double ND()throws Exception{return Double.parseDouble(NS());}
    int abs(int x){return x<0?-x:x;}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {public static void main(String[] args) throws Exception {new Solve();}}

class Solve { public Solve() throws Exception {solve();}

    void solve() throws Exception {
        int n = NI();
        int m = NI();
        int k = NI();
        //String[] a = new String[n];
        HashMap<Integer, Integer> b = new LinkedHashMap<Integer, Integer>();
        boolean[][] a = new boolean[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = NI() == 1;
        int[] res = new int[n];
        for (int i = 0; i < k; i++) {
            int c = NI() - 1;
            int t = NI() - 1;
            res[c]--;
            if (b.get(t) != null)
                b.put(t, b.get(t) + 1);
            else
                b.put(t, 1);
        }
        for (int i = 0; i < m; i++) {
            Integer t = b.get(i);
            if (t != null) {
               for (int j = 0; j < n; j++)
                   if (a[j][i])
                       res[j] += t;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++)
            sb.append(res[i]).append(" ");
        System.out.println(sb.toString());
    }

    class Cell {
        int i;
        boolean blocked;
    }

    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");
    static Scanner sc = new Scanner(System.in);
    int min(int i1,int i2){return i1<i2?i1:i2;}
    long min(long i1,long i2){return i1<i2?i1:i2;}
    int max(int i1,int i2){return i1>i2?i1:i2;}
    long max(long i1,long i2){return i1>i2?i1:i2;}
    String NS()throws Exception{while(!st.hasMoreTokens())st=new StringTokenizer(stdin.readLine());return st.nextToken();}
    String NLn() throws Exception {return stdin.readLine();}
    int NI()throws Exception{return Integer.parseInt(NS());}
    long NL()throws Exception{return Long.parseLong(NS());}
    double ND()throws Exception{return Double.parseDouble(NS());}
    int abs(int x){return x<0?-x:x;}
}