#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1004];
int ad[1003][1003];
pair<int, pair<int, int> > p[1000005];
int pr[1005], r[1004];
int find(int n) {
  if (pr[n] == n)
    return n;
  else
    return find(pr[n]);
}
void link(int u, int v) {
  if (r[u] < r[v]) {
    pr[u] = v;
    r[v]++;
  } else {
    pr[v] = u;
    r[u]++;
  }
}
int main() {
  int n, m, k, w, i, j, it, jt;
  cin >> n >> m >> k >> w;
  it = 0;
  string s[1003][12];
  for (i = 0; i <= 1000; i++) pr[i] = i, r[i] = 1;
  while (it < k) {
    for (i = 0; i < n; i++) {
      cin >> s[it][i];
    }
    it++;
  }
  for (it = 0; it < k; it++) {
    for (jt = it + 1; jt < k; jt++) {
      int d = 0;
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if (s[it][i][j] != s[jt][i][j]) d++;
        }
      }
      ad[it][jt] = d * w;
      ad[jt][it] = d * w;
    }
  }
  it = 0;
  for (i = 1; i <= k; i++) {
    for (j = i + 1; j <= k; j++) {
      p[it++] = make_pair(ad[i - 1][j - 1], make_pair(i, j));
    }
  }
  for (i = 0; i < k; i++) p[it++] = make_pair(n * m, make_pair(0, i + 1));
  sort(p, p + it);
  i = 0;
  j = 0;
  vector<pair<int, int> > ans;
  int cnt = 0;
  while (j < it) {
    int u = find(p[j].second.first), v = find(p[j].second.second);
    if (u == v) {
      j++;
      continue;
    }
    i++;
    link(u, v);
    cnt += p[j].first;
    adj[p[j].second.first].push_back(p[j].second.second);
    adj[p[j].second.second].push_back(p[j].second.first);
    j++;
  }
  cout << cnt << endl;
  bool done[1004];
  memset(done, false, sizeof(done));
  done[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int tp = q.front();
    q.pop();
    for (i = 0; i < adj[tp].size(); i++) {
      if (done[adj[tp][i]] == false) {
        cout << adj[tp][i] << ' ' << tp << endl;
        done[adj[tp][i]] = true;
        q.push(adj[tp][i]);
      }
    }
  }
  return 0;
}
