codeparrot/codecomplex huggingface dataset:
// Problem: 1497E2
// Time complexity: cubic
import java.util.*;
import java.io.*;

public class Solve{
    public static void main(String[] args) throws Exception{
        Scanner sc=new Scanner(System.in);
        PrintWriter out =new PrintWriter(System.out);
        int size=(int)1e7+1;
        int[] pr=new int[size];
        for(int i=0;i<size;i++){
            pr[i]=i;
        }
        for(int i=2;i*i<size;i++){
           if(pr[i]==i){int val=i*i;
            for(int j=val;j<=size;j+=val){
                 pr[j]=j/val;
            }
           }
        }
        int t=sc.nextInt();
        int[] dp=new int[size];
        Arrays.fill(dp,-1);
        while(t-->0){
            int n=sc.nextInt();
            int k=sc.nextInt();
            int[] ar=new int[n];
            for(int i=0;i<n;i++){
                int a=sc.nextInt();
                ar[i]=pr[a];
            }
            int[] ans=new int[k+1];
            int[] ind=new int[k+1];
            for(int i=0;i<n;i++){
                for(int h=k;h>=0;h--){
                    if(dp[ar[i]]>=ind[h]){
                        ans[h]++;
                        ind[h]=i;
                    }
                    if(h>0 && (ans[h-1]<ans[h] ||(ans[h-1]==ans[h] && ind[h-1]>ind[h])))
                    {
                        ans[h]=ans[h-1];
                        ind[h]=ind[h-1];
                    }
                }
                dp[ar[i]]=i;
            }
            out.println(ans[k]+1);
            for(int i=0;i<n;i++)dp[ar[i]]=-1;
        }
        out.close();
    }
}
// Problem: 840_C. On the Bench
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Div1_429C {

	static final long MOD = 1_000_000_007;
	static long[] fact = new long[305];
	static long[] iFact = new long[305];
	static final long I304 = 904487323;

	public static void main(String[] args) throws IOException {
		fact[0] = 1;
		for (int i = 1; i < 305; i++) {
			fact[i] = fact[i - 1] * i % MOD;
		}
		iFact[304] = I304;
		for (int i = 303; i >= 0; i--) {
			iFact[i] = iFact[i + 1] * (i + 1) % MOD;
		}

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int len = Integer.parseInt(reader.readLine());
		long[] groups = new long[len + 1];
		int[] gSizes = new int[len + 1];
		int nG = 0;

		StringTokenizer inputData = new StringTokenizer(reader.readLine());
		iLoop:
		for (int i = 0; i < len; i++) {
			long nxt = Integer.parseInt(inputData.nextToken());
			for (int j = 1; j <= nG; j++) {
				if (isSquare(nxt * groups[j])) {
					gSizes[j]++;
					continue iLoop;
				}
			}
			groups[++nG] = nxt;
			gSizes[nG] = 1;
		}

		long[][] dp = new long[nG + 1][len];

		dp[0][0] = 1;

		int fTotal = 0;
		for (int fG = 0; fG < nG; fG++) {
			for (int fB = 0; fB < len; fB++) {
				if (dp[fG][fB] == 0) {
					continue;
				}
				int nGSize = gSizes[fG + 1];
				for (int nS = 1; nS <= Math.min(nGSize, fTotal + 1); nS++) {
					for (int nBR = 0; nBR <= Math.min(fB, nS); nBR++) {
						long nW = dp[fG][fB] * fact[nGSize] % MOD * comb(nGSize - 1, nS - 1) % MOD * comb(fB, nBR) % MOD
								* comb(fTotal + 1 - fB, nS - nBR) % MOD;
						dp[fG + 1][fB - nBR + nGSize - nS] = (dp[fG + 1][fB - nBR + nGSize - nS] + nW) % MOD;
					}
				}
			}
			fTotal += gSizes[fG + 1];
		}
		printer.println(dp[nG][0]);
		printer.close();
	}

	static long comb(int a, int b) {
		if(b > a) {
			return 0;
		}
		return fact[a] * iFact[a - b] % MOD * iFact[b] % MOD;
	}

	static boolean isSquare(long inp) {
		long sqrt = (long) Math.sqrt(inp);
		return inp == sqrt * sqrt;
	}

}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.*;
import java.util.*;
import java.math.*;

public class D {
	static byte[] buf = new byte[1<<26];
    static int bp = -1;	
	
	public static void main(String[] args) throws IOException {

		/**/
		DataInputStream in = new DataInputStream(System.in);
		/*/
		DataInputStream in = new DataInputStream(new FileInputStream("src/d.in"));
		/**/
		
		in.read(buf, 0, 1<<26);
		
		int n = nni();
		int m = nni();
		int k = nni();
		
		if (k%2==1) {
			for (int i = 0; i < n; ++i) {
				StringBuilder ans = new StringBuilder();
				String sp = "";
				for (int j = 0; j < m; ++j) {
					ans.append(sp+"-1");
					sp = " ";
				}
				System.out.println(ans);
			}
			return;
		}
		
		int[][] lr = new int[n][m-1];
		int[][] ud = new int[n-1][m];
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m-1; ++j) {
				lr[i][j] = nni();
			}
		}
		for (int i = 0; i < n-1; ++i) {
			for (int j = 0; j < m; ++j) {
				ud[i][j] = nni();
			}
		}
		
		int[][][] ans = new int[k/2+1][n][m];
		
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				for (int q = 1; q <= k/2; ++q) {
					ans[q][i][j] = 123456789;
				}
			}
		}
		
		for (int uq = 0; uq < k/2; ++uq) {
			for (int ui = 0; ui < n; ++ui) {
				for (int uj = 0; uj < m; ++uj) {
					int w = ans[uq][ui][uj];
					if (ui>0 && w+ud[ui-1][uj]<ans[uq+1][ui-1][uj]) {
						ans[uq+1][ui-1][uj] = w+ud[ui-1][uj];
					}
					if (ui<n-1 && w+ud[ui][uj]<ans[uq+1][ui+1][uj]) {
						ans[uq+1][ui+1][uj] = w+ud[ui][uj];
					}
					if (uj>0 && w+lr[ui][uj-1]<ans[uq+1][ui][uj-1]) {
						ans[uq+1][ui][uj-1] = w+lr[ui][uj-1];
					}
					if (uj<m-1 && w+lr[ui][uj]<ans[uq+1][ui][uj+1]) {
						ans[uq+1][ui][uj+1] = w+lr[ui][uj];
					}
				}
			}
		}
		
		for (int i = 0; i < n; ++i) {
			StringBuilder as = new StringBuilder();
			String sp = "";
			for (int j = 0; j < m; ++j) {
				as.append(sp+ans[k/2][i][j]*2);
				sp = " ";
			}
			System.out.println(as);
		}
	}
	
	public static int nni() {
        int ret = 0;
        byte b = buf[++bp];
        while (true) {
            ret = ret*10+b-'0';
            b = buf[++bp];
            if (b<'0'||b>'9') {
            	while (buf[bp+1]=='\r'||buf[bp+1]=='\n'||buf[bp+1]==' ') {++bp;}
            	break;
            }
        }
        return ret;
    }
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.PrintWriter;
import java.util.*;

public class D {

    static Scanner sc;
    static PrintWriter out;

    public static void main(String[] args) throws Exception {

        sc = new Scanner(System.in);
        out = new PrintWriter(System.out);
        //int t = sc.nextInt();
        for(int i=0; i<1; i++) {
            solve();
        }
        out.flush();
    }

    static void solve() {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        int[][] a = new int[n][m-1];
        int[][] b = new int[n-1][m];
        for(int i=0 ;i<n; i++) {
            for(int j=0; j<m-1; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for(int i=0 ;i<n-1; i++) {
            for(int j=0; j<m; j++) {
                b[i][j] = sc.nextInt();
            }
        }

        if(k % 2 == 1) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (j > 0)
                        out.print(" ");
                    out.print("-1");
                }
                out.println();
            }
            return;
        }


        int[][] prev = new int[n][m];
        k /= 2;
        for(int l=0; l<k; l++) {
            int[][] next = new int[n][m];
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    next[i][j] = Integer.MAX_VALUE;
                    if(i>0) {
                        next[i][j] = Math.min(next[i][j], prev[i-1][j] + b[i-1][j]);
                    }
                    if(i+1<n) {
                        next[i][j] = Math.min(next[i][j], prev[i+1][j] + b[i][j]);
                    }
                    if(j>0) {
                        next[i][j] = Math.min(next[i][j], prev[i][j-1] + a[i][j-1]);
                    }
                    if(j+1<m) {
                        next[i][j] = Math.min(next[i][j], prev[i][j+1] + a[i][j]);
                    }
                }
            }
            prev = next;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (j > 0)
                    out.print(" ");
                out.print(prev[i][j] * 2);
            }
            out.println();
        }


    }



}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

public class Main {

    public static void deal(int n,int m,int k,int[][] d1,int[][] d2) {
        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;
        }
        int[][][] dp = new int[k/2+1][n][m];
        for(int i=0;i<k/2;i++) {
            for(int j=0;j<n;j++) {
                for(int l=0;l<m;l++) {
                    int min = Integer.MAX_VALUE;
                    if(j>0) min = Math.min(min,d2[j-1][l]+dp[i][j-1][l]);
                    if(j<n-1) min = Math.min(min,d2[j][l]+dp[i][j+1][l]);
                    if(l>0) min = Math.min(min,d1[j][l-1]+dp[i][j][l-1]);
                    if(l<m-1) min = Math.min(min,d1[j][l]+dp[i][j][l+1]);
                    dp[i+1][j][l] = min;
                }
            }
        }
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                System.out.print(dp[k/2][i][j]*2);
                System.out.print(" ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        MyScanner scanner = new MyScanner();
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();
        int[][] d1 = new int[n][m-1];
        int[][] d2 = new int[n-1][m];
        for(int i=0;i<n;i++) {
            for(int j=0;j<m-1;j++) {
                d1[i][j] = scanner.nextInt();
            }
        }
        for(int i=0;i<n-1;i++) {
            for(int j=0;j<m;j++) {
                d2[i][j] = scanner.nextInt();
            }
        }
        deal(n,m,k,d1,d2);
    }
    
    public 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;
        }
        
    }
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.io.*;
import java.util.*;

public class CF1517D extends PrintWriter {
	CF1517D() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1517D o = new CF1517D(); o.main(); o.flush();
	}

	static final int INF = 0x3f3f3f3f;
	void main() {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		if (k % 2 == 1) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++)
					print("-1 ");
				println();
			}
			return;
		}
		k /= 2;
		int[][] hh = new int[n][m - 1];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m - 1; j++)
				hh[i][j] = sc.nextInt();
		int[][] vv = new int[n - 1][m];
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < m; j++)
				vv[i][j] = sc.nextInt();
		int[][] dp = new int[n][m];
		int[][] dq = new int[n][m];
		while (k-- > 0) {
			for (int i = 0; i < n; i++)
				for (int j = 0; j < m; j++) {
					int x = INF;
					if (i > 0)
						x = Math.min(x, dp[i - 1][j] + vv[i - 1][j]);
					if (j > 0)
						x = Math.min(x, dp[i][j - 1] + hh[i][j - 1]);
					if (i + 1 < n)
						x = Math.min(x, dp[i + 1][j] + vv[i][j]);
					if (j + 1 < m)
						x = Math.min(x, dp[i][j + 1] + hh[i][j]);
					dq[i][j] = x;
				}
			int[][] tmp = dp; dp = dq; dq = tmp;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				print(dp[i][j] * 2 + " ");
			println();
		}
	}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.*;
import java.io.*;

public class D1517 {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		int[][] costRight = new int[n][m - 1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m - 1; j++) {
				costRight[i][j] = sc.nextInt();
			}
		}
		int[][] costDown = new int[n - 1][m];
		for (int i = 0; i < n - 1; i++) {
			for (int j = 0; j < m; j++) {
				costDown[i][j] = sc.nextInt();
			}
		}
		if (k % 2 == 1) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					pw.print(-1 + " ");
				}
				pw.println();
			}
			pw.close();
			return;
		}
		int[][][] dp = new int[k + 1][n][m];
		for (int w = 2; w <= k; w += 2) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					dp[w][i][j] = (int) 1e9;
					if (i + 1 < n)
						dp[w][i][j] = Math.min(dp[w][i][j], 2 * costDown[i][j] + dp[w - 2][i + 1][j]);
					if (i - 1 >= 0)
						dp[w][i][j] = Math.min(dp[w][i][j], 2 * costDown[i - 1][j] + dp[w - 2][i - 1][j]);
					if (j + 1 < m)
						dp[w][i][j] = Math.min(dp[w][i][j], 2 * costRight[i][j] + dp[w - 2][i][j + 1]);
					if (j - 1 >= 0)
						dp[w][i][j] = Math.min(dp[w][i][j], 2 * costRight[i][j - 1] + dp[w - 2][i][j - 1]);
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				pw.print(dp[k][i][j] + " ");
			}
			pw.println();
		}
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

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

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic

