using namespace std;
#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <utility>
#include <algorithm>
#define pb push_back
#define sz size
#define f first
#define s second
#define II inline
#define ll long long
#define db double
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define all(v) v.begin() , v.end()
#define CC(v) memset((v),0,sizeof((v)))
#define CP(v,w) memcpy((v),(w),sizeof((w)))
#define mp make_pair
#define IN "barabr.in"//"kino.in"
#define OUT "barbar.out"//"kino.out"
#define N_MAX 1<<15
#define M_MAX 1<<6
typedef vector<int> VI;
typedef pair<int,int> pi;
typedef vector<string> VS;
int K,N,M,V[M_MAX];
int A[N_MAX][M_MAX];
int viz[N_MAX],S[N_MAX];
char buffer[1<<10];
II void read(int &aux)
{
for(;buffer[K] < '0' || buffer[K] > '9';++K);
for(aux = 0;buffer[K] >= '0' && buffer[K] <= '9';++K)
aux = aux * 10 + buffer[K] - '0';
}
II void scan()
{
freopen(IN,"r",stdin);
freopen(OUT,"w",stdout);
scanf("%d%d\n",&N,&M);
FOR(j,1,M-1)
scanf("%d",V+j);
scanf("%d\n",&V[M]);
FOR(i,1,N)
{
fgets(buffer,1<<10,stdin);
K = 0;
FOR(j,1,M)
read(A[i][j]);//scanf("%d",&A[i][j]);
}
}
priority_queue<pi,vector<pi>,greater<pi> > Q;
II void solve()
{
int nr;
vector<pi> C(N_MAX);
FOR(j,1,M)
{
CC(viz);
C.resize(0);
for(;!Q.empty();Q.pop() );
V[j] = min(N+1,V[j]);
FOR(i,1,N)
if(A[i][j])
C.pb( mp(A[i][j],i) );
sort(C.begin(),C.end() );
nr = 1;
FOR(i,0,(int)C.sz()-1)
{
if(i && C[i].f != C[i-1].f)
++nr;
A[ C[i].s ][j] = nr;
}
pi poz;
poz = mp(1<<30,1<<30);
FOR(i,1,N)
if(A[i][j])
++viz[ A[i][j] ];
FOR(i,1,V[j])
Q.push( mp(viz[i],i) );
FOR(i,1,N)
{
if(A[i][j])
continue;
pi val = Q.top();
Q.pop();
A[i][j] = val.s;
Q.push( mp(val.f+1,val.s) );
}
}
}
II void count()
{
ll rez(0);
// FOR(i,1,M)
// {
// FOR(j,1,N)
// printf("%d ",A[i][j]);
// printf("\n");
// }
FOR(j,1,M)
{
CC(viz);
CC(S);
FOR(i,1,N)
++viz[ A[i][j] ];
for(int i=V[j];i>=1;--i)
S[i] = S[i+1] + viz[i];
FOR(i,1,N)
rez += (ll)S[i+1] * (ll)viz[i];
}
printf("%lld\n",rez);
}
II void baga_ceva()
{
srand((int)time(0));
freopen(IN,"w",stdout);
printf("20000 50\n");
N = 20000;
M = 50;
FOR(i,1,N)
printf("%d ",rand() % 55 + 1);
printf("\n");
FOR(i,1,N)
{
FOR(j,1,M)
printf("%d ",rand() * rand() + 1);
printf("\n");
}
fclose(stdout);
}
int main()
{
baga_ceva();
scan();
solve();
count();
// freopen(OUT,"w",stdout);
// printf("%d\n", clock() );
return 0;
}