#include <bits/stdc++.h>
using namespace std;
char ar[1005][12][12];
int br[1005][1005];
bool e[1005] = {0};
int main() {
  int n, m, k, w;
  vector<pair<int, int> > ans;
  long long sum = 0;
  scanf("%d %d %d %d", &n, &m, &k, &w);
  for (__typeof(1) i = 1; i < (k + 1); ++i)
    for (long long j = 0; (j) < (n); ++j)
      for (long long x = 0; (x) < (m); ++x) cin >> ar[i][j][x];
  for (__typeof(1) i = 1; i < (k + 1); ++i) {
    for (__typeof(i + 1) j = i + 1; j < (k + 1); ++j) {
      for (long long x = 0; (x) < (n); ++x) {
        for (long long y = 0; (y) < (m); ++y) {
          if (ar[i][x][y] != ar[j][x][y]) {
            br[i][j] += w;
            br[j][i] += w;
          }
        }
      }
    }
  }
  long long md;
  long long z;
  sum += n * m;
  ans.push_back(make_pair(1, 0));
  e[1] = 1;
  long long p = 0;
  long long y;
  while (1) {
    bool flag = 0;
    md = INT_MAX;
    for (long long i = 0; (i) < (ans.size()); ++i) {
      for (int j = 1; j <= k; j++) {
        if (e[j] == 1) continue;
        if (br[j][ans[i].first] < md) {
          flag = 1;
          md = br[j][ans[i].first];
          z = j;
          y = ans[i].first;
        }
      }
    }
    if (flag == 0) break;
    if (br[z][y] < n * m) {
      sum += br[z][y];
      e[z] = 1;
      ans.push_back(make_pair(z, y));
    } else {
      sum += n * m;
      e[z] = 1;
      ans.push_back(make_pair(z, 0));
    }
  }
  printf("%d\n", sum);
  for (long long i = 0; (i) < (ans.size()); ++i)
    printf("%d %d\n", ans[i].first, ans[i].second);
}
