# The problem statement is at
# https://cw.felk.cvut.cz/brute/data/ae/release/2019l_be5b33pge/pge19/evaluation/input.php?task=lpaths
# and also at the end of this file as a comment.


# Be careful with indices, the matrix size in this implementation
# is N+2 x N+2,
# it has an additional artificial border of width 1,
# filled with big values  in rows and columns 0 and N+1.
# The artificial border helps to manipulate the cell which are
# at the border of the original matrix in the same way and by
# the same code as the "regular" cells which are inside the matrix.


import time

N = 0
matrix = []
maxval = 9

def rotateLeft90deg( arr ):
    b = [[0] * (N+2) for i in range(N+2)]
    for i in range(N+2):
        for j in range(N+2):
            b[i][j] = arr[j][N+1-i]
    return b

def tryFindL( a, i ):   # in direction East and North
    origi = i

    # start at the West border of the matrix
    val = a[i][1]
    
    # can we  go East?
    if a[i-1][1] == val or a[i+1][1] == val: return 0,0,0, 0

    # yes, we can

    j = 2
    while True:
        # obvious fails:
        if j > N or a[i][j] != val: return 0,0,0, 0
        # can move one step East?
        if a[i-1][j] != val and a[i+1][j] != val:
            j += 1; continue;
        # found corner down to South?
        if a[i+1][j] != val and a[i][j+1] != val and a[i-1][j] == val: break
        return 0,0,0, 0 # in all other cases
    # now try move North
    i -= 2
    while True:
        # happily arrived to North border?
        if i < 1 : return origi, j, val, (origi+j-1)*val
        # can continue North?
        if a[i][j-1] != val and a[i][j] == val and a[i][j+1] != val:
            i -= 1; continue
        return 0,0,0, 0 # in all other cases

# Instead of looking for L-shaped regions in all 4 possible directions
# we implement only the search in one direction and rotate
# the matrix 3 times by 90 degrees.

def mxValue(arr):
    value = 0
    for k in range(4):
        for i in range (2, N+1):
            ii, jj, val, valsum = tryFindL( arr, i )
            #if val > 0: print( k, ii, jj, val, valsum)
            value += valsum
        if k < 3:
            arr = rotateLeft90deg( arr )
    return value


# Note the creation of the artificial border
def loadOneMx():
    mx = []
    border = [maxval] * (N+2)
    mx.append( border )
    for i in range(N):
        row = list( map( int, input().split() ) )
        mxrow = [maxval]+row +[maxval]
        mx.append(mxrow)
    mx.append( border )
    return mx


# -----------------------------------------------------------------------
#   M A I N
# -----------------------------------------------------------------------
t1 = time.time()
N, K = map(int, input().split())
for k in range(K):
    matrix = loadOneMx()
    print( mxValue(matrix) )
t2 = time.time()
#print( "time %6.3f" % (t2-t1) )


# ---------------------------------------------------------------------------
#       P R O B L E M    S T A T E M E N T
#              with  examples
# ---------------------------------------------------------------------------

'''


L-paths in a Matrix
In this problem, we consider integer square matrices consisting of N×N cells.
The indices of the rows and the columns are 0,1,2, ..., N−1.
Two cells in a matrix are neighbours if they are in the same row
or in the same column and either their row coordinates differ by 1
or their column coordinates differ by 1.

A cell in the matrix interior has four neighbour cells,
a cell in the corner of a matrix has two neighbour cells
and a cell at the border of the matrix and not in the corner has three neighbour cells.

An L-path in a matrix is a set LP of cells with the following properties:

1. All cells in LP contain the same value.
2. Any cell which is not in LP and which is a neighbour of some cell in LP
   contains a different value from the value of cells in LP.
3. There is one specific cell D in LP with coordinates [r, c]
   such that exactly one of the four following conditions is satisfied:
    3.1. All cells with coordinates [r-1, c], [r-2, c], ..., [0, c]
                                and [r, c+1], [r, c+2], ..., [r, N-1]    are in LP.
    3.2. All cells with coordinates [r-1, c], [r-2, c], ..., [0, c]
                                and [r, c-1], [r, c-2], ..., [r, 0]      are in LP.
    3.3. All cells with coordinates [r+1, c], [r+2, c], ..., [N-1, c]
                                and [r, c-1], [r, c-2], ..., [r, 0]      are in LP.
    3.4. All cells with coordinates [r+1, c], [r+2, c], ..., [N-1, c]
                                and [r, c+1], [r, c+2], ..., [r, N-1] are in LP.
4. No other cells in the matrix than those specified in 3. are part of LP.
5. LP consists of at least 3 cells.

The value of an L-path is the sum of values in all its cells.

The L-value of a matrix is the sum of all values of L-paths in the matrix.
If there is no L-path in the matrix, the L-value of the matrix is defined to be 0.




The task
--------
Determine the L-value of each input matrix.


Input
-----
The first input line contains two integers N and K.
Integer N specifies the size of all matrices in the input.
Integer K specifies the number of matrices in the input.
Next, there are exactly K matrices of size N×N occupying K×N lines.
First N lines specify the first matrix, next N lines specify the second matrix and so on.
Each line contains N values, the values correspond to the values in a particular row
in the current matrix. All values are separated by single space.
It holds 1 ≤ N, K ≤ 100, all values in all matrices are positive integers less than 100.

Output
------
The output contains as many text lines as there are matrices in the input.
Each line contains the L-value of the corresponding matrix.
The order of the output values is the same as the order of the input matrices.


Example 1
Input

8 1
4 5 2 3 1 2 2 2
2 2 2 3 1 1 1 1
4 3 3 3 3 3 3 3
7 7 7 7 6 5 5 5
1 1 1 7 6 6 6 6
8 8 1 7 5 5 5 5
3 8 1 7 5 1 1 1
3 3 1 7 5 1 1 1

Output

105

Comment:
The region with 3's in the upper right part of the matrix
is not an L-path.


Example 2
Input

5 3
1 8 1 8 1
8 8 1 8 1
1 1 1 8 1
8 8 8 8 1
1 1 1 1 1
4 4 4 4 4
5 5 5 5 4
5 5 5 5 4
1 1 5 5 4
5 1 5 5 4
7 7 7 2 7
7 7 7 2 2
2 2 2 7 7
7 7 2 7 7
7 7 2 7 7

Output

94
39
16


Example 3
Input

6 2
2 1 2 2 1 2
1 1 2 2 1 1
2 2 6 6 2 2
2 2 6 6 2 2
1 1 2 2 1 1
2 1 2 2 1 2
4 4 4 4 4 4
8 8 8 8 8 8
8 5 5 5 5 1
8 3 3 3 6 1
8 1 1 1 1 1
4 4 4 4 4 4

Output

12
0



'''




