#include <bits/stdc++.h>
using namespace std;
int n, m, k, w;
struct data {
  char ch[15][15];
} level[1010];
int dis[1010][1010], low[1010], vis[1010];
int num[1010];
struct pp {
  int from, to;
};
queue<pp> q;
int check(int x, int y) {
  int sum = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (level[x].ch[i][j] != level[y].ch[i][j]) sum++;
    }
  return sum * w;
}
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%s", level[i].ch[j] + 1);
    }
  }
  for (int i = 1; i <= k; i++) {
    dis[0][i] = dis[i][0] = n * m;
    for (int j = i + 1; j <= k; j++) {
      int v = check(i, j);
      dis[i][j] = dis[j][i] = v;
    }
  }
  int i, j, pos, min, result = 0;
  memset(vis, 0, sizeof(vis));
  vis[0] = 1, pos = 0;
  for (i = 1; i <= k; i++)
    if (i != pos) {
      low[i] = dis[pos][i];
      num[i] = 0;
    }
  pp temp;
  for (i = 1; i <= k; i++) {
    min = 999999999;
    for (j = 1; j <= k; j++)
      if (vis[j] == 0 && min > low[j]) {
        min = low[j];
        pos = j;
      }
    result += min;
    vis[pos] = 1;
    temp.from = pos, temp.to = num[pos];
    q.push(temp);
    for (j = 1; j <= k; j++)
      if (vis[j] == 0 && low[j] > dis[pos][j]) {
        low[j] = dis[pos][j];
        num[j] = pos;
      }
  }
  printf("%d\n", result);
  while (!q.empty()) {
    pp temp = q.front();
    q.pop();
    printf("%d %d\n", temp.from, temp.to);
  }
  return 0;
}
