#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> parent;
vector<int> a[1005];
bool vis[1005];
bool d[1001][1001];
char g[1005][12][12];
int n, m, k;
struct Edge {
  int vert1;
  int vert2;
  int weight;
  Edge(int v1, int v2, int w) : vert1(v1), vert2(v2), weight(w) {}
};
vector<Edge> edges1;
vector<Edge> A;
void init() {
  for (int i = 0; i <= k; i++) {
    parent[i] = i;
  }
}
int Find(int i) {
  if (parent[i] == i) return i;
  return parent[i] = Find(parent[i]);
}
bool Union(int i, int j) {
  int r_i = Find(i);
  int r_j = Find(j);
  if (r_i == r_j) return false;
  parent[r_i] = r_j;
  return true;
}
int an;
int Kruskal() {
  init();
  sort(edges1.begin(), edges1.end(),
       [](Edge x, Edge y) { return x.weight < y.weight; });
  for (int j = 0; j < edges1.size(); j++) {
    if (Union(edges1[j].vert1, edges1[j].vert2)) {
      A.push_back(edges1[j]);
    }
  }
  int ans = 0;
  for (Edge e : A) {
    a[e.vert1].push_back(e.vert2);
    a[e.vert2].push_back(e.vert1);
    ans += e.weight;
  }
  cout << ans << endl;
  return ans;
}
int main() {
  int w;
  cin >> n >> m >> k >> w;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      for (int c = 0; c < m; c++) {
        scanf(" %c", &g[i][j][c]);
      }
    }
  }
  int count = 0;
  for (int i = 1; i <= k; i++) {
    edges1.push_back(Edge(0, i, n * m));
    for (int j = i + 1; j <= k; j++) {
      for (int c = 0; c < n; c++) {
        for (int t = 0; t < m; t++) {
          if (g[i][c][t] != g[j][c][t]) count++;
        }
      }
      edges1.push_back(Edge(i, j, count * w));
      count = 0;
    }
  }
  Kruskal();
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    if (vis[i]) continue;
    vis[i] = true;
    int s = a[i].size();
    for (int t = 0; t < s; t++) {
      int j = a[i][t];
      if (!d[i][j]) {
        cout << j << " " << i << endl;
        d[i][j] = true, d[j][i] = true;
        q.push(j);
      }
    }
  }
  return 0;
}
