#include <bits/stdc++.h>
using namespace std;
int ar[1002][1002];
char buf[20];
int main() {
  int n, m, k, w;
  int res = 0;
  vector<pair<int, int> > r;
  scanf("%d %d %d %d\n", &n, &m, &k, &w);
  vector<vector<string> > f(k);
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      fgets(buf, 20, stdin);
      string s = buf;
      s = s.substr(0, s.size() - 1);
      f[i].push_back(s);
    }
  }
  for (int i = 0; i < f.size(); i++) {
    for (int j = i + 1; j < f.size(); j++) {
      int d = 0;
      for (int e = 0; e < n; e++) {
        for (int l = 0; l < m; l++) {
          if (f[i][e][l] != f[j][e][l]) d++;
        }
      }
      if (d * w < n * m) {
        ar[i + 1][j + 1] = d * w;
        ar[j + 1][i + 1] = d * w;
      } else {
        ar[i + 1][j + 1] = 1000;
        ar[j + 1][i + 1] = 1000;
      }
    }
  }
  for (int i = 0; i < k + 1; i++) {
    ar[0][i] = n * m;
    ar[i][0] = n * m;
  }
  vector<char> used(k + 1, false);
  vector<int> min_edge(k + 1, 1000), end_edge(k + 1, -1);
  min_edge[0] = 0;
  for (int i = 0; i < k + 1; i++) {
    int v = -1;
    for (int j = 0; j < k + 1; j++) {
      if (!used[j] && (v == -1 || min_edge[j] < min_edge[v])) v = j;
    }
    used[v] = true;
    if (end_edge[v] != -1) {
      res += ar[v][end_edge[v]];
      r.push_back(pair<int, int>(v, end_edge[v]));
    }
    for (int j = 0; j < k + 1; j++) {
      if (ar[v][j] < min_edge[j]) {
        min_edge[j] = ar[v][j];
        end_edge[j] = v;
      }
    }
  }
  printf("%d\n", res);
  for (int i = 0; i < r.size(); i++) {
    printf("%d %d\n", r[i].first, r[i].second);
  }
  return 0;
}
