{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003eNumber Link is a famous game available in platforms including iOS and Android. Given a board with $n$ rows and $m$ columns, the target of the game is to connect pairs of grids with the same numbers. Once two numbers are paired, the path connecting them will occupy the corresponding grids. The path can only go vertically or horizontally. Note that, no two paths could intersect (by sharing the same grid) in any grid. In this problem, you are going to play a modified version, called Number Link ++. See the picture below for an example.\u003cbr\u003e\u003cbr\u003e\u003ccenter\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/9a5ed94981331b9ba475865cfc843d93?v\u003d1726331943\"\u003e\u003c/center\u003e\u003cbr\u003e\u003cbr\u003eIn this new game, you can use two types of paths. Type I is to connect two number grids with different parities (i.e., connect odd number with any other even number). It might be hard to cover the entire grid with only type I path, so we allow type II path, which is a circle path covers only the empty grids (the only \u003cb\u003especial case of type II path\u003c/b\u003e is a path only connecting two adjacent empty grids; see the figure above). Since there is no free lunch, we have no free path either. When goes from grid $(a,b)$ to an adjacent grid $(c,d)$, you have to pay for a certain amount of tolls. The cost is the same when goes back from $(c,d)$ to $(a,b)$. Usually the cost of a path is the sum of tolls you paid by traveling along the grids on this path. The only exception is for the special case of type II path. In that case, you have to \u003cb\u003epay twice\u003c/b\u003e the cost (since it is a circle).\u003cbr\u003eThe total cost of the game is the sum of costs for all the paths. Can you help me figure out the paths so that each grid is on exactly one path? If there exists such solution, what is the minimum possible cost?\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The first line of input consists of an integer $T$, which is the number of test cases.\u003cbr\u003eEach case begins with two integers, $n$ and $m$, in a line $( 1 \\le n, m \\le 50)$.\u003cbr\u003eThe next $n$ lines describe the board. Each line consists of $m$ nonnegative numbers, which describe the status of each column from left to right. If the number is zero, then the grid is empty; otherwise it indicates the number on the corresponding grid.\u003cbr\u003eThe next $n-1$ lines each have $m$ nonnegative numbers, which describe the cost of vertical connection. The $j$-th number in $i$-th line is the cost when travels from grid $(i,j)$ to $(i+1,j)$.\u003cbr\u003eThe next $n$ lines each have $m-1$ nonnegative numbers, which describe the cost of horizontal connection. The $j$-th number in $i$-th line is the cost for a path to go from grid $(i,j)$ to $(i,j + 1)$.\u003cbr\u003eAll the numbers, including the answer, can be represented using $3$2-bit signed integer.\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":"For each test case, first output the case number, then output a single number, which is the minimum cost possible to finish the game. When there is no solution available, simply output -1."}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3\r\n3 3\r\n1 0 0\r\n1 0 0\r\n2 0 2\r\n1 2 1\r\n2 1 1\r\n3 1\r\n5 6\r\n1 4\r\n1 4\r\n1 1 2 2\r\n1 2 3\r\n3 5\r\n0 0 0 0 0\r\n0 5 0 6 0\r\n0 0 0 0 0\r\n1 1000 1000 1000 1\r\n1 1000 1000 1000 1\r\n1 1 1 1\r\n1000 1 1 1000\r\n1 1 1 1\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase #1: 10\r\nCase #2: -1\r\nCase #3: 14\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"\u003cbr\u003eBelow are the solutions corresponding to case 1 and case 3 respectively. In case 1, you should double pay the red path, since it is a special case of type II path.\u003cbr\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/6cb9850e48a0b68f45e6a64abc66cb86?v\u003d1726331943\"\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/811f5541c8edcbd18e4fdf75ce73e03b?v\u003d1726331943\"\u003e\u003cbr\u003e"}}]}