#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k, w;
  cin >> n >> m >> k >> w;
  const int INF = m * n * w + 1;
  char*** a = new char**[k];
  int** d = new int*[k + 1];
  int i, j, x, y;
  d[0] = new int[k + 1];
  for (i = 0; i < k; i++) {
    a[i] = new char*[n];
    d[i + 1] = new int[k + 1];
    d[i + 1][0] = d[0][i + 1] = m * n;
    d[i][i] = INF;
    for (j = 0; j < n; j++) {
      a[i][j] = new char[m];
      for (x = 0; x < m; x++) cin >> a[i][j][x];
    }
    for (j = 0; j < i; j++) {
      d[j + 1][i + 1] = 0;
      for (x = 0; x < n; x++) {
        for (y = 0; y < m; y++) {
          if (a[i][x][y] != a[j][x][y]) d[j + 1][i + 1]++;
        }
      }
      d[j + 1][i + 1] *= w;
      d[i + 1][j + 1] = d[j + 1][i + 1];
    }
  }
  bool* used = new bool[k + 1];
  int *min, *end;
  int v, size;
  int** order = new int*[2];
  order[0] = new int[k];
  order[1] = new int[k];
  min = new int[k + 1];
  end = new int[k + 1];
  for (i = 0; i <= k; i++) {
    min[i] = INF;
    end[i] = 0;
    used[i] = false;
  }
  min[1] = size = 0;
  for (i = 0; i < k; i++) {
    v = -1;
    for (j = 0; j <= k; j++) {
      if (j != v && !used[j] && (v == -1 || min[j] < min[v])) v = j;
    }
    used[v] = true;
    if (v == 0) {
      i--;
      j = 1;
      used[0] = false;
      while (used[j]) j++;
      min[j] = 0;
      end[j] = 0;
    } else {
      order[0][i] = v;
      order[1][i] = end[v];
      size += min[v];
      if (end[v] == 0) size += n * m;
      for (j = 0; j <= k; j++) {
        if (j > 0 && used[j]) continue;
        if (d[v][j] < min[j]) {
          min[j] = d[v][j];
          end[j] = v;
        }
      }
    }
  }
  cout << size << endl;
  for (i = 0; i < k; i++) {
    cout << order[0][i] << " " << order[1][i];
    if (i < k - 1) cout << endl;
  }
  for (i = 0; i < k; i++) {
    delete[] d[i];
    for (j = 0; j < n; j++) delete[] a[i][j];
    delete[] a[i];
  }
  delete[] order[0], order[1], order;
  delete[] min, end, used, a, d;
  return 0;
}
