#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1000 + 10;
const int maxn5 = 5 * 1000 * 100 + 10;
const int maxn3 = 1000 + 10;
const long long mod = 1000 * 1000 * 1000 + 7;
const long long inf = 1LL * 2 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10;
set<pair<long long, int>> av;
bool mark[maxn5];
int mn[maxn5], a[maxn5], b[maxn5], num = 1;
long long len[maxn5];
vector<pair<int, int>> adj[maxn5];
vector<pair<int, int>> edge;
string s[maxn3][20];
long long x, y, k, w;
void add_edge(int v, int u) {
  a[num] = u;
  b[num] = v;
  for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++)
      if (s[v][i][j] != s[u][i][j]) len[num] += w;
  adj[v].push_back({u, num});
  adj[u].push_back({v, num});
  num++;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> x >> y >> k >> w;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < x; j++) cin >> s[i][j];
  for (int i = 0; i < k; i++)
    for (int j = i + 1; j < k; j++) add_edge(i, j);
  int n = k;
  len[0] = x * y;
  fill(mn, mn + n + 10, 0);
  mark[0] = true;
  long long ans = x * y;
  edge.push_back({1, 0});
  for (auto [u, ind] : adj[0])
    if (x * y > len[ind]) mn[u] = ind;
  for (int i = 1; i < n; i++) av.insert({len[mn[i]], i});
  for (int i = 1; i < n; i++) {
    auto it = av.begin();
    int v = it->second;
    ans += it->first;
    mark[v] = true;
    edge.push_back(
        {v + 1, mn[v] == 0 ? 0 : (a[mn[v]] == v ? b[mn[v]] : a[mn[v]]) + 1});
    av.erase(it);
    for (auto [u, ind] : adj[v])
      if (!mark[u] and (len[mn[u]] > len[ind])) {
        av.erase({len[mn[u]], u});
        mn[u] = ind;
        av.insert({len[ind], u});
      }
  }
  cout << ans << endl;
  for (auto u : edge) cout << u.first << ' ' << u.second << '\n';
  return 0;
}
