#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
  printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
  printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
  printsp(x1), printsp(x2), printsp(x3), println(x4);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int a[211][211];
int dp[211][211][211];
int dst[211][211];
int id[211][211];
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
char res[211][211];
int vst[1011];
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      gn(a[i][j]);
      vst[a[i][j]]++;
      a[i][j] = a[i][j] * 10000 + vst[a[i][j]];
    }
  int nn = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) id[i][j] = ++nn;
  memset(dst, 0x3f3f3f3f, sizeof dst);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        int ii = i + di[k], jj = j + dj[k];
        if (ii < 1 or jj < 1 or ii > n or jj > m) continue;
        dst[id[i][j]][id[ii][jj]] = a[ii][jj];
        dst[id[ii][jj]][id[i][j]] = a[i][j];
      }
    }
  for (int k = 1; k <= nn; k++) {
    for (int i = 1; i <= nn; i++) {
      for (int j = 1; j <= nn; j++) {
        smin(dst[i][j], dst[i][k] + dst[k][j]);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int ii = 1; ii <= n; ii++)
        for (int jj = 1; jj <= m; jj++) {
          dst[id[i][j]][id[ii][jj]] += a[i][j];
        }
    }
  memset(dp, 0x3f3f3f3f, sizeof dp);
  for (int i = 0; i < k; i++) {
    int first, second;
    gn(first, second);
    dp[1 << i][first][second] = a[first][second];
  }
  for (int i = 1; i < 1 << k; i++) {
    for (int p = 1; p <= n; p++) {
      for (int q = 1; q <= m; q++) {
        int b = 0;
        while (b = (b - i) & i) {
          if (b < i) smin(dp[i][p][q], dp[b][p][q] + dp[i ^ b][p][q] - a[p][q]);
        }
      }
    }
    for (int r = 1; r <= n; r++) {
      for (int w = 1; w <= m; w++) {
        for (int p = 1; p <= n; p++) {
          for (int q = 1; q <= m; q++) {
            smin(dp[i][p][q], dp[i][r][w] + dst[id[p][q]][id[r][w]] - a[r][w]);
          }
        }
      }
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) smin(ans, dp[(1 << k) - 1][i][j]);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) res[i][j] = '.';
  cout << ans / 10000 << endl;
  int tmp = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dp[(1 << k) - 1][i][j] == ans)
        res[i][j] = 'X', tmp += a[i][j] / 10000;
    }
  }
  if (ans / 10000 != tmp)
    while (1)
      ;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) printf("%c", res[i][j]);
    puts("");
  }
  return 0;
}
