#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
vector<int> g[1010];
int n;
vector<pair<long long, pair<int, int> > > ejes;
int _cl[10000];
int cl(int i) { return (_cl[i] == -1 ? i : _cl[i] = cl(_cl[i])); }
void join(int i, int j) {
  if (cl(i) != cl(j)) _cl[cl(i)] = cl(j);
}
long long krus() {
  if (n == 1) return 0;
  sort(ejes.begin(), ejes.end());
  int u = 0;
  long long t = 0;
  memset(_cl, -1, sizeof(_cl));
  for (int i = 0; i < (int)ejes.size(); i++) {
    pair<long long, pair<int, int> >& e = ejes[i];
    if (cl(e.second.first) != cl(e.second.second)) {
      int v1 = e.second.first;
      int v2 = e.second.second;
      g[v1].push_back(v2);
      g[v2].push_back(v1);
      u++;
      t += e.first;
      if (u == n - 1) return t;
      join(e.second.first, e.second.second);
    }
  }
  return -1;
}
int R, C, k, w;
char t[1010][11][11];
int dif[1010][1010];
void dfs(int p, int v) {
  bool zero = (p == -1);
  if (p != -1) zero |= (dif[p][v] * w >= R * C);
  if (zero) {
    cout << v + 1 << " " << 0 << endl;
  } else {
    cout << v + 1 << " " << p + 1 << endl;
  }
  for (int i = 0; i < (int)g[v].size(); i++) {
    int u = g[v][i];
    if (u != p) {
      dfs(v, u);
    }
  }
}
int main() {
  cin >> R >> C >> k >> w;
  n = k;
  for (int i = 0; i < (int)k; i++) {
    for (int r = 0; r < (int)R; r++) {
      for (int c = 0; c < (int)C; c++) {
        cin >> t[i][r][c];
      }
    }
  }
  for (int i = 0; i < k; i++) {
    for (int j = i + 1; j < k; j++) {
      dif[i][j] = 0;
      for (int r = 0; r < R; r++) {
        for (int c = 0; c < C; c++) {
          if (t[i][r][c] != t[j][r][c]) dif[i][j]++;
        }
      }
      dif[j][i] = dif[i][j];
    }
  }
  for (int i = 0; i < (int)k; i++) {
    for (int j = 0; j < (int)k; j++)
      if (i != j) {
        int weight = min(R * C, dif[i][j] * w);
        ejes.push_back(make_pair(weight, make_pair(i, j)));
      }
    ejes.push_back(make_pair(100000000, make_pair(i, i)));
  }
  long long resCost = krus();
  int degs = 0;
  for (int i = 0; i < (int)k; i++) degs += g[i].size();
  assert(degs == 2 * (k - 1));
  resCost = resCost + R * C;
  cout << resCost << endl;
  for (int i = 0; i < n; i++) {
    if (g[i].size() <= 1) {
      dfs(-1, 0);
      break;
    }
  }
  return 0;
}
