#include <bits/stdc++.h>
using namespace std;
int n, yy, zz, w, cur[1005], from[1005], val[1005][1005], ans, s[1005], top;
char mp[1005][12][12];
bool vis[1005];
vector<int> e[1005];
void dfs(int c) {
  vis[c] = true;
  for (__typeof(e[c].begin()) i = e[c].begin(); i != e[c].end(); i++)
    if (!vis[*i]) dfs(*i);
  s[top++] = c;
}
int main() {
  while (~scanf("%d %d %d %d", &yy, &zz, &n, &w)) {
    top = 0;
    ans = 0;
    memset(vis, false, sizeof(vis));
    memset(val, 0, sizeof(val));
    memset(from, 0, sizeof(from));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= yy; j++) scanf("%s", mp[i][j] + 1);
    for (int i = 1; i <= n; i++) cur[i] = yy * zz;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++)
        for (int x = 1; x <= yy; x++)
          for (int y = 1; y <= zz; y++)
            if (mp[i][x][y] != mp[j][x][y]) {
              val[i][j] += w;
              val[j][i] += w;
            }
    int id;
    for (int cnt = 1; cnt <= n; cnt++) {
      id = 0;
      for (int i = 1; i <= n; i++)
        if (!vis[i]) {
          if (!id || cur[i] < cur[id]) id = i;
        }
      vis[id] = true;
      ans += cur[id];
      for (int i = 1; i <= n; i++)
        if (!vis[i])
          if (cur[i] > val[id][i]) {
            cur[i] = min(cur[i], val[id][i]);
            from[i] = id;
          }
    }
    printf("%d\n", ans);
    for (int i = 1; i <= n; i++) e[i].clear();
    for (int i = 1; i <= n; i++) e[from[i]].push_back(i);
    memset(vis, false, sizeof(vis));
    dfs(0);
    for (int i = top - 2; i >= 0; i--) printf("%d %d\n", s[i], from[s[i]]);
  }
  return 0;
}
