#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e8;
struct state {
  int mask, v;
};
int n, m, k, ans;
int a[200];
int x[7], y[7];
int dist[200][200], pre[200][200];
int visited[200], used[200];
int dp[200][200];
state f[200][200];
void prim(int s) {
  memset(visited, 0, sizeof(visited));
  visited[s] = 1;
  for (int i = 0; i < n * m; i++) {
    dist[s][i] = INF;
    int x1 = s / m, y1 = s % m, x2 = i / m, y2 = i % m;
    if (abs(x1 - x2) + abs(y1 - y2) == 1) {
      dist[s][i] = a[i];
      pre[s][i] = s;
    }
  }
  dist[s][s] = 0;
  for (int i = 1; i < n * m; i++) {
    int Min = INF, k;
    for (int j = 0; j < n * m; j++)
      if (!visited[j] && dist[s][j] < Min) {
        Min = dist[s][j];
        k = j;
      }
    visited[k] = 1;
    for (int j = 0; j < n * m; j++) {
      int x1 = k / m, y1 = k % m, x2 = j / m, y2 = j % m;
      if (abs(x1 - x2) + abs(y1 - y2) == 1 && !visited[j] &&
          dist[s][j] >= dist[s][k] + a[j]) {
        dist[s][j] = dist[s][k] + a[j];
        pre[s][j] = k;
      }
    }
  }
}
void print(int mask, int cur) {
  if (__builtin_popcount(mask) == 1) {
    int t;
    for (t = 0; t < k; t++)
      if (mask == (1 << t)) break;
    while (cur != x[t] * m + y[t]) {
      used[cur] = 1;
      cur = pre[x[t] * m + y[t]][cur];
    }
    used[x[t] * m + y[t]] = 1;
    return;
  }
  if (f[mask][cur].mask == mask) {
    int v = f[mask][cur].v;
    while (cur != v) {
      used[cur] = 1;
      cur = pre[v][cur];
    }
    print(mask, v);
    return;
  }
  print(f[mask][cur].mask, cur);
  print(mask ^ f[mask][cur].mask, cur);
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i * m + j];
  for (int i = 0; i < k; i++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  for (int i = 0; i < n * m; i++) prim(i);
  for (int i = 0; i < (1 << k); i++)
    for (int j = 0; j < n * m; j++) dp[i][j] = INF;
  for (int i = 0; i < (1 << k); i++) {
    if (__builtin_popcount(i) == 1) {
      int t;
      for (t = 0; t < k; t++)
        if (i == (1 << t)) break;
      for (int j = 0; j < n * m; j++)
        if (dp[i][j] > a[x[t] * m + y[t]] + dist[x[t] * m + y[t]][j]) {
          dp[i][j] = a[x[t] * m + y[t]] + dist[x[t] * m + y[t]][j];
          f[i][j].mask = i;
          f[i][j].v = j;
        }
      continue;
    }
    for (int j = 0; j < i; j++)
      if ((i & j) == j)
        for (int l = 0; l < n * m; l++)
          if (dp[i][l] > dp[j][l] + dp[i ^ j][l] - a[l]) {
            dp[i][l] = dp[j][l] + dp[i ^ j][l] - a[l];
            f[i][l].mask = j;
            f[i][l].v = l;
          }
    for (int j = 0; j < n * m; j++)
      for (int l = 0; l < n * m; l++)
        if (dp[i][j] > dp[i][l] + dist[l][j]) {
          dp[i][j] = dp[i][l] + dist[l][j];
          f[i][j].mask = i;
          f[i][j].v = l;
        }
  }
  ans = INF;
  for (int i = 0; i < n * m; i++) ans = min(ans, dp[(1 << k) - 1][i]);
  for (int i = 0; i < n * m; i++)
    if (ans == dp[(1 << k) - 1][i]) {
      print((1 << k) - 1, i);
      break;
    }
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (used[i * m + j])
        cout << "X";
      else
        cout << ".";
    cout << endl;
  }
  return 0;
}
