codeparrot/codecomplex huggingface dataset:
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.*;
import java.io.*;
public class D {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		int[][] lr = new int[n][m-1];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m-1; j++){
				lr[i][j] = sc.nextInt();
			}
		}
		int[][] ud = new int[n-1][m];
		for(int i = 0; i < n-1; i++){
			for(int j = 0; j < m; j++){
				ud[i][j] = sc.nextInt();
			}
		}
		if(k % 2 == 1) {
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < n; i++){
				for(int j = 0; j < m; j++){
					sb.append(-1+" ");
				}
				sb.replace(sb.length()-1, sb.length(), "\n");
			}
			PrintWriter pw = new PrintWriter(System.out);
			pw.println(sb.toString().trim());
			pw.flush();
		}
		else {
			int[][] dir = {{0,1},{0,-1},{1,0},{-1,0}};
			long[][][] dp = new long[k/2+1][n][m];
			for(int s = 1; s <= k/2; s++) {
				for(int i = 0; i < n; i++){
					for(int j = 0; j < m; j++){
						dp[s][i][j] = Long.MAX_VALUE;
						for(int[] d: dir) {
							int u = i + d[0], v = j + d[1];
							if(u >= 0 && u < n && v >= 0 && v < m) {
								long w = calc(i, j, u, v, lr, ud);
								dp[s][i][j] = Math.min(dp[s][i][j], dp[s-1][u][v] + 2*w);
							}
						}
					}
				}
			}
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < n; i++){
				for(int j = 0; j < m; j++){
					sb.append(dp[k/2][i][j]+" ");
				}
				sb.replace(sb.length()-1, sb.length(), "\n");
			}
			PrintWriter pw = new PrintWriter(System.out);
			pw.println(sb.toString().trim());
			pw.flush();
		}
	}
	static long calc(int i, int j, int u, int v, int[][] lr, int[][] ud) {
		if(i == u) {
			return lr[i][Math.min(j, v)];
		}
		else {
			return ud[Math.min(i, u)][j];
		}
	}
	
	
	static class Pair implements Comparable<Pair>{
		int x, y;
		public Pair(int x, int y) {
			this.x = x; this.y = y;
		}
		@Override
		public int compareTo(Pair p) {
			if(x == p.x) return y - p.y;
			else return x - p.x;
		}
		public String toString() {
			return x+" "+y;
		}
	}
	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.io.*;
 
public class ExplorerSpace {
	
	private static class MyScanner {
	    BufferedReader br;
	    StringTokenizer st;

	    public MyScanner() {
	       br = new BufferedReader(new InputStreamReader(System.in));
	    }

	    String next() {
	        while (st == null || !st.hasMoreElements()) {
	            try {
	                st = new StringTokenizer(br.readLine());
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return st.nextToken();
	    }

	    int nextInt() {
	        return Integer.parseInt(next());
	    }

	    long nextLong() {
	        return Long.parseLong(next());
	    }

	    double nextDouble() {
	        return Double.parseDouble(next());
	    }

	    String nextLine(){
	        String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  return str;
	    }

	 }
	
    
	public static int[][][] dp; 
	
	public static boolean valid(int i, int j, int n, int m)
	{
		return i>=0 && i<n &&j>=0 && j<m;
	}
	
	public static void solution(int n, int m, int k, int[][] h, int[][] v)
			
	{
		if(k%2==1)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				   out.print(-1+" ");
				
				out.println();
				
			}
			
			return;
		}
		
		dp = new int[n][m][k/2+1];
		
		for(int t = 1; t<=k/2; t++)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				{
					dp[i][j][t] = Integer.MAX_VALUE;
						
				}
			}
		}
		
		for(int i = 0; i<n; i++)
		{
			for(int j = 0; j<m; j++)
			{
				dp[i][j][0] = 0;
					
			}
		}
		
		
		for(int t = 1; t<=k/2; t++)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				{
					if(valid(i,j+1,n,m))
						dp[i][j][t] = Math.min(dp[i][j][t], h[i][j] + dp[i][j+1][t-1]); 
				
					if(valid(i,j-1,n,m))
						 dp[i][j][t] = Math.min(dp[i][j][t], h[i][j-1] + dp[i][j-1][t-1]);
				
					if(valid(i+1,j,n,m))
						 dp[i][j][t] = Math.min(dp[i][j][t], v[i][j] + dp[i+1][j][t-1]); 
			
					if(valid(i-1,j,n,m))
						dp[i][j][t] = Math.min(dp[i][j][t], v[i-1][j] + dp[i-1][j][t-1]); 
		
				}
			}
		}
		
		
		for(int i = 0; i<n; i++)
		{
			for(int j = 0; j<m; j++)
				out.print((dp[i][j][k/2]*2)+" ");
			
			out.println();
		}
		
	}
        
