#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, t, w, ans, d[N], o[N], fa[N];
char s[N][15][15];
int dist(int x, int y) {
  int c = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) c += s[x][i][j] != s[y][i][j];
  return c;
}
int main() {
  scanf("%d%d%d%d", &n, &m, &t, &w);
  for (int i = 1; i <= t; ++i) {
    d[i] = n * m;
    for (int j = 1; j <= n; ++j) scanf("%s", s[i][j] + 1);
  }
  for (int i = 1; i <= t; ++i) {
    int D = n * m + 1, k;
    for (int j = 1; j <= t; ++j)
      if (d[j] >= 0 && d[j] < D) D = d[j], k = j;
    o[++*o] = k, ans += d[k], d[k] = -1;
    for (int j = 1; j <= t; ++j)
      if (d[j] >= 0) {
        int tmp = dist(j, k) * w;
        if (tmp < d[j]) d[j] = tmp, fa[j] = k;
      }
  }
  printf("%d\n", ans);
  for (int i = 1; i <= t; ++i) printf("%d %d\n", o[i], fa[o[i]]);
  return 0;
}
