#include <bits/stdc++.h>
using namespace std;
char s[1010][12][12];
int n, m, k, w;
vector<pair<int, int> > v[1010];
int r[1010];
int u[1010];
int find(int x) {
  if (r[x] == x) return x;
  int y = find(r[x]);
  r[x] = y;
  return y;
}
int unite(int x, int y) {
  int a = find(x), b = find(y);
  if (rand() % 2)
    r[a] = b;
  else
    r[b] = a;
}
int ans;
vector<pair<int, int> > res;
int was[1010];
set<pair<int, pair<int, int> > > q;
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &w);
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) scanf("%s", s[i][j]);
  }
  for (int i = 1; i <= k; i++) {
    v[0].push_back(make_pair(n * m, i));
    v[i].push_back(make_pair(n * m, 0));
  }
  for (int i = 1; i <= k; i++)
    for (int j = i + 1; j <= k; j++) {
      int df = 0;
      for (int z = 0; z < n; z++)
        for (int k = 0; k < m; k++) df += (s[i][z][k] != s[j][z][k]);
      v[i].push_back(make_pair(df * w, j));
      v[j].push_back(make_pair(df * w, i));
    }
  for (int i = 0; i <= k; i++) sort(v[i].begin(), v[i].end());
  was[0] = 1;
  u[0] = 1;
  ans += v[0][0].first;
  was[v[0][0].second] = 1;
  res.push_back(make_pair(0, v[0][0].second));
  for (int it = 2; it <= k; it++) {
    for (int j = 0; j <= k; j++) {
      while (u[j] < v[j].size()) {
        if (was[v[j][u[j]].second])
          u[j]++;
        else
          break;
      }
    }
    int mint = (1 << 20);
    pair<int, int> best = make_pair(-1, -1);
    for (int j = 0; j <= k; j++) {
      if (was[j] == 0 || u[j] == v[j].size()) continue;
      if (v[j][u[j]].first < mint)
        mint = v[j][u[j]].first, best = make_pair(j, v[j][u[j]].second);
    }
    ans += mint;
    res.push_back(best);
    was[best.second] = 1;
  }
  cout << ans << endl;
  for (int i = 0; i < res.size(); i++)
    cout << res[i].second << " " << res[i].first << endl;
  return 0;
}
