#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char lev[N][10][10];
int par[N], sz[N];
bool was[N];
vector<int> g[N];
void uni(int a, int b) {
  if (sz[a] < sz[b]) {
    par[a] = b;
    sz[b] += sz[a];
  } else {
    par[b] = a;
    sz[a] += sz[b];
  }
}
int get(int v) {
  if (par[v] == v) return v;
  par[v] = get(par[v]);
  return par[v];
}
vector<pair<int, int> > edg[105];
void dfs(int v) {
  was[v] = 1;
  for (int i = 0; i < (int)(g[v]).size(); i++) {
    int to = g[v][i];
    if (!was[to]) {
      cout << to << " " << v << endl;
      dfs(to);
    }
  }
}
int main() {
  int n, m, k, w;
  cin >> n >> m >> k >> w;
  for (int i = 1; i <= k; i++) {
    par[i] = i;
    sz[i] = 1;
    for (int x = 0; x < n; x++)
      for (int y = 0; y < m; y++) {
        cin >> lev[i][x][y];
      }
  }
  int ans = 0, edges = k;
  for (int i = 1; i <= k; i++)
    for (int j = i + 1; j <= k; j++) {
      int dif = 0;
      for (int x = 0; x < n; x++)
        for (int y = 0; y < m; y++)
          if (lev[i][x][y] != lev[j][x][y]) dif += w;
      if (dif < n * m) edg[dif].push_back(make_pair(i, j));
    }
  for (int i = 0; i < n * m; i++) {
    for (int j = 0; j < (int)(edg[i]).size(); j++) {
      int v1 = edg[i][j].first;
      int v2 = edg[i][j].second;
      int V1 = get(v1);
      int V2 = get(v2);
      if (V1 != V2) {
        uni(V1, V2);
        ans += i;
        g[v1].push_back(v2);
        g[v2].push_back(v1);
        edges--;
      }
    }
  }
  cout << edges * n * m + ans << endl;
  for (int i = 1; i <= k; i++)
    if (!was[i]) {
      cout << i << " " << 0 << endl;
      dfs(i);
    }
  return 0;
}
