#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:134217728")
using namespace std;
int n, m, k, w;
char s[1 << 10][1 << 4][1 << 4];
int G[1 << 10][1 << 10];
int used[1 << 10];
int d[1 << 10];
int v[1 << 10];
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < n; ++j) scanf("%s", s[i][j]);
  memset(G, 0, sizeof(G));
  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      int diff = 0;
      for (int r = 0; r < n; ++r)
        for (int c = 0; c < m; ++c) diff += s[i][r][c] != s[j][r][c];
      G[i][j] = diff * w;
      G[j][i] = diff * w;
    }
  }
  memset(used, 0, sizeof(used));
  used[0] = 1;
  int res = n * m;
  vector<int> A, B;
  for (int i = 1; i < k; ++i) {
    d[i] = G[i][0];
    v[i] = 0;
  }
  A.push_back(1);
  B.push_back(0);
  for (int i = 0; i < k - 1; ++i) {
    int u = -1;
    for (int j = 0; j < k; ++j)
      if (!used[j] && (u == -1 || d[u] > d[j])) u = j;
    if (n * m < d[u]) {
      res += n * m;
      A.push_back(u + 1);
      B.push_back(0);
    } else {
      res += d[u];
      A.push_back(u + 1);
      B.push_back(v[u] + 1);
    }
    used[u] = 1;
    for (int j = 0; j < k; ++j)
      if (!used[j]) {
        if (d[j] > G[u][j]) {
          d[j] = G[u][j];
          v[j] = u;
        }
      }
  }
  printf("%d\n", res);
  for (int i = 0; i < k; ++i) printf("%d %d\n", A[i], B[i]);
  return 0;
}