private static PrintWriter out = new PrintWriter(System.out);

public static void main (String[] args)
{
	MyScanner s =  new MyScanner();
     
    int n = s.nextInt();
    int m = s.nextInt();
    int k = s.nextInt();
    
    int[][] h = new int[n][m-1];
    
    for(int i = 0; i<n; i++)
    {
    	for(int j = 0; j<m-1; j++)
    	{
    		h[i][j] = s.nextInt();
    	}
    }
    
    int[][] v = new int[n-1][m];
    
    for(int i = 0; i<n-1; i++)
    {
    	for(int j = 0; j<m; j++)
    	{
    		v[i][j] = s.nextInt();
    	}
    }
    
    
   solution(n,m,k,h,v);
    
    out.flush();
    out.close();
    
}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.*;
import java.util.*;

public class Codeforces
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]),k=Integer.parseInt(s[2]);
        int i,j,max=n*m,in[][]=new int[n][m],x=0;
        if(k%2==1)  //not possible
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++) sb.append("-1 ");
                sb.append("\n");
            }
            System.out.print(sb);
            return;
        }

        for(i=0;i<n;i++)
        for(j=0;j<m;j++) in[i][j]=x++;

        ArrayList<Edge> g[]=new ArrayList[max];
        for(i=0;i<max;i++) g[i]=new ArrayList<>();
        for(i=0;i<n;i++)
        {
            s=bu.readLine().split(" ");
            for(j=0;j<m-1;j++)
            {
                int u=in[i][j],v=in[i][j+1],w=Integer.parseInt(s[j]);
                g[u].add(new Edge(v,w));
                g[v].add(new Edge(u,w));
            }
        }
        for(i=0;i<n-1;i++)
        {
            s=bu.readLine().split(" ");
            for(j=0;j<m;j++)
            {
                int u=in[i][j],v=in[i+1][j],w=Integer.parseInt(s[j]);
                g[u].add(new Edge(v,w));
                g[v].add(new Edge(u,w));
            }
        }

        k/=2;
        int dp[][]=new int[k][max];
        for(i=0;i<max;i++)
        {
            dp[0][i]=Integer.MAX_VALUE;
            for(Edge e:g[i])
            dp[0][i]=Math.min(dp[0][i],2*e.w);
        }

        for(i=1;i<k;i++)
        for(j=0;j<max;j++)
        {
            dp[i][j]=Integer.MAX_VALUE;
            for(Edge e:g[j])
            dp[i][j]=Math.min(dp[i][j],dp[i-1][e.v]+2*e.w);
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            sb.append(dp[k-1][in[i][j]]+" ");
            sb.append("\n");
        }
        System.out.print(sb);
    }

    static class Edge
    {
        int v,w,d;
        Edge(int a,int b)
        {
            v=a;
            w=b;
            d=0;
        }
    }
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

/*
height == 2, width == 3

2 3 2
1 1
1 1
1 1 1
 */
public class D {

	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int h=fs.nextInt(), w=fs.nextInt(), k=fs.nextInt();
		PrintWriter out=new PrintWriter(System.out);
		if (k%2==1) {
			for (int y=0; y<h; y++) {
				for (int x=0; x<w; x++) {
					if (x!=0) out.print(" ");
					out.print(-1);
				}
				out.println();
			}
			out.close();
			return;
		}
		k/=2;
		
		int[][] rightCost=new int[w-1][h];
		int[][] downCost=new int[w][h-1];
		for (int y=0; y<h; y++) 
			for (int x=0; x<w-1; x++)
				rightCost[x][y]=fs.nextInt();
		for (int y=0; y<h-1; y++)
			for (int x=0; x<w; x++)
				 downCost[x][y]=fs.nextInt();
		
		long[][] dp=new long[w][h];
		long[][] dpNext=new long[w][h];
		for (int i=0; i<k; i++) {
			for (int x=0; x<w; x++) {
				for (int y=0; y<h; y++) {
					long ans=(long)1e18;
					if (x!=0) ans=Math.min(ans, dp[x-1][y]+rightCost[x-1][y]);
					if (y!=0) ans=Math.min(ans, dp[x][y-1]+downCost[x][y-1]);
					if (x!=w-1) ans=Math.min(ans, dp[x+1][y]+rightCost[x][y]);
					if (y!=h-1) ans=Math.min(ans, dp[x][y+1]+downCost[x][y]);
					dpNext[x][y]=ans;
				}
			}
			dp=dpNext;
			dpNext=new long[w][h];
		}
		//TODO: double answer
		for (int y=0; y<h; y++) {
			for (int x=0; x<w; x++) {
				if (x!=0) out.print(" ");
				out.print(2*dp[x][y]);
			}
			out.println();
		}
		out.close();
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		//idea 1 : K/2번까지만 실행해서, 그 최솟값 *2를 하면 된다
		//bfs로 i,j칸에 크거나 같은 값을 가지고 진입하면, 즉시 cut
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		n = pint(st.nextToken());
		m = pint(st.nextToken());
		k = pint(st.nextToken());
		
		//up down left right
		//0 is wall
		map = new int[n][m][4];
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for (int j = 0; j < m-1; j++) {
				int temp = pint(st.nextToken());
				map[i][j][3]=temp;
				map[i][j+1][2]=temp;
			}
		}
		for (int i = 0; i < n-1; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for (int j = 0; j < m; j++) {
				int temp = pint(st.nextToken());
				map[i][j][1]=temp;
				map[i+1][j][0]=temp;
			}
		}
		
		ans = new int[n][m][k/2+1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if(k%2==1) {
					sb.append("-1 ");
					continue;
				}
				
				int min=rec(i,j,0,k/2);
				
				sb.append(min*2).append(" ");
				
			}sb.append("\n");
		}
		
		System.out.println(sb);
		
	}
	static int n,m,k;
	//up down left right
	static int[]dx = {-1,1, 0, 0};
	static int[]dy = {0, 0, -1,1};
	static int[][][]map;
	static int[][][]ans;
	
	static int rec(int x, int y, int cost, int cnt) {
		if(cnt==0) {
			return 0;
		}
		if(ans[x][y][cnt]!=0)return ans[x][y][cnt];
		
		int temp=Integer.MAX_VALUE;
		for (int i = 0; i < 4; i++) {
			int tx=x+dx[i], ty=y+dy[i];
			
			if(tx<0||tx>=n||ty<0||ty>=m)continue;
			
			if(map[x][y][i]!=0) {
				temp=Math.min(temp, rec(tx, ty, cost, cnt-1)+map[x][y][i]);
			}
		}
		ans[x][y][cnt]=temp;
		
		return ans[x][y][cnt];
	}
	
	static int pint(String s) {
		return Integer.parseInt(s);
	}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Practice {
	public static long mod = (long) Math.pow(10, 9) + 7;
	public static long[][][]dp;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		
		// int t = Integer.parseInt(br.readLine());
//		while (t-- > 0) {
		String[] s2 = br.readLine().split(" ");
		int n = Integer.parseInt(s2[0]);
		int m = Integer.parseInt(s2[1]);
		int k = Integer.parseInt(s2[2]);
		dp=new long[n][m][k+1];
		int[][] hori = new int[n][m - 1];
		int[][] verti = new int[n - 1][m];
		for (int i = 0; i < n; i++) {
			String str = (br.readLine());
			String[] s1 = str.split(" ");
			for (int j = 0; j < m - 1; j++) {
				hori[i][j] = Integer.parseInt(s1[j]);
			}
		}
		for (int i = 0; i < n - 1; i++) {
			String str = (br.readLine());
			String[] s1 = str.split(" ");
			for (int j = 0; j < m; j++) {
				verti[i][j] = Integer.parseInt(s1[j]);
			}
		}
		long[][]ans=new long[n][m];
		if(k%2!=0) {
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					ans[i][j]=-1;
				}
			}
		}else {
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					ans[i][j]=findAns(i,j,k,hori,verti,n,m,Integer.MAX_VALUE);
				}
			}
		}
		for(int i=0;i<n;i++) {
			StringBuilder str=new StringBuilder();
			for(int j=0;j<m;j++) {
				str.append(ans[i][j]+" ");
			}pw.println(str.toString());
		}
		// }

		pw.close();
	}

	private static long findAns(int i, int j, int k, int[][] hori, int[][] verti, int n, int m, int last) {
		// TODO Auto-generated method stub
		if(k==0) {
			return 0;
		}
		if(i<n&&j<m&&i>=0&&j>=0) {
			
		}else {
			return 100000000;
		}
	//	System.out.println(i+" "+j+" "+k);
		if(dp[i][j][k]!=0) {
			return dp[i][j][k];
		}
		long ans=k*((long)last);
		if(j>0) {
			long curr=2*hori[i][j-1];
			curr+=findAns(i, j-1, k-2, hori, verti, n, m, hori[i][j-1]);
			ans=Math.min(ans, curr);
		}
	//	System.out.println(ans+" 0");
		if(j<m-1) {
			long curr=2*hori[i][j];
			curr+=findAns(i, j+1, k-2, hori, verti, n, m, hori[i][j]);
			ans=Math.min(ans, curr);
		}
	//	System.out.println(ans+" 1");
		if(i>0) {
			long curr=2*verti[i-1][j];
			curr+=findAns(i-1, j, k-2, hori, verti, n, m, verti[i-1][j]);
			ans=Math.min(ans, curr);
		}
	//	System.out.println(ans+" 2");
		if(i<n-1) {
			long curr=2*verti[i][j];
			curr+=findAns(i+1, j, k-2, hori, verti, n, m, verti[i][j]);
			ans=Math.min(ans, curr);
		}
	//	System.out.println(ans+" 3");
		dp[i][j][k]=ans;
		return ans;
	}
}

