using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 6");
WillReturn.Add("9 9 9 9 1 0");
WillReturn.Add("9 9 9 9 1 9");
WillReturn.Add("9 9 9 1 1 1");
WillReturn.Add("9 1 1 1 9 1");
WillReturn.Add("0 1 9 9 9 0");
//10
}
else if (InputPattern == "Input2") {
WillReturn.Add("10 10");
WillReturn.Add("1 2 265 1544 0 1548 4334 9846 58 0");
WillReturn.Add("21 0 50 44 2 388 5 0 0 4");
WillReturn.Add("170 0 2 1 54 1379 50 3 41 0");
WillReturn.Add("310 0 1 0 2163 0 226 26 3 12");
WillReturn.Add("151 33 0 9 0 0 0 36 365 2286");
WillReturn.Add("0 3 12 3 9 317 645 100 21 4");
WillReturn.Add("52 1 569 0 144 0 6 202 25 0");
WillReturn.Add("8869 19 2058 1948 1252 1002 7 1750 0 5");
WillReturn.Add("0 3 8 29 2 4403 0 0 0 5");
WillReturn.Add("0 17 93 9367 159 6 1 216 0 0");
//246
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int[,] mBanArr;
static int UB_X;
static int UB_Y;
static void Main()
{
List<string> InputList = GetInputList();
mBanArr = CreateBanArr(InputList.Skip(1).ToList());
UB_X = mBanArr.GetUpperBound(0);
UB_Y = mBanArr.GetUpperBound(1);
int[,] MinKyoriArr1 = DeriveMinKyoriArr(0, UB_Y);
int[,] MinKyoriArr2 = DeriveMinKyoriArr(UB_X, 0);
int[,] MinKyoriArr3 = DeriveMinKyoriArr(UB_X, UB_Y);
// 中継地点を全探索する
int Answer = int.MaxValue;
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
int AnswerKouho = MinKyoriArr1[X, Y] + MinKyoriArr2[X, Y] + MinKyoriArr3[X, Y];
AnswerKouho -= mBanArr[X, Y] * 2; // 2回余計に足してるので引く
Answer = Math.Min(Answer, AnswerKouho);
}
}
Console.WriteLine(Answer);
}
struct JyoutaiDef
{
internal int CurrX;
internal int CurrY;
internal int Cost;
}
// 始点を引数として、各座標までの最短距離の2次元配列を返す
static int[,] DeriveMinKyoriArr(int pStaX, int pStaY)
{
int[,] MinKyoriArr = new int[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= MinKyoriArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= MinKyoriArr.GetUpperBound(1); Y++) {
MinKyoriArr[X, Y] = int.MaxValue;
}
}
MinKyoriArr[pStaX, pStaY] = 0;
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrX = pStaX;
WillEnqueue.CurrY = pStaY;
WillEnqueue.Cost = 0;
Que.Enqueue(WillEnqueue);
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
Action<int, int> PushAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB_X < pNewX) return;
if (pNewY < 0 || UB_Y < pNewY) return;
WillEnqueue.CurrX = pNewX;
WillEnqueue.CurrY = pNewY;
WillEnqueue.Cost = Dequeued.Cost + mBanArr[pNewX, pNewY];
if (WillEnqueue.Cost >= MinKyoriArr[pNewX, pNewY]) {
return;
}
MinKyoriArr[pNewX, pNewY] = WillEnqueue.Cost;
Que.Enqueue(WillEnqueue);
};
PushAct(Dequeued.CurrX, Dequeued.CurrY - 1);
PushAct(Dequeued.CurrX, Dequeued.CurrY + 1);
PushAct(Dequeued.CurrX - 1, Dequeued.CurrY);
PushAct(Dequeued.CurrX + 1, Dequeued.CurrY);
}
return MinKyoriArr;
}
////////////////////////////////////////////////////////////////
// stringのListをintの2次元配列に設定する
////////////////////////////////////////////////////////////////
static int[,] CreateBanArr(List<string> pStringList)
{
if (pStringList.Count == 0) {
return new int[0, 0];
}
int[] IntArr = { };
Action<string> SplitAct = pStr =>
IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(pStringList[0]);
int UB_X = IntArr.GetUpperBound(0);
int UB_Y = pStringList.Count - 1;
int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
SplitAct(pStringList[Y]);
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = IntArr[X];
}
}
return WillReturn;
}
////////////////////////////////////////////////////////////////
// 2次元配列(int型)のデバッグ出力
////////////////////////////////////////////////////////////////
static void PrintBan(int[,] pBanArr)
{
for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
Console.Write("{0,4},", pBanArr[X, Y]);
}
Console.WriteLine();
}
}
}