import math, copy

f = open ('DATA5.txt','r')
f1 = open('OUT5.txt','w')
def peasy(a):
    for x in range (len(a)):
        print(a[x])
for p in range (5):
    line = f.readline()
    line = line.split()
    N = int(line[0])
    M = int(line[1])
    m=[]
    for x in range(N):
        line = f.readline()
        line = line.strip("\n")
        m.append(line.split())
        for y in range (M):
            m[x][y]= int(m[x][y])
    MH = 0
    for x in m:
        if max(x)>MH:
            MH = max(x)

    a = []

    for x in range (MH):
        a.append([])
        for y in range(N):
            a[x].append([])
            for z in range (M):
                a[x][y].append(0)
                if m[y][z] < x+1:
                    a[x][y][z]=0
                else:
                    a[x][y][z] = 1
    vt = 0
    
    for q in range (MH):
        r = copy.deepcopy(a[q])
        for x in range (N):
            for y in range (M):
                if r[x][y] == 0:
                    s=[]
                    s.append([x,y])
                    r[x][y]=2
                    add = True
                    v = 0
                    while len(s)>0:
                        v+= 1
                        if s[0][0]==0 or s[0][0] == N-1 or s[0][1] == 0 or s [0][1]==M-1:
                            add = False
                        if s[0][0]<N-1:
                            if r[s[0][0]+1][s[0][1]]==0:
                                s.append([s[0][0]+1,s[0][1]])
                                r[s[0][0]+1][s[0][1]]=2
                        if s[0][0]>0:
                            if r[s[0][0]-1][s[0][1]]==0:
                                s.append([s[0][0]-1,s[0][1]])
                                r[s[0][0]-1][s[0][1]]=2
                        if s[0][1]<M-1:
                            if r[s[0][0]][s[0][1]+1]==0:
                                s.append([s[0][0],s[0][1]+1])
                                r[s[0][0]][s[0][1]+1]=2
                        if s[0][1]>0:
                            if r[s[0][0]][s[0][1]-1]==0:
                                s.append([s[0][0],s[0][1]-1])
                                r[s[0][0]][s[0][1]-1]=2
                        del s[0]
                    if add:
                        vt += v
    f1.write(str(vt))
    f1.write("\n")
f1.close()
f.close()



