#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int inf = 1e9;
int G[maxn][maxn];
int checked[maxn];
char level[maxn][105];
vector<pair<int, int> > out;
vector<tuple<int, int, int> > edges;
int k;
void printResults(int v) {
  if (checked[v] == 0) return;
  checked[v] = 0;
  for (int i = 1; i <= k; ++i)
    if (G[v][i] == 0 && checked[i] != 0) {
      cout << i << " " << v << endl;
      printResults(i);
    }
}
int main(void) {
  for (int i = 0; i < maxn; ++i)
    for (int j = 0; j < maxn; ++j) {
      G[i][j] = inf;
    }
  int n, m, w;
  cin >> n >> m >> k >> w;
  string s;
  for (int i = 1; i <= k; ++i) {
    for (int j = 0; j < n * m; ++j) {
      scanf(" %c", &(level[i][j]));
    }
    level[i][n * m] = '\0';
  }
  for (int i = 1; i <= k; ++i)
    for (int j = 1; j < i; ++j) {
      int diff = 0;
      for (int l = 0; l < n * m; ++l)
        if (level[i][l] != level[j][l]) ++diff;
      G[j][i] = G[i][j] = diff * w;
    }
  for (int i = 1; i <= k; ++i) G[0][i] = G[i][0] = m * n;
  for (int i = 0; i <= k; ++i) {
    checked[i] = 0;
    G[i][i] = inf;
  }
  for (int i = 0; i <= k; ++i)
    for (int j = 0; j < i; ++j) edges.push_back(make_tuple(i, j, G[i][j]));
  sort(edges.begin(), edges.end(),
       [](const tuple<int, int, int> &t1, const tuple<int, int, int> &t2) {
         return get<2>(t1) < get<2>(t2);
       });
  for (int i = 0; i <= k; ++i)
    for (int j = 0; j <= k; ++j) {
      G[i][j] = inf;
    }
  int ans = 0;
  int comp = 1;
  map<int, int> head;
  head[0] = 0;
  for (auto x : edges) {
    int a = get<0>(x);
    int b = get<1>(x);
    if (checked[a] != checked[b] || checked[a] == 0) {
      if (checked[a] == 0 && checked[b] == 0) {
        checked[a] = checked[b] = comp++;
      } else if (checked[a] == 0) {
        checked[a] = checked[b];
      } else if (checked[b] == 0) {
        checked[b] = checked[a];
      } else {
        int oldcomp = checked[b];
        for (int i = 0; i <= k; ++i)
          if (checked[i] == oldcomp) checked[i] = checked[a];
      }
      G[a][b] = G[b][a] = 0;
      ans += get<2>(x);
    }
  }
  cout << ans << endl;
  printResults(0);
  return 0;
}
