#include <bits/stdc++.h>
using namespace std;
const int M = 1000 + 4, Inf = 1e9 + 10;
char c[M][11][11];
int d[M][M], par[M];
bool mark[M];
vector<int> v[M];
long long ans;
int n, m, k, w;
void dfs(int x) {
  if (par[x] == k && x != k) cout << x + 1 << " " << 0 << endl;
  if (par[x] != k && x != k) cout << x + 1 << " " << par[x] + 1 << endl;
  for (int i = 0; i < v[x].size(); i++) dfs(v[x][i]);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k >> w;
  for (int q = 0; q < k; q++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> c[q][i][j];
  for (int i = 0; i < k; i++)
    for (int j = i + 1; j < k; j++) {
      int D = 0;
      for (int x = 0; x < n && D <= n * m; x++)
        for (int y = 0; y < m && D <= n * m; y++)
          D += (c[i][x][y] != c[j][x][y]) * w;
      d[j][i] = d[i][j] = D;
    }
  for (int i = 0; i < k; i++) d[k][i] = d[i][k] = n * m;
  for (int i = 0; i < k; i++) par[i] = k;
  for (int q = 0; q < k; q++) {
    int mi = 0;
    for (int i = 0; i < k; i++)
      if (!mark[i] && (d[i][par[i]] < d[mi][par[mi]] || mark[mi])) mi = i;
    mark[mi] = 1;
    ans += d[mi][par[mi]];
    v[par[mi]].push_back(mi);
    for (int i = 0; i < k; i++)
      if (d[mi][i] < d[i][par[i]] && !mark[i]) par[i] = mi;
  }
  cout << ans << endl;
  dfs(k);
  return 0;
}