import java.util.*;

public class Main {
  public static Scanner scan = new Scanner(System.in);

  int n, m;
  int[][] x;
  int[][] y;
  int k, k2;

  int[][] sol0;
  int[][] sol1;
    
  public Main(int[][] x, int[][] y, int k) {
    this.x = x;
    this.y = y;
    this.k = k;
    this.n = x.length;
    this.m = x[0].length;
  }

  void go() {
    if(k%2 != 0) {
      for(int i=0; i<n; ++i) {
        for(int j=0; j<m; ++j) {
          System.out.print(-1 + " ");
        }
        System.out.println();
      }  
      return;
    }
    k2 = k/2;
    sol0 = new int[n][m];
    sol1 = new int[n][m];
    
    for(int d=0; d<k2; ++d) {
      var zzz = sol1;
      sol1 = sol0;
      sol0 = zzz;
      
      for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j) {
          update(i,j);
        }
    }
    for(int i=0; i<n; ++i) {
      for(int j=0; j<m; ++j) {
        System.out.print(sol1[i][j]*2 + " ");
      }
      System.out.println();
    }

  }
  
  void update(int i, int j) {
    int ret = Integer.MAX_VALUE;
    if(i>0)
      ret = Math.min(ret, sol0[i-1][j] + y[i-1][j]);
    if(j>0)
      ret = Math.min(ret, sol0[i][j-1] + x[i][j-1]);
    if(i < n-1)
      ret = Math.min(ret, sol0[i+1][j] + y[i][j]);
    if(j < m-1)
      ret = Math.min(ret, sol0[i][j+1] + x[i][j]);
    sol1[i][j] = ret;
  }
  
  
  public static void main(String[] args) {
    int n = scan.nextInt();
    int m = scan.nextInt();
    int k = scan.nextInt();
    int x[][] = new int[n][m];
    int y[][] = new int[n][m];
    for(int i=0; i<n; ++i) {
      for(int j=0; j<m-1; ++j) {
        x[i][j] = scan.nextInt();
      }
    }
    for(int i=0; i<n-1; ++i) {
      for(int j=0; j<m; ++j) {
        y[i][j] = scan.nextInt();
      }
    }
    Main mm = new Main(x,y,k);
    mm.go();
  }

}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
//created by Whiplash99
import java.io.*;
import java.util.*;
public class D
{
    private static ArrayDeque<Integer>[][] edge;
    private static long[][] dp[],w1,w2;
    private static int N,M,K;

    private static void answer()
    {
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<N;i++)
        {
            for (int j = 0; j < M; j++) sb.append(dp[i][j][K]).append(" ");
            sb.append("\n");
        }
        System.out.println(sb);
    }
    private static long solve(int i, int j, int pos)
    {
        if(pos==0) return 0;
        if(dp[i][j][pos]!=-1) return dp[i][j][pos];

        long a=Long.MAX_VALUE/100;
        if(i-1>=0) a=Math.min(a,solve(i-1,j,pos-1)+w2[i-1][j]);
        if(i<N-1) a=Math.min(a,solve(i+1,j,pos-1)+w2[i][j]);
        if(j-1>=0) a=Math.min(a,solve(i,j-1,pos-1)+w1[i][j-1]);
        if(j<M-1) a=Math.min(a,solve(i,j+1,pos-1)+w1[i][j]);

        return dp[i][j][pos]=a;
    }
    public static void main(String[] args) throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i;

        String[] s=br.readLine().trim().split(" ");
        N=Integer.parseInt(s[0]);
        M=Integer.parseInt(s[1]);
        K=Integer.parseInt(s[2]);

        edge=new ArrayDeque[N][M];
        for(i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
                edge[i][j]=new ArrayDeque<>();
        }

        w1=new long[N][M-1];
        w2=new long[N-1][M];
        dp=new long[N][M][K/2+1];

        for(i=0;i<N;i++)
        {
            s=br.readLine().trim().split(" ");
            for(int j=0;j<M-1;j++) w1[i][j]=Integer.parseInt(s[j])*2L;
        }
        for(i=0;i<N-1;i++)
        {
            s=br.readLine().trim().split(" ");
            for(int j=0;j<M;j++) w2[i][j]=Integer.parseInt(s[j])*2L;
        }

        for(i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
                Arrays.fill(dp[i][j],-1);
        }

        if(K%2==1)
        {
            K/=2;
            answer();
            System.exit(0);
        }

        K/=2;
        for(i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
                solve(i,j,K);
        }

        answer();
    }
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
import java.util.*;
import java.io.*;
public class B{
	static int[][] hor;
	static int[][] ver;
	static int n;
	static int m;
	static int k;
	static long[][][] dp;
	
