#include <bits/stdc++.h>
using namespace std;
int pa[2000];
int ra[2000];
void init(int n) {
  for (int i = 0; i < n; i++) {
    pa[i] = i;
    ra[i] = 0;
  }
}
int find(int x) {
  if (pa[x] != x) pa[x] = find(pa[x]);
  return pa[x];
}
int unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return 0;
  if (ra[x] < ra[y]) {
    pa[x] = y;
  } else {
    pa[y] = x;
    if (ra[x] == ra[y]) ra[x]++;
  }
  return 1;
}
bool same(int x, int y) { return find(x) == find(y); }
int n, m, k, w;
char mz[2000][10][11];
int diff(int a, int b) {
  int c = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (mz[a][i][j] != mz[b][i][j]) c++;
    }
  }
  return c;
}
int G[2000][2000];
int vis[2000];
void dfs(int u) {
  vis[u] = 1;
  for (int v = 0; v < k; v++) {
    if (!vis[v] && G[u][v]) {
      cout << v + 1 << ' ' << u + 1 << endl;
      dfs(v);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 less<pair<int, int> > >
      q;
  cin >> n >> m >> k >> w;
  init(k);
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) cin >> mz[i][j];
    for (int j = 0; j < i; j++) {
      int d = diff(i, j);
      if (d * w < n * m) q.push(make_pair(n * m - d * w, i * 2000 + j));
    }
  }
  long long ans = n * m * k;
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int d = p.first;
    int x = p.second / 2000;
    int y = p.second % 2000;
    if (!same(x, y)) {
      unite(x, y);
      G[x][y] = G[y][x] = 1;
      ans -= d;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < k; i++) {
    if (!vis[i]) {
      cout << i + 1 << ' ' << 0 << endl;
      dfs(i);
    }
  }
  return 0;
}
