#include "tickets.h"
#include <bits/stdc++.h>
using namespace std;
#define DEBUG false
#if DEBUG
void allocate_tickets(vector<vector<int>> arr){
for(int j = 0; j< arr[0].size(); j++){
for(int i = 0; i< arr.size(); i++){
cout << arr[i][j] << " ";
}
cout << endl;
}
}
#endif
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
vector<int> l(n,0);
vector<int> r(n,m-k);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
for(int i = 0; i< n; i++){
pq.push({x[i][r[i]] + x[i][l[i]],i});
}
int nrunder = k*n/2;
for(int i = 0; i< nrunder; i++){
int curi = pq.top().second;
pq.pop();
l[curi]++;
r[curi]++;
if(r[curi] == m) continue;
pq.push({x[i][r[curi]] + x[i][l[curi]],curi});
}
long long totprize = 0;
for(int i = 0; i< n; i++){
for(int j = 0; j< l[i]; j++){
totprize -= x[i][j];
}
for(int j = r[i]; j < m; j++){
totprize += x[i][j];
}
}
// for(int i = 0; i< n; i++){
// cout << l[i] << " ";
// }
// cout << endl;
// for(int i = 0; i< n; i++){
// cout << r[i] << " ";
// }
// cout << endl;
vector<vector<int>> ans(n,vector<int>(m,-1));
int half = n/2;
int forcedl = 0;
int forcedr = 0;
for(int i = 0; i< n; i++){
if(l[i] == 0) forcedr++;
else if(r[i] == m) forcedl++;
}
for(int round = 0; round < k; round++){
int curl = forcedl;
int curr = forcedr;
for(int i = 0; i< n; i++){
if(l[i] == 0) ans[i][r[i]++] = round;
else if(r[i] == m) ans[i][--l[i]] = round;
else if(curl < half){
ans[i][--l[i]] = round;
curl++;
if(l[i] == 0) forcedr++;
}
else{
ans[i][r[i]++] = round;
curr++;
if(r[i] == m) forcedl++;
}
}
}
allocate_tickets(ans);
return totprize;
}
#if DEBUG
int main(){
int n,m,k;
cin >> n >> m >> k;
vector<vector<int>> arr(n,vector<int>(m));
for(auto& row : arr){
for(auto& x : row) cin >> x;
}
int ans = find_maximum(k,arr);
cout << "! " << ans << endl;
}
#endif