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("3");
WillReturn.Add("3 2 1");
WillReturn.Add("2 2 1");
WillReturn.Add("1 1 1");
WillReturn.Add("3");
WillReturn.Add("1");
WillReturn.Add("4");
WillReturn.Add("9");
//3
//9
//14
}
else if (InputPattern == "Input2") {
WillReturn.Add("3");
WillReturn.Add("1 1 1");
WillReturn.Add("1 1 1");
WillReturn.Add("9 9 9");
WillReturn.Add("1");
WillReturn.Add("4");
//27
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int N = int.Parse(InputList[0]);
int[,] BanArr = CreateBanArr(InputList.Skip(1).Take(N));
int UB_X = BanArr.GetUpperBound(0);
int UB_Y = BanArr.GetUpperBound(1);
var QList = new List<int>();
foreach (string EachStr in InputList.Skip(1 + N + 1)) {
QList.Add(int.Parse(EachStr));
}
// 2次元累積和を求める
int[,] RunSumArr = (int[,])BanArr.Clone();
// 累積和を設定する (横方向)
for (int LoopX = 1; LoopX <= UB_X; LoopX++) {
for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
RunSumArr[LoopX, LoopY] += RunSumArr[LoopX - 1, LoopY];
}
}
// 累積和を設定する (縦方向)
for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
for (int LoopY = 1; LoopY <= UB_Y; LoopY++) {
RunSumArr[LoopX, LoopY] += RunSumArr[LoopX, LoopY - 1];
}
}
// RMaxQ and 1点更新なセグ木
var InsSegmentTree = new SegmentTree(N * N + 1);
for (int StaX = 0; StaX <= UB_X; StaX++) {
for (int EndX = StaX; EndX <= UB_X; EndX++) {
for (int StaY = 0; StaY <= UB_Y; StaY++) {
for (int EndY = StaY; EndY <= UB_Y; EndY++) {
int CurrSum = DeriveSumRect(RunSumArr, EndX, EndY);
CurrSum -= DeriveSumRect(RunSumArr, StaX - 1, EndY);
CurrSum -= DeriveSumRect(RunSumArr, EndX, StaY - 1);
CurrSum += DeriveSumRect(RunSumArr, StaX - 1, StaY - 1);
// 長方形のマスの数
int MasuCnt = (EndX - StaX + 1) * (EndY - StaY + 1);
int SegVal = InsSegmentTree.Query(MasuCnt, MasuCnt, 0);
if (SegVal < CurrSum) {
InsSegmentTree.Update(MasuCnt, CurrSum);
}
}
}
}
}
foreach (int EachQ in QList) {
int SegVal = InsSegmentTree.Query(0, EachQ, 0);
Console.WriteLine(SegVal);
}
}
// (0,0)と(pX,pY)からなる長方形の和を返す
static int DeriveSumRect(int[,] pRunSumArr, int pX, int pY)
{
if (pX < 0) return 0;
if (pY < 0) return 0;
return pRunSumArr[pX, pY];
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をintの2次元配列に設定する
////////////////////////////////////////////////////////////////
static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new int[0, 0];
}
int[] IntArr = { };
Action<string> SplitAct = pStr =>
IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(StrList[0]);
int UB_X = IntArr.GetUpperBound(0);
int UB_Y = StrList.Count - 1;
int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
SplitAct(StrList[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(pBanArr[X, Y]);
}
Console.WriteLine();
}
}
}
#region SegmentTree
// SegmentTreeクラス (RMaxQ and 1点更新)
internal class SegmentTree
{
private int[] mTreeNodeArr;
private int UB; // 木のノードの配列のUB
private int mLeafCnt; // 葉ノードの数
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
private struct RangeInfoDef
{
internal int StaInd;
internal int EndInd;
}
private RangeInfoDef[] mRangeInfo;
// コンストラクタ
internal SegmentTree(int pLeafCnt)
{
// 簡単のため、葉ノード数を2のべき乗に
int ArrLength = 0;
for (int I = 1; I < int.MaxValue; I *= 2) {
ArrLength += I;
mLeafCnt = I;
if (pLeafCnt < mLeafCnt) break;
}
// すべての値をint.MinValueに
UB = ArrLength - 1;
mTreeNodeArr = new int[UB + 1];
for (int I = 0; I <= UB; I++) {
mTreeNodeArr[I] = int.MinValue;
}
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
mRangeInfo = new RangeInfoDef[UB + 1];
for (int I = 0; I <= UB; I++) {
if (I == 0) {
RangeInfoDef WillSet1;
WillSet1.StaInd = 0;
WillSet1.EndInd = mLeafCnt - 1;
mRangeInfo[I] = WillSet1;
continue;
}
int ParentNode = DeriveParentNode(I);
RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];
RangeInfoDef WillSet2;
int Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;
if (I % 2 == 1) { // 奇数ノードの場合
WillSet2.StaInd = ParentRangeInfo.StaInd;
WillSet2.EndInd = Mid;
}
else { // 偶数ノードの場合
WillSet2.StaInd = Mid + 1;
WillSet2.EndInd = ParentRangeInfo.EndInd;
}
mRangeInfo[I] = WillSet2;
}
}
// 親ノードの添字を取得
private int DeriveParentNode(int pTarget)
{
return (pTarget - 1) / 2;
}
// 子ノードの添字(小さいほう)を取得
private int DeriveChildNode(int pTarget)
{
return pTarget * 2 + 1;
}
// 葉ノードの配列の添字を木の添字に変換して返す
private int DeriveTreeNode(int pLeafArrInd)
{
int BaseInd = UB - mLeafCnt + 1;
return BaseInd + pLeafArrInd;
}
// 葉ノードの配列のK番目の値をNewValに変更
internal void Update(int pK, int pNewVal)
{
int CurrNode = DeriveTreeNode(pK);
mTreeNodeArr[CurrNode] = pNewVal;
// 登りながら更新
while (CurrNode > 0) {
CurrNode = DeriveParentNode(CurrNode);
int ChildNode1 = DeriveChildNode(CurrNode);
int ChildNode2 = ChildNode1 + 1;
mTreeNodeArr[CurrNode] =
Math.Max(mTreeNodeArr[ChildNode1], mTreeNodeArr[ChildNode2]);
}
}
// 開始添字と終了添字とカレントノードを引数として、最大値を返す
internal int Query(int pSearchStaInd, int pSearchEndInd, int pCurrNode)
{
int CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
int CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// OverLapしてなければ、int.MinValue
if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
return int.MinValue;
// 完全に含んでいれば、このノードの値
if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
return mTreeNodeArr[pCurrNode];
// そうでなければ、2つの子の最大値
int ChildNode1 = DeriveChildNode(pCurrNode);
int ChildNode2 = ChildNode1 + 1;
int ChildVal1 = Query(pSearchStaInd, pSearchEndInd, ChildNode1);
int ChildVal2 = Query(pSearchStaInd, pSearchEndInd, ChildNode2);
return Math.Max(ChildVal1, ChildVal2);
}
}
#endregion