#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b, long long c) {
  long long res = a + b;
  return (res >= c ? res - c : res);
}
long long mod_neg(long long a, long long b, long long c) {
  long long res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
long long mul(long long a, long long b, long long c) {
  long long res = (long long)a * b;
  return (res >= c ? res % c : res);
}
template <typename T>
T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T>
T mdpower(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T>
T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T>
T mod_inverse(T a, T n) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
string arr[1002][11];
long long parent[1002];
long long size[1002];
vector<long long> vv[1002];
long long root = -1;
vector<pair<long long, pair<long long, long long>>> edges;
void init() {
  for (long long i = 1; i < 1001; i++) {
    parent[i] = i;
    size[i] = 1;
  }
}
long long getParent(long long a) {
  while (a != parent[a]) {
    parent[a] = parent[parent[a]];
    a = parent[a];
  }
  return a;
}
void union_(long long a, long long b) {
  long long root_A = getParent(a);
  long long root_B = getParent(b);
  if (root_A == root_B) return;
  if (size[root_A] < size[root_B]) {
    parent[root_A] = parent[root_B];
    size[root_B] += size[root_A];
  } else {
    parent[root_B] = parent[root_A];
    size[root_A] += size[root_B];
  }
}
long long kruskal(long long n) {
  long long cost = 0;
  long long k = 0;
  long long count = 0;
  while (count < n) {
    long long weight = edges[k].first;
    pair<long long, long long> edge = edges[k].second;
    long long u = edge.first;
    long long v = edge.second;
    if (root == -1) root = u;
    long long aa = getParent(u);
    long long bb = getParent(v);
    if (aa == bb) {
      k++;
      continue;
    } else {
      cost += weight;
      union_(u, v);
      vv[u].push_back(v);
      vv[v].push_back(u);
      k++;
      count++;
    }
  }
  return cost;
}
void dfs(long long s, long long par) {
  if (s != 0) {
    if (par != -1)
      cout << s << " " << par << "\n";
    else
      cout << s << " " << 0 << "\n";
  }
  for (auto it : vv[s]) {
    if (it == par) continue;
    dfs(it, s);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  long long n, m, k1, w;
  cin >> n >> m >> k1 >> w;
  for (long long i = 1; i <= k1; i++) {
    for (long long j = 1; j <= n; j++) cin >> arr[i][j];
  }
  long long maxCost = n * m;
  for (long long i = 1; i <= k1; i++) {
    for (long long j = i + 1; j <= k1; j++) {
      if (i == j) continue;
      long long dij = 0;
      for (long long k = 1; k <= n; k++) {
        for (long long l = 0; l < m; l++) {
          if (arr[i][k][l] != arr[j][k][l]) dij++;
        }
      }
      long long totcost = dij * w;
      edges.push_back({totcost, {i, j}});
    }
  }
  for (long long i = 1; i <= k1; i++) {
    edges.push_back({n * m, {0, i}});
  }
  sort(edges.begin(), edges.end());
  cout << kruskal(k1) << "\n";
  dfs(root, -1);
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
