#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int MMAX = 50005;
const int NMAX = 27;

int a[MMAX][NMAX];
ll mlc[MMAX];
vector<int> v[MMAX];
vector<ll> s[MMAX];

int main() {
  cin.sync_with_stdio(false);

  int n, m;
  cin >> m >> n;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      v[j].pb(a[i][j]);
    }
  }

  for (int j = 1; j <= n; j++) {
    sort(all(v[j]));
    ll sum = 0;
    for (int i = 0; i < m; i++) {
      sum += v[j][i];
      s[j].pb(sum);
    }
  }

  ll best = 1LL << 62;
  for (int i = 1; i <= m; i++) {
    ll cost = 0;
    for (int j = 1; j <= n; j++) {
      int curr = a[i][j];
      int where = upper_bound(v[j].begin(), v[j].end(), curr) - v[j].begin() - 1;

      if (where >= 0) {
        cost += 1LL * curr * (where + 1) - s[j][where];
      }
      if (where < m - 1) {
        cost += (s[j][m - 1] - s[j][where]) - 1LL * curr * (m - 1 - where);
      }
    }
    mlc[i] = cost;
    best = min(best, cost);
  }

  cout << best << '\n';

  vector<vector<int>> sol;
  for (int i = 1; i <= m; i++) {
    if (mlc[i] == best) {
      vector<int> aux;
      for (int j = 1; j <= n; j++) {
        aux.pb(a[i][j]);
      }
      sol.pb(aux);
    }
  }

  sort(all(sol));
  cout << sol.size() << '\n';
  for (auto it : sol) {
    for (auto it2 : it) {
      cout << it2 << " ";
    }
    cout << '\n';
  }

  return 0;
}