#include <bits/stdc++.h>
using namespace std;
char c[1002][12][12];
int g[1002][1002];
int d[1002], pr[1002];
bool b[1002];
int ans[1002];
int main() {
  int n, m, k, w, i, j, l, r;
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &k);
  scanf("%d", &w);
  for (l = 1; l <= k; l++) {
    for (i = 0; i < n; i++) {
      scanf("%s", c[l][i]);
    }
  }
  for (l = 1; l <= k; l++) {
    g[l][l] = 0;
    for (r = l + 1; r <= k; r++) {
      int cnt = 0;
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (c[l][i][j] != c[r][i][j]) cnt++;
        }
      }
      g[l][r] = g[r][l] = w * cnt;
    }
  }
  for (i = 1; i <= k; i++) {
    g[0][i] = g[i][0] = n * m;
  }
  g[0][0] = 0;
  for (i = 0; i <= k; i++) {
    d[i] = (int)1e9;
    b[i] = true;
    pr[i] = -1;
  }
  d[0] = 0;
  int sum = 0;
  for (l = 0; l <= k; l++) {
    int mn = (int)1e9, km = -1;
    for (i = 0; i <= k; i++) {
      if (b[i] && d[i] < mn) {
        mn = d[i];
        km = i;
      }
    }
    b[km] = false;
    sum += mn;
    ans[l] = km;
    for (j = 0; j <= k; j++) {
      if (b[j] && g[km][j] < d[j]) {
        d[j] = g[km][j];
        pr[j] = km;
      }
    }
  }
  printf("%d\n", sum);
  for (i = 1; i <= k; i++) {
    printf("%d %d\n", ans[i], pr[ans[i]]);
  }
  return 0;
}