	static long dist(int row, int col)
	{
		if(k%2==1)return Integer.MAX_VALUE;
		return 2*make(row,col,k/2);
	}
	
	static long make(int row, int col, int moves)
	{
		if(moves == 0)
		{
			return 0;
		}
		
		if(dp[row][col][moves]!=-1)return dp[row][col][moves];
		
		long ans = Long.MAX_VALUE;
		
		if(col-1>=0)
		{
			ans = Math.min(ans, hor[row][col-1]+make(row,col-1,moves-1));
		}
		if(col+1<m)
		{
			ans = Math.min(ans, hor[row][col]+make(row,col+1,moves-1));
		}
		if(row-1>=0)
		{
			ans = Math.min(ans, ver[row-1][col]+make(row-1,col,moves-1));
		}
		if(row+1<n)
		{
			ans = Math.min(ans, ver[row][col]+make(row+1,col,moves-1));
		}
		dp[row][col][moves] = ans;
		return ans;
	}
	
	public static void main(String[] args)
	{
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		n = fs.nextInt(); m = fs.nextInt(); k = fs.nextInt();
		hor = new int[n][m]; ver = new int[n][m];
		dp = new long[505][505][24];
		for(int i=0;i<505;i++)for(int j=0;j<505;j++)for(int k=0;k<24;k++)dp[i][j][k] = -1;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m-1;j++)
			{
				int a = fs.nextInt();
				hor[i][j] = a;
			}
		}
		
		for(int row=0;row<n-1;row++)
		{
			for(int col =0;col<m;col++)
			{
				int a = fs.nextInt();
				ver[row][col] = a;
			}
		}

		
		for(int row=0;row<n;row++)
		{
			for(int col=0;col<m;col++)
			{
				long d = dist(row,col);
				if(d<Integer.MAX_VALUE)
				{
					out.print(d+" ");
				}
				else out.print("-1 ");
			}
			out.println();
		}
		out.close();
	}
	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());
		}
	}
	public static int[] sort(int[] arr)
	{
		List<Integer> temp = new ArrayList();
		for(int i:arr)temp.add(i);
		Collections.sort(temp);
		int start = 0;
		for(int i:temp)arr[start++]=i;
		return arr;
	}
}
// Problem: 1517_D. Explorer Space
// Time complexity: cubic
// package com.company.codeforces;




import java.util.*;

public class  Solution {
   static int n,m,h[][],v[][];
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

            n=input.nextInt();
            m=input.nextInt();
           int k=input.nextInt();
           h=new int[n][m-1];
            for (int i = 0; i <n ; i++) {
                for (int j = 0; j <m-1 ; j++) {
                    h[i][j]=input.nextInt();
                }
            }
            v=new int[n][m];
            for (int i = 0; i <n-1 ; i++) {
                for (int j = 0; j <m ; j++) {
                    v[i][j]=input.nextInt();
                }
            }
            int ans[][]=new int[n][m];
           dp=new int[501][501][11];
        for (int aa[]:ans
             ) { Arrays.fill(aa,-1);

        }
        if (k%2==0) {

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ans[i][j] = dfs(i, j, k / 2) * 2;
                }

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

        }

    }
    static int dp[][][];

    private static int dfs(int i, int j, int k) {
        if (k==0) return 0;
        if (dp[i][j][k]!=0){
//            System.out.println("hit");
            return dp[i][j][k];
        }
        //left
        int ans=Integer.MAX_VALUE;
        if (j-1>=0)
         ans=dfs(i, j-1, k-1)+h[i][j-1];
        if (i<n-1)
        ans=Math.min(ans,dfs(i+1, j, k-1)+v[i][j]);
        if (i>0)
        ans=Math.min(ans,dfs(i-1, j, k-1)+v[i-1][j]);
        if (j<m-1)
        ans=Math.min(ans,dfs(i, j+1, k-1)+h[i][j]);

        return dp[i][j][k]= ans;

    }


}