#include <bits/stdc++.h>
using namespace std;
int X, Y, K;
int x[10], y[10];
int a[110][110];
int dp[110][110][(1 << 7)], path[110][110][(1 << 7)][3];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
string ans[110];
void shortest(int mask) {
  int iter, i, j, k;
  for ((iter) = 0; (iter) < (int)(X * Y + 10); (iter)++) {
    for ((i) = 0; (i) < (int)(X); (i)++)
      for ((j) = 0; (j) < (int)(Y); (j)++)
        for ((k) = 0; (k) < (int)(4); (k)++) {
          int x2 = i + dx[k], y2 = j + dy[k];
          if (x2 >= 0 && x2 < X && y2 >= 0 && y2 < Y) {
            int tmp = dp[i][j][mask] + a[x2][y2];
            if (tmp < dp[x2][y2][mask]) {
              dp[x2][y2][mask] = tmp;
              path[x2][y2][mask][0] = 0;
              path[x2][y2][mask][1] = i;
              path[x2][y2][mask][2] = j;
            }
          }
        }
  }
}
void print(int x2, int y2, int mask) {
  ans[x2][y2] = 'X';
  if (path[x2][y2][mask][0] == 0)
    print(path[x2][y2][mask][1], path[x2][y2][mask][2], mask);
  if (path[x2][y2][mask][0] == 1) {
    print(x2, y2, path[x2][y2][mask][1]);
    print(x2, y2, path[x2][y2][mask][2]);
  }
}
int main(void) {
  int i, j, k, mask;
  cin >> X >> Y >> K;
  for ((i) = 0; (i) < (int)(X); (i)++)
    for ((j) = 0; (j) < (int)(Y); (j)++) cin >> a[i][j];
  for ((i) = 0; (i) < (int)(K); (i)++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  for ((i) = 0; (i) < (int)(X); (i)++)
    for ((j) = 0; (j) < (int)(Y); (j)++)
      for ((mask) = 0; (mask) < (int)((1 << K)); (mask)++)
        dp[i][j][mask] = (1 << 29);
  for ((i) = 0; (i) < (int)(K); (i)++) {
    dp[x[i]][y[i]][(1 << i)] = a[x[i]][y[i]];
    path[x[i]][y[i]][(1 << i)][0] = 2;
  }
  for ((mask) = 0; (mask) < (int)((1 << K)); (mask)++) {
    for ((i) = 0; (i) < (int)(X); (i)++)
      for ((j) = 0; (j) < (int)(Y); (j)++) {
        int mask2;
        for (mask2 = mask; mask2 > 0; mask2 = (mask & (mask2 - 1))) {
          int tmp = dp[i][j][mask2] + dp[i][j][mask ^ mask2] - 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] = mask2;
            path[i][j][mask][2] = (mask ^ mask2);
          }
        }
      }
    shortest(mask);
  }
  int best = (1 << 29);
  for ((i) = 0; (i) < (int)(X); (i)++)
    for ((j) = 0; (j) < (int)(Y); (j)++)
      best = min(best, dp[i][j][(1 << K) - 1]);
  cout << best << endl;
  for ((i) = 0; (i) < (int)(X); (i)++)
    for ((j) = 0; (j) < (int)(Y); (j)++) ans[i] += '.';
  for ((i) = 0; (i) < (int)(X); (i)++)
    for ((j) = 0; (j) < (int)(Y); (j)++)
      if (dp[i][j][(1 << K) - 1] == best) {
        print(i, j, (1 << K) - 1);
        for ((k) = 0; (k) < (int)(X); (k)++) cout << ans[k] << endl;
        return 0;
      }
  return 0;
}
