#include <bits/stdc++.h>
char map[1010][25][25];
int visited[1010];
int distance[1010];
int before[1010];
int q[1010];
int AdjMat[1010][1010];
int n, m, k, w;
void prim() {
  for (int a = 0; a <= k; a++) {
    visited[a] = 0;
    distance[a] = (1 << 30);
    before[a] = -1;
  }
  distance[0] = 0;
  for (int a = 0; a <= k; a++) {
    int currA = -1;
    int currDis = (1 << 30);
    for (int b = 0; b <= k; b++) {
      if (visited[b]) {
        continue;
      }
      if (distance[b] < currDis) {
        currDis = distance[b];
        currA = b;
      }
    }
    if (currA == -1) {
      break;
    }
    if (visited[currA]) {
      continue;
    }
    q[a] = currA;
    visited[currA] = 1;
    for (int b = 0; b <= k; b++) {
      if (visited[b]) {
        continue;
      }
      if (distance[b] > AdjMat[currA][b]) {
        before[b] = currA;
        distance[b] = AdjMat[currA][b];
      }
    }
  }
}
int main() {
  scanf("%d %d %d %d", &n, &m, &k, &w);
  getchar();
  for (int a = 0; a < k; a++) {
    for (int b = 0; b < n; b++) {
      for (int c = 0; c < m; c++) {
        scanf("%c", &map[a][b][c]);
      }
      getchar();
    }
  }
  int total = 0;
  for (int a = 0; a <= k; a++) {
    AdjMat[0][a] = m * n;
    AdjMat[a][0] = m * n;
    for (int b = a + 1; b < k; b++) {
      total = 0;
      for (int x = 0; x < n; x++) {
        for (int y = 0; y < m; y++) {
          if (map[a][x][y] != map[b][x][y]) {
            total++;
          }
        }
      }
      AdjMat[a + 1][b + 1] = w * total;
      AdjMat[b + 1][a + 1] = w * total;
    }
  }
  prim();
  int total2 = 0;
  for (int a = 1; a <= k; a++) {
    total2 += distance[a];
  }
  printf("%d\n", total2);
  for (int a = 1; a <= k; a++) {
    printf("%d %d\n", q[a], before[q[a]]);
  }
  return 0;
}