//private static long getGCD(long l, long m) {
//// TODO Auto-generated method stub
//
//long t1 = Math.min(l, m);
//long t2 = Math.max(l, m);
//while (true) {
//	long temp = t2 % t1;
//	if (temp == 0) {
//		return t1;
//	}
//	t2 = t1;
//	t1 = temp;
//}
//}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.*;
import java.io.*;

public class _1517_D {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer line = new StringTokenizer(in.readLine());
        int n = Integer.parseInt(line.nextToken());
        int m = Integer.parseInt(line.nextToken());
        int k = Integer.parseInt(line.nextToken());
        int[][] edges1 = new int[n][m - 1];
        int[][] edges2 = new int[n - 1][m];
        for(int i = 0; i < n; i++) {
            line = new StringTokenizer(in.readLine());
            for(int j = 0; j < m - 1; j++) {
                edges1[i][j] = Integer.parseInt(line.nextToken());
            }
        }
        for(int i = 0; i < n - 1; i++) {
            line = new StringTokenizer(in.readLine());
            for(int j = 0; j < m; j++) {
                edges2[i][j] = Integer.parseInt(line.nextToken());
            }
        }
        if(k % 2 == 1) {
            for(int i = 0; i < n; i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < m; j++) {
                    sb.append(-1);
                    if(j < m - 1) sb.append(' ');
                }
                out.println(sb.toString());
            }
        }else {
            int[][][] dp = new int[n][m][k + 1];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    Arrays.fill(dp[i][j], Integer.MAX_VALUE);
                    dp[i][j][0] = 0;
                }
            }
            for(int a = 2; a <= k; a += 2) {
                for(int i = 0; i < n; i++) {
                    for(int j = 0; j < m; j++) {
                        if(i > 0) {
                            dp[i][j][a] = Math.min(dp[i][j][a], dp[i - 1][j][a - 2] + 2 * edges2[i - 1][j]);
                        }
                        if(i < n - 1) {
                            dp[i][j][a] = Math.min(dp[i][j][a], dp[i + 1][j][a - 2] + 2 * edges2[i][j]);
                        }
                        if(j > 0) {
                            dp[i][j][a] = Math.min(dp[i][j][a], dp[i][j - 1][a - 2] + 2 * edges1[i][j - 1]);
                        }
                        if(j < m - 1) {
                            dp[i][j][a] = Math.min(dp[i][j][a], dp[i][j + 1][a - 2] + 2 * edges1[i][j]);
                        }
                    }
                }
            }
            for(int i = 0; i < n; i++) {
                StringBuilder sb = new StringBuilder();
                for(int j = 0; j < m; j++) {
                    sb.append(dp[i][j][k]);
                    if(j < m - 1) sb.append(' ');
                }
                out.println(sb.toString());
            }
        }
        in.close();
        out.close();
    }
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.io.*;
 
public class ExplorerSpace {
	
	private static class MyScanner {
	    BufferedReader br;
	    StringTokenizer st;

	    public MyScanner() {
	       br = new BufferedReader(new InputStreamReader(System.in));
	    }

	    String next() {
	        while (st == null || !st.hasMoreElements()) {
	            try {
	                st = new StringTokenizer(br.readLine());
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return st.nextToken();
	    }

	    int nextInt() {
	        return Integer.parseInt(next());
	    }

	    long nextLong() {
	        return Long.parseLong(next());
	    }

	    double nextDouble() {
	        return Double.parseDouble(next());
	    }

	    String nextLine(){
	        String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  return str;
	    }

	 }
	
    
	public static long[][][] dp; 
	
	public static boolean valid(int i, int j, int n, int m)
	{
		return i>=0 && i<n &&j>=0 && j<m;
	}
	
	public static void solution(int n, int m, int k, int[][] h, int[][] v)
			
	{
		if(k%2==1)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				   out.print(-1+" ");
				
				out.println();
				
			}
			
			return;
		}
		
		dp = new long[n][m][k/2+1];
		
		for(int t = 1; t<=k/2; t++)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				{
					dp[i][j][t] = Long.MAX_VALUE;
						
				}
			}
		}
		
		for(int i = 0; i<n; i++)
		{
			for(int j = 0; j<m; j++)
			{
				dp[i][j][0] = 0;
					
			}
		}
		
		
		for(int t = 1; t<=k/2; t++)
		{
			for(int i = 0; i<n; i++)
			{
				for(int j = 0; j<m; j++)
				{
					if(valid(i,j+1,n,m))
						dp[i][j][t] = Math.min(dp[i][j][t], h[i][j] + dp[i][j+1][t-1]); 
				
					if(valid(i,j-1,n,m))
						 dp[i][j][t] = Math.min(dp[i][j][t], h[i][j-1] + dp[i][j-1][t-1]);
				
					if(valid(i+1,j,n,m))
						 dp[i][j][t] = Math.min(dp[i][j][t], v[i][j] + dp[i+1][j][t-1]); 
			
					if(valid(i-1,j,n,m))
						dp[i][j][t] = Math.min(dp[i][j][t], v[i-1][j] + dp[i-1][j][t-1]); 
		
				}
			}
		}
		
		
		for(int i = 0; i<n; i++)
		{
			for(int j = 0; j<m; j++)
				out.print((dp[i][j][k/2]*2)+" ");
			
			out.println();
		}
		
	}
        
private static PrintWriter out = new PrintWriter(System.out);

public static void main (String[] args)
{
	MyScanner s =  new MyScanner();
     
    int n = s.nextInt();
    int m = s.nextInt();
    int k = s.nextInt();
    
    int[][] h = new int[n][m-1];
    
    for(int i = 0; i<n; i++)
    {
    	for(int j = 0; j<m-1; j++)
    	{
    		h[i][j] = s.nextInt();
    	}
    }
    
    int[][] v = new int[n-1][m];
    
    for(int i = 0; i<n-1; i++)
    {
    	for(int j = 0; j<m; j++)
    	{
    		v[i][j] = s.nextInt();
    	}
    }
    
    
   solution(n,m,k,h,v);
    
    out.flush();
    out.close();
    
}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

public class Main {
    static public void main(String args[]) throws Exception {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt(), m = s.nextInt(), K = s.nextInt();
        int[][] p = new int[n][m - 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m - 1; j++) {
                p[i][j] = s.nextInt();
            }
        }
        int[][] v = new int[n - 1][m];
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < m; j++) {
                v[i][j] = s.nextInt();
            }
        }
        if (K % 2 == 1){
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    System.out.print("-1 ");
                }
                System.out.println();
            }

            return;
        }

        long[][][] dp = new long[K + 1][n][m];
        for (int k = 2; k <= K; k += 2) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    long res = Long.MAX_VALUE;
                    if (i + 1 < n) {
                        res = Math.min(res, dp[k - 2][i + 1][j] + v[i][j] * 2);
                    }
                    if (i - 1 >= 0) {
                        res = Math.min(res, dp[k - 2][i - 1][j] + v[i - 1][j] * 2);
                    }
                    if (j + 1 < m) {
                        res = Math.min(res, dp[k - 2][i][j + 1] + p[i][j] * 2);
                    }

                    if (j - 1 >= 0) {
                        res = Math.min(res, dp[k - 2][i][j - 1] + p[i][j - 1] * 2);
                    }
                    dp[k][i][j] = res;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(dp[K][i][j] + " ");
            }
            System.out.println();
        }

    }
}