use std::cmp::{max, min};
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("Failed to read bytes") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n:usize = read();
    let mut a = vec![vec![0; n]; n];
    for i in 0..n {
        for j in 0..n {
            a[i][j] = read();
         }
    }
    let mut s = vec![vec![0; n+1]; n+1];
    for i in 0..n {
        for j in 0..n {
            s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + a[i][j];
        }
    }
    let mut val = vec![0; n*n+1];
    for x1 in 0..n {
        for x2 in 0..n+1 {
            for y1 in 0..n {
                for y2 in 0..n+1 {
                    let area = (max(x2, x1) - min(x1, x2)) * (max(y2, y1) - min(y2, y1));
                    let sum = s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1];
                    val[area] = max(val[area], sum);
                }
            }
        }
    }
    for v in 0..n*n {
        val[v+1] = max(val[v+1], val[v]);
    }

    let q:usize = read();
    for _ in 0..q {
        let i:usize = read();
        println!("{}", val[i]);
    }
}
