#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n, m, k, w, x[5000][5000], ans[5000], parent[5000], sz[5000];
pair<int, pair<int, int>> p[3000000];
char a[1500][N][N];
bool used[5000];
vector<int> g[5000];
void make(int v) {
  parent[v] = v;
  sz[v] = 1;
}
int find(int v) {
  if (parent[v] == v) return v;
  return parent[v] = find(parent[v]);
}
void merge(int u, int v) {
  u = find(u);
  v = find(v);
  if (sz[u] > sz[v]) {
    sz[u] += sz[v];
    parent[v] = u;
  } else {
    sz[v] += sz[u];
    parent[u] = v;
  }
}
void dfs(int v, int p) {
  ans[v] = p;
  for (int i = 0; i < g[v].size(); i++)
    if (g[v][i] != p) dfs(g[v][i], v);
}
void dfs1(int v, int p) {
  printf("%d %d\n", v, ans[v]);
  for (int i = 0; i < g[v].size(); i++)
    if (g[v][i] != p) dfs1(g[v][i], v);
}
int main() {
  scanf("%d%d%d%d\n", &n, &m, &k, &w);
  for (int r = 1; r <= k; r++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) scanf("%c", &a[r][i][j]);
      scanf("\n");
    }
  }
  for (int i1 = 1; i1 <= k; i1++)
    for (int i2 = 1; i2 <= k; i2++) {
      x[i1][i2] = 0;
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
          if (a[i1][i][j] != a[i2][i][j]) x[i1][i2]++;
    }
  int l = 0;
  for (int i = 1; i <= k; i++)
    for (int j = i + 1; j <= k; j++)
      p[++l] = make_pair(min(x[i][j] * w, n * m), make_pair(i, j));
  sort(p + 1, p + l + 1);
  int res = n * m;
  for (int i = 1; i <= k; i++) make(i);
  for (int i = 1; i <= l; i++) {
    int u = p[i].second.first, v = p[i].second.second;
    if (find(u) != find(v)) {
      g[u].push_back(v);
      g[v].push_back(u);
      merge(u, v);
    }
  }
  dfs(1, 0);
  for (int i = 2; i <= k; i++) {
    res += min(x[i][ans[i]] * w, n * m);
    if (n * m < x[i][ans[i]] * w) ans[i] = 0;
  }
  printf("%d\n", res);
  dfs1(1, 0);
  return 0;
}
