# 벌꿀채취 - SWEA
from itertools import combinations
def internal(x, y):
cnt = 0
for i in range(M):
if -1 < y + i < N:
cnt += 1
return 1 if cnt == M else 0
def calculate(x, y):
li = []
for i in range(M):
tmp = a[x][y + i]
li.append(tmp)
sums = []
for i in range(M):
for c in combinations(li, M - i):
if sum(c) <= C:
temp = 0
for j in c:
temp += (j ** 2)
sums.append(temp)
return max(sums)
def value(x, y):
pre = calculate(x, y)
# for second
case = []
for i in range(x, N):
if i == x: # 같은 라인이면
idx = y + M
else:
idx = 0
for j in range(idx, N):
if internal(i, j):
case.append(calculate(i, j))
if case: return max(case) + pre
else: return pre
T = int(input())
for test_case in range(1, T + 1):
N, M, C = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
ret = 0
for i in range(N):
for j in range(N):
if internal(i, j):
ret = max(ret, value(i, j))
print("#" + str(test_case) + " " + str(ret))