// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;
struct infoE {
int a,b,d;
bool operator<(const infoE &E) const {
return d < E.d;}
};
int main() {
// freopen("skilevel.in","r",stdin);
// freopen("skilevel.out","w",stdout);
int N,M,T;
scanf(" %d %d %d",&N,&M,&T);
vector< vector<int> > H(N, vector<int>(M));
for(int i =0; i < N*M; i++) scanf(" %d",&H[i/M][i%M]);
vector<infoE> E;
vector<int> D(N*M);
int dx[] ={1,-1,0,0};
int dy[] ={0,0,1,-1};
for(int i =0; i < N*M; i++) for(int k =0; k < 4; k++) {
int x =i/M+dx[k], y =i%M+dy[k];
if(x < 0 || y < 0 || x >= N || y >= M) continue;
infoE I;
I.a =x*M+y;
I.b =i;
I.d =abs(H[i/M][i%M]-H[x][y]);
E.push_back(I);}
sort(E.begin(),E.end());
vector< vector<int> > C(N*M);
vector<int> isC(N*M);
for(int i =0; i < N*M; i++) {
C[i].resize(1,i);
isC[i] =i;}
for(uint e =0; e < E.size(); e++) {
int x =E[e].a, y =E[e].b;
if(isC[x] == isC[y]) continue;
x =isC[x], y =isC[y];
if(C[x].size() < C[y].size()) swap(x,y);
// y do x
if((int)(C[x].size()+C[y].size()) >= T) {
if((int)C[x].size() < T)
for(uint j =0; j < C[x].size(); j++) D[C[x][j]] =E[e].d;
if((int)C[y].size() < T)
for(uint j =0; j < C[y].size(); j++) D[C[y][j]] =E[e].d;
}
for(uint j =0; j < C[y].size(); j++) {
isC[C[y][j]] =x;
C[x].push_back(C[y][j]);}
}
long long ans =0;
for(int i =0; i < N*M; i++) {
int a;
scanf(" %d",&a);
ans +=a*(long long)(D[i]);}
printf("%lld\n",ans);
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MAojZGVmaW5lIHBhdGthbiA5CiNkZWZpbmUgdGlzaWMgNDcKI2RlZmluZSBzb2Nsb3NlIDFlLTkKI2RlZmluZSBwaSAzLjE0MTU5MjY1MzU4OTgKI2RlZmluZSBBTExfVEhFKENBS0UsTElFKSBmb3IoYXV0byBMSUUgPUNBS0UuYmVnaW4oKTsgTElFICE9IENBS0UuZW5kKCk7IExJRSsrKQojZGVmaW5lIGNob2NvbGF0ZSB3aW4KI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIGFicyh4KSAoKHggPCAwKT8tKHgpOih4KSkKI2RlZmluZSB1aW50IHVuc2lnbmVkIGludAovLyBteWxpdHRsZXBvbnkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBpbmZvRSB7CglpbnQgYSxiLGQ7CgoJYm9vbCBvcGVyYXRvcjwoY29uc3QgaW5mb0UgJkUpIGNvbnN0IHsKCQlyZXR1cm4gZCA8IEUuZDt9Cgl9OwoKaW50IG1haW4oKSB7Ci8vCWZyZW9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