#include <bits/stdc++.h>
using namespace std;
int n, m;
bool debug = false;
int k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int a[105][105];
char out[105][105];
int dp[105][105][1 << 7], path[105][105][1 << 7][3];
vector<pair<int, int>> vk;
void dd(int mask) {
  for (int p = 0; p < m * n + 10; p++) {
    for (int(i) = 0; (i) < (int)(n); (i)++)
      for (int(j) = 0; (j) < (int)(m); (j)++)
        for (int(q) = 0; (q) < (int)(4); (q)++) {
          int x = dx[q] + i, y = dy[q] + j;
          if (x >= 0 && y >= 0 && x < n && y < m) {
            int tmp = dp[i][j][mask] + a[x][y];
            if (tmp < dp[x][y][mask]) {
              dp[x][y][mask] = tmp;
              path[x][y][mask][0] = 2;
              path[x][y][mask][1] = i;
              path[x][y][mask][2] = j;
            }
          }
        }
  }
}
void pp(int i, int j, int mask) {
  out[i][j] = 'X';
  if (path[i][j][mask][0] == 1) {
    pp(i, j, path[i][j][mask][1]);
    pp(i, j, path[i][j][mask][2]);
  } else if (path[i][j][mask][0] == 2) {
    pp(path[i][j][mask][1], path[i][j][mask][2], mask);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int(i) = 0; (i) < (int)(n); (i)++)
    for (int(j) = 0; (j) < (int)(m); (j)++) scanf("%d", &a[i][j]);
  for (int i = 0, x, y; i < k; i++) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    vk.push_back({x, y});
  }
  for (int p = 0; p < (1 << k); p++) {
    for (int(i) = 0; (i) < (int)(n); (i)++)
      for (int(j) = 0; (j) < (int)(m); (j)++) {
        dp[i][j][p] = 1 << 30;
      }
  }
  for (int p = 0; p < k; p++) {
    dp[vk[p].first][vk[p].second][1 << p] = a[vk[p].first][vk[p].second];
  }
  for (int mask = 0; mask < (1 << k); mask++) {
    for (int(i) = 0; (i) < (int)(n); (i)++)
      for (int(j) = 0; (j) < (int)(m); (j)++) {
        for (int subMask = mask; subMask; subMask = mask & (subMask - 1)) {
          int tmp = dp[i][j][subMask] + dp[i][j][mask ^ subMask] - a[i][j];
          if (tmp < dp[i][j][mask]) {
            dp[i][j][mask] = tmp;
            path[i][j][mask][0] = 1;
            path[i][j][mask][1] = subMask;
            path[i][j][mask][2] = mask ^ subMask;
          }
        }
      }
    dd(mask);
  }
  int ans = 1 << 30;
  for (int(i) = 0; (i) < (int)(n); (i)++)
    for (int(j) = 0; (j) < (int)(m); (j)++) {
      ans = min(ans, dp[i][j][(1 << k) - 1]);
      out[i][j] = '.';
    }
  printf("%d\n", ans);
  for (int(i) = 0; (i) < (int)(n); (i)++)
    for (int(j) = 0; (j) < (int)(m); (j)++)
      if (ans == dp[i][j][(1 << k) - 1]) {
        pp(i, j, (1 << k) - 1);
        for (int(i) = 0; (i) < (int)(n); (i)++) puts(out[i]);
        return 0;
      }
  return 0;
}
