#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(1e3) + 123;
const long long inf = (long long)(1e18);
const int INF = (int)(1e9);
int n, m, k, p[maxn], used[maxn];
char a[maxn][12][12];
queue<int> q;
long long cost, w;
vector<pair<long long, pair<int, int> > > g;
vector<pair<int, int> > ans;
vector<int> e[maxn];
void bfs(int v) {
  used[v] = 1;
  q.push(v);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < e[cur].size(); ++i) {
      int to = e[cur][i];
      if (!used[to]) {
        q.push(to);
        used[to] = 1;
        ans.push_back(make_pair(to, cur));
      }
    }
  }
}
int get(int v) { return (v == p[v]) ? v : (p[v] = get(p[v])); }
void unite(int a, int b) {
  a = get(a);
  b = get(b);
  if (rand() & 1) swap(a, b);
  if (a != b) p[a] = b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> k >> w;
  for (int i = 1; i <= k; ++i) p[i] = i;
  for (int p = 1; p <= k; ++p)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) cin >> a[p][i][j];
  for (int i = 1; i <= k; ++i)
    for (int j = 1; j <= k; ++j) {
      long long cnt = 0ll;
      for (int ii = 1; ii <= n; ++ii)
        for (int jj = 1; jj <= m; ++jj)
          if (a[i][ii][jj] != a[j][ii][jj]) cnt++;
      g.push_back(make_pair(cnt * 1ll * w, make_pair(i, j)));
    }
  for (int i = 1; i <= k; ++i)
    g.push_back(make_pair(n * 1ll * m, make_pair(i, 0)));
  sort(g.begin(), g.end());
  for (int i = 0; i < g.size(); ++i) {
    int v = g[i].second.first, u = g[i].second.second;
    long long len = g[i].first;
    if (get(u) != get(v)) {
      e[u].push_back(v);
      e[v].push_back(u);
      unite(u, v);
      cost += len;
    }
  }
  bfs(0);
  cout << cost << endl;
  for (int i = 0; i < ans.size(); ++i)
    cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}
