#include <bits/stdc++.h>
using namespace std;
int n, m, k, w;
char a[1003][12][12];
int diffA(int A, int B) {
  int res = 0, i, j;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (a[A][i][j] != a[B][i][j]) res++;
  return res;
}
int pre[1003];
long long res, f[1003], c[1003][1003];
bool d[1003];
int l[1003];
void process() {
  int i, j;
  for (i = 1; i <= k; i++) {
    c[i][0] = n * m;
    c[0][i] = n * m;
    c[i][i] = n * m * w;
  }
  for (i = 2; i <= k; i++)
    for (j = 1; j < i; j++) {
      c[i][j] = diffA(i, j) * w;
      c[j][i] = c[i][j];
    }
  memset(d, false, sizeof(d));
  for (i = 0; i <= k; i++) {
    pre[i] = 0;
    f[i] = c[0][i];
  }
  f[0] = 0;
  d[0] = true;
  int u;
  while (true) {
    u = 0;
    for (i = 1; i <= k; i++)
      if (d[i] == false) {
        if ((u == 0) || (f[i] < f[u])) u = i;
      }
    if (u == 0) break;
    d[u] = true;
    for (i = 1; i <= k; i++)
      if ((i != u) && (d[i] == false))
        if (f[i] > c[u][i]) {
          pre[i] = u;
          f[i] = c[u][i];
        }
  }
  res = 0;
  for (i = 1; i <= k; i++) res += f[i];
  ios::sync_with_stdio(false);
  cout << res;
  l[0] = 0;
  int top = 0;
  for (i = 0; i <= top; i++)
    for (j = 1; j <= k; j++)
      if (pre[j] == l[i]) {
        top++;
        l[top] = j;
      }
  for (i = 1; i <= top; i++) cout << "\n" << l[i] << " " << pre[l[i]];
}
int main() {
  scanf("%d%d%d%d\n", &n, &m, &k, &w);
  int i, j;
  for (i = 1; i <= k; i++)
    for (j = 0; j < n; j++) gets(a[i][j]);
  process();
  return 0;
}
