#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
const int inf = 2e9 + 5;
string s[maxn][15];
int d[maxn][maxn], gg[maxn], ans[maxn], pre[maxn];
bool vis[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k, w;
  cin >> n >> m >> k >> w;
  for (int i = 1; i <= k; i++)
    for (int j = 0; j < n; j++) cin >> s[i][j];
  for (int i = 1; i <= k; i++) {
    for (int j = i + 1; j <= k; j++) {
      int cnt = 0;
      for (int x = 0; x < n; x++)
        for (int y = 0; y < m; y++) {
          if (s[i][x][y] != s[j][x][y]) cnt++;
        }
      d[i][j] = d[j][i] = w * cnt;
    }
  }
  for (int i = 1; i <= k; i++) d[i][0] = d[0][i] = n * m;
  for (int i = 1; i <= k; i++) gg[i] = inf;
  int res = 0;
  for (int i = 0; i < k + 1; i++) {
    int dd = inf, id = -1;
    for (int j = 0; j < k + 1; j++)
      if (!vis[j] && dd > gg[j]) {
        dd = gg[j];
        id = j;
      }
    vis[id] = 1;
    ans[i] = id;
    res += dd;
    for (int j = 0; j < k + 1; j++)
      if (!vis[j] && d[id][j] < gg[j]) {
        pre[j] = id;
        gg[j] = d[id][j];
      }
  }
  cout << res << '\n';
  for (int i = 1; i <= k; i++) cout << ans[i] << ' ' << pre[ans[i]] << '\n';
  return 0;
}
