#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2005;
int n, m, k, nRotate, mRotate;
int arr[MAX_N][MAX_N];
int radius[MAX_N][MAX_N];
int prefixSumUnit[2 * MAX_N][2 * MAX_N];
int prefixSumUnitTriangle[2 * MAX_N][2 * MAX_N];
long long prefixSum[2 * MAX_N][2 * MAX_N],
prefixSumTriangle[2 * MAX_N][2 * MAX_N];
long long answer[MAX_N][MAX_N];
pair<int, int> getPos(int x, int y) { return make_pair(x + y - 1, n - x + y); }
int getSumUnit(int u, int v, int x, int y) {
u = max(u, 1);
v = max(v, 1);
x = min(x, nRotate);
y = min(y, nRotate);
return prefixSumUnit[x][y] - prefixSumUnit[x][v - 1] -
prefixSumUnit[u - 1][y] + prefixSumUnit[u - 1][v - 1];
}
long long getSum(int u, int v, int x, int y) {
u = max(u, 1);
v = max(v, 1);
x = min(x, nRotate);
y = min(y, nRotate);
return prefixSum[x][y] - prefixSum[x][v - 1] - prefixSum[u - 1][y] +
prefixSum[u - 1][v - 1];
}
void calculateRadius() {
int row, col;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
tie(row, col) = getPos(i, j);
prefixSumUnit[row][col] = arr[i][j];
}
}
nRotate = n + m - 1;
for (int i = 1; i <= nRotate; i++) {
for (int j = 1; j <= nRotate; j++) {
prefixSumUnit[i][j] =
prefixSumUnit[i][j] + prefixSumUnit[i - 1][j] +
prefixSumUnit[i][j - 1] - prefixSumUnit[i - 1][j - 1];
}
}
radius[1][1] = 0;
tie(row, col) = getPos(1, 1);
while (getSumUnit(row - radius[1][1], col - radius[1][1],
row + radius[1][1], col + radius[1][1]) < k) {
radius[1][1]++;
}
for (int j = 2; j <= m; j++) {
radius[1][j] = radius[1][j - 1] + 1;
int &tmp = radius[1][j];
tie(row, col) = getPos(1, j);
while (tmp > 0 && getSumUnit(row - tmp + 1, col - tmp + 1,
row + tmp - 1, col + tmp - 1) >= k) {
tmp--;
}
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= m; j++) {
radius[i][j] = radius[i - 1][j] + 1;
int &tmp = radius[i][j];
tie(row, col) = getPos(i, j);
while (tmp > 0 && getSumUnit(row - tmp + 1, col - tmp + 1,
row + tmp - 1, col + tmp - 1) >= k) {
tmp--;
}
}
}
}
long long getSumTriangleX(int u, int v, int x, int y) {
long long result = 0;
if (u < 1) {
int tmp = 1 - u;
u += tmp;
v += tmp;
}
if (y > nRotate) {
int tmp = y - nRotate;
x -= tmp;
y -= tmp;
}
if (v < 1) {
int tmp = 1 - v;
result += getSum(u, 1, u + tmp - 1, y);
u += tmp;
v += tmp;
}
if (x > nRotate) {
int tmp = x - nRotate;
result += getSum(u, y - tmp + 1, nRotate, y);
x -= tmp;
y -= tmp;
}
result += prefixSumTriangle[x][y] - prefixSumTriangle[u - 1][v - 1] -
getSum(1, v, u - 1, y);
return result;
}
int getSumUnitTriangleX(int u, int v, int x, int y) {
int result = 0;
if (u < 1) {
int tmp = 1 - u;
u += tmp;
v += tmp;
}
if (y > nRotate) {
int tmp = y - nRotate;
x -= tmp;
y -= tmp;
}
if (v < 1) {
int tmp = 1 - v;
result += getSumUnit(u, 1, u + tmp - 1, y);
u += tmp;
v += tmp;
}
if (x > nRotate) {
int tmp = x - nRotate;
result += getSumUnit(u, y - tmp + 1, nRotate, y);
x -= tmp;
y -= tmp;
}
result += prefixSumUnitTriangle[x][y] -
prefixSumUnitTriangle[u - 1][v - 1] - getSumUnit(1, v, u - 1, y);
return result;
}
void calculateAnswerX() {
int row, col;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
tie(row, col) = getPos(i, j);
prefixSum[row][col] = i * arr[i][j];
prefixSumUnitTriangle[row][col] = arr[i][j];
prefixSumTriangle[row][col] = i * arr[i][j];
}
}
for (int i = 1; i <= nRotate; i++) {
for (int j = 1; j <= nRotate; j++) {
prefixSum[i][j] = prefixSum[i][j] + prefixSum[i - 1][j] +
prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
}
}
for (int i = 2; i <= nRotate; i++) {
for (int j = 1; j <= nRotate; j++) {
prefixSumUnitTriangle[i][j] = prefixSumUnitTriangle[i][j] +
prefixSumUnitTriangle[i - 1][j] +
prefixSumUnitTriangle[i - 1][j - 1] -
prefixSumUnitTriangle[i - 2][j - 1];
prefixSumTriangle[i][j] = prefixSumTriangle[i][j] +
prefixSumTriangle[i - 1][j] +
prefixSumTriangle[i - 1][j - 1] -
prefixSumTriangle[i - 2][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (radius[i][j] > 0) {
tie(row, col) = getPos(i, j);
int tmp = radius[i][j] - 1;
int sumUnitUpTriangle = getSumUnitTriangleX(
row - tmp, col - tmp, row + tmp, col + tmp);
long long sumUpTriangle =
getSumTriangleX(row - tmp, col - tmp, row + tmp, col + tmp);
int sumUnitArea =
getSumUnit(row - tmp, col - tmp, row + tmp, col + tmp);
long long sumArea =
getSum(row - tmp, col - tmp, row + tmp, col + tmp);
answer[i][j] += i * sumUnitUpTriangle - sumUpTriangle +
(sumArea - sumUpTriangle) -
i * (sumUnitArea - sumUnitUpTriangle);
}
}
}
}
long long getSumTriangleY(int u, int v, int x, int y) {
long long result = 0;
if (u < 1) {
int tmp = 1 - u;
u += tmp;
v -= tmp;
}
if (y < 1) {
int tmp = 1 - y;
x -= tmp;
y += tmp;
}
if (x > nRotate) {
int tmp = x - nRotate;
result += getSum(u, y, nRotate, y + tmp - 1);
x -= tmp;
y += tmp;
}
if (v > nRotate) {
int tmp = v - nRotate;
result += getSum(u, y, u + tmp - 1, nRotate);
u += tmp;
v -= tmp;
}
result += prefixSumTriangle[x][y] - prefixSumTriangle[u - 1][v + 1] -
getSum(1, y, u - 1, v);
return result;
}
int getSumUnitTriangleY(int u, int v, int x, int y) {
int result = 0;
if (u < 1) {
int tmp = 1 - u;
u += tmp;
v -= tmp;
}
if (y < 1) {
int tmp = 1 - y;
x -= tmp;
y += tmp;
}
if (x > nRotate) {
int tmp = x - nRotate;
result += getSumUnit(u, y, nRotate, y + tmp - 1);
x -= tmp;
y += tmp;
}
if (v > nRotate) {
int tmp = v - nRotate;
result += getSumUnit(u, y, u + tmp - 1, nRotate);
u += tmp;
v -= tmp;
}
result += prefixSumUnitTriangle[x][y] -
prefixSumUnitTriangle[u - 1][v + 1] - getSumUnit(1, y, u - 1, v);
return result;
}
void calculateAnswerY() {
memset(prefixSum, 0, sizeof(prefixSum));
memset(prefixSumTriangle, 0, sizeof(prefixSumTriangle));
memset(prefixSumUnitTriangle, 0, sizeof(prefixSumUnitTriangle));
int row, col;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
tie(row, col) = getPos(i, j);
prefixSum[row][col] = j * arr[i][j];
prefixSumUnitTriangle[row][col] = arr[i][j];
prefixSumTriangle[row][col] = j * arr[i][j];
}
}
for (int i = 1; i <= nRotate; i++) {
for (int j = 1; j <= nRotate; j++) {
prefixSum[i][j] = prefixSum[i][j] + prefixSum[i - 1][j] +
prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
}
}
for (int i = 2; i <= nRotate; i++) {
for (int j = 1; j <= nRotate; j++) {
prefixSumUnitTriangle[i][j] = prefixSumUnitTriangle[i][j] +
prefixSumUnitTriangle[i - 1][j] +
prefixSumUnitTriangle[i - 1][j + 1] -
prefixSumUnitTriangle[i - 2][j + 1];
prefixSumTriangle[i][j] = prefixSumTriangle[i][j] +
prefixSumTriangle[i - 1][j] +
prefixSumTriangle[i - 1][j + 1] -
prefixSumTriangle[i - 2][j + 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (radius[i][j] > 0) {
tie(row, col) = getPos(i, j);
int tmp = radius[i][j] - 1;
int sumUnitUpTriangle = getSumUnitTriangleY(
row - tmp, col + tmp, row + tmp, col - tmp);
long long sumUpTriangle =
getSumTriangleY(row - tmp, col + tmp, row + tmp, col - tmp);
int sumUnitArea =
getSumUnit(row - tmp, col - tmp, row + tmp, col + tmp);
long long sumArea =
getSum(row - tmp, col - tmp, row + tmp, col + tmp);
answer[i][j] += j * sumUnitUpTriangle - sumUpTriangle +
(sumArea - sumUpTriangle) -
j * (sumUnitArea - sumUnitUpTriangle);
}
}
}
}
long long calculateAnswer() {
long long result = 0;
int row, col;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (radius[i][j] > 0) {
tie(row, col) = getPos(i, j);
int tmp = radius[i][j] - 1;
result += answer[i][j] +
radius[i][j] * (k - getSumUnit(row - tmp, col - tmp,
row + tmp, col + tmp));
}
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("formation.inp", "r", stdin);
freopen("formation.out", "w", stdout);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> arr[i][j];
}
}
calculateRadius();
calculateAnswerX();
calculateAnswerY();
cout << calculateAnswer();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWF9OID0gMjAwNTsKCmludCBuLCBtLCBrLCBuUm90YXRlLCBtUm90YXRlOwppbnQgYXJyW01BWF9OXVtNQVhfTl07CmludCByYWRpdXNbTUFYX05dW01BWF9OXTsKaW50IHByZWZpeFN1bVVuaXRbMiAqIE1BWF9OXVsyICogTUFYX05dOwppbnQgcHJlZml4U3VtVW5pdFRyaWFuZ2xlWzIgKiBNQVhfTl1bMiAqIE1BWF9OXTsKbG9uZyBsb25nIHByZWZpeFN1bVsyICogTUFYX05dWzIgKiBNQVhfTl0sCiAgICBwcmVmaXhTdW1UcmlhbmdsZVsyICogTUFYX05dWzIgKiBNQVhfTl07CmxvbmcgbG9uZyBhbnN3ZXJbTUFYX05dW01BWF9OXTsKCnBhaXI8aW50LCBpbnQ+IGdldFBvcyhpbnQgeCwgaW50IHkpIHsgcmV0dXJuIG1ha2VfcGFpcih4ICsgeSAtIDEsIG4gLSB4ICsgeSk7IH0KCmludCBnZ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ICAgICAgIHByZWZpeFN1bVVuaXRbaV1bal0gPQogICAgICAgICAgICAgICAgcHJlZml4U3VtVW5pdFtpXVtqXSArIHByZWZpeFN1bVVuaXRbaSAtIDFdW2pdICsKICAgICAgICAgICAgICAgIHByZWZpeFN1bVVuaXRbaV1baiAtIDFdIC0gcHJlZml4U3VtVW5pdFtpIC0gMV1baiAtIDFdOwogICAgICAgIH0KICAgIH0KICAgIHJhZGl1c1sxXVsxXSA9IDA7CiAgICB0aWUocm93LCBjb2wpID0gZ2V0UG9zKDEsIDEpOwogICAgd2hpbGUgKGdldFN1bVVuaXQocm93IC0gcmFkaXVzWzFdWzFdLCBjb2wgLSByYWRpdXNbMV1bMV0sCiAgICAgICAgICAgICAgICAgICAgICByb3cgKyByYWRpdXNbMV1bMV0sIGNvbCArIHJhZGl1c1sxXVsxXSkgPCBrKSB7CiAgICAgICAgcmFkaXVzWzFdWzFdKys7CiAgICB9CiAgICBmb3IgKGludCBqID0gMjsgaiA8PSBtOyBqKyspIHsKICAgICAgICByYWRpdXNbMV1bal0gPSByYWRpdXNbMV1baiAtIDFdICsgMTsKICAgICAgICBpbnQgJnRtcCA9IHJhZGl1c1sxXVtqXTsKICAgICAgICB0aWUocm93LCBjb2wpID0gZ2V0UG9zKDEsIGopOwogICAgICAgIHdoaWxlICh0bXAgPiAwICYmIGdldFN1bVVuaXQocm93IC0gdG1wICsgMSwgY29sIC0gdG1wICsgMSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJvdyArIHRtcCAtIDEsIGNvbCArIHRtcCAtIDEpID49IGspIHsKICAgICAgICAgICAgdG1wLS07CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbTsgaisrKSB7CiAgICAgICAgICAgIHJhZGl1c1tpXVtqXSA9IHJhZGl1c1tpIC0gMV1bal0gKyAxOwogICAgICAgICAgICBpbnQgJnRtcCA9IHJhZGl1c1tpXVtqXTsKICAgICAgICAgICAgdGllKHJvdywgY29sKSA9IGdldFBvcyhpLCBqKTsKICAgICAgICAgICAgd2hpbGUgKHRtcCA+IDAgJiYgZ2V0U3VtVW5pdChyb3cgLSB0bXAgKyAxLCBjb2wgLSB0bXAgKyAxLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJvdyArIHRtcCAtIDEsIGNvbCArIHRtcCAtIDEpID49IGspIHsKICAgICAgICAgICAgICAgIHRtcC0tOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cgpsb25nIGxvbmcgZ2V0U3VtVHJpYW5nbGVYKGludCB1LCBpbnQgdiwgaW50IHgsIGludCB5KSB7CiAgICBsb25nIGxvbmcgcmVzdWx0ID0gMDsKICAgIGlmICh1IDwgMSkgewogICAgICAgIGludCB0bXAgPSAxIC0gdTsKICAgICAgICB1ICs9IHRtcDsKICAgICAgICB2ICs9IHRtcDsKICAgIH0KICAgIGlmICh5ID4gblJvdGF0ZSkgewogICAgICAgIGludCB0bXAgPSB5IC0gblJvdGF0ZTsKICAgICAgICB4IC09IHRtcDsKICAgICAgICB5IC09IHRtcDsKICAgIH0KICAgIGlmICh2IDwgMSkgewogICAgICAgIGludCB0bXAgPSAxIC0gdjsKICAgICAgICByZXN1bHQgKz0gZ2V0U3VtKHUsIDEsIHUgKyB0bXAgLSAxLCB5KTsKICAgICAgICB1ICs9IHRtcDsKICAgICAgICB2ICs9IHRtcDsKICAgIH0KICAgIGlmICh4ID4gblJvdGF0ZSkgewogICAgICAgIGludCB0bXAgPSB4IC0gblJvdGF0ZTsKICAgICAgICByZXN1bHQgKz0gZ2V0U3VtKHUsIHkgLSB0bXAgKyAxLCBuUm90YXRlLCB5KTsKICAgICAgICB4IC09IHRtcDsKICAgICAgICB5IC09IHRtcDsKICAgIH0KICAgIHJlc3VsdCArPSBwcmVmaXhTdW1UcmlhbmdsZVt4XVt5XSAtIHByZWZpeFN1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TsgaSA8PSBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBtOyBqKyspIHsKICAgICAgICAgICAgdGllKHJvdywgY29sKSA9IGdldFBvcyhpLCBqKTsKICAgICAgICAgICAgcHJlZml4U3VtW3Jvd11bY29sXSA9IGkgKiBhcnJbaV1bal07CiAgICAgICAgICAgIHByZWZpeFN1bVVuaXRUcmlhbmdsZVtyb3ddW2NvbF0gPSBhcnJbaV1bal07CiAgICAgICAgICAgIHByZWZpeFN1bVRyaWFuZ2xlW3Jvd11bY29sXSA9IGkgKiBhcnJbaV1bal07CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gblJvdGF0ZTsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gblJvdGF0ZTsgaisrKSB7CiAgICAgICAgICAgIHByZWZpeFN1bVtpXVtqXSA9IHByZWZpeFN1bVtpXVtqXSArIHByZWZpeFN1bVtpIC0gMV1bal0gKwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmVmaXhTdW1baV1baiAtIDFdIC0gcHJlZml4U3VtW2kgLSAxXVtqIC0gMV07CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gblJvdGF0ZTsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gblJvdGF0ZTsgaisrKSB7CiAgICAgICAgICAgIHByZWZpeFN1bVVuaXRUcmlhbmdsZVtpXVtqXSA9IHByZWZpeFN1bVVuaXRUcmlhbmdsZVtpXVtqXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVVuaXRUcmlhbmdsZVtpIC0gMV1bal0gKwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmVmaXhTdW1Vbml0VHJpYW5nbGVbaSAtIDFdW2ogLSAxXSAtCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVVuaXRUcmlhbmdsZVtpIC0gMl1baiAtIDFdOwoKICAgICAgICAgICAgcHJlZml4U3VtVHJpYW5nbGVbaV1bal0gPSBwcmVmaXhTdW1UcmlhbmdsZVtpXVtqXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJlZml4U3VtVHJpYW5nbGVbaSAtIDFdW2pdICsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmVmaXhTdW1UcmlhbmdsZVtpIC0gMV1baiAtIDFdIC0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmVmaXhTdW1UcmlhbmdsZVtpIC0gMl1baiAtIDFdOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG07IGorKykgewogICAgICAgICAgICBpZiAocmFkaXVzW2ldW2pdID4gMCkgewogICAgICAgICAgICAgICAgdGllKHJvdywgY29sKSA9IGdldFBvcyhpLCBqKTsKICAgICAgICAgICAgICAgIGludCB0bXAgPSByYWRpdXNbaV1bal0gLSAxOwogICAgICAgICAgICAgICAgaW50IHN1bVVuaXRVcFRyaWFuZ2xlID0gZ2V0U3VtVW5pdFRyaWFuZ2xlWCgKICAgICAgICAgICAgICAgICAgICByb3cgLSB0bXAsIGNvbCAtIHRtcCwgcm93ICsgdG1wLCBjb2wgKyB0bXApOwogICAgICAgICAgICAgICAgbG9uZyBsb25nIHN1bVVwVHJpYW5nbGUgPQogICAgICAgICAgICAgICAgICAgIGdldFN1bVRyaWFuZ2xlWChyb3cgLSB0bXAsIGNvbCAtIHRtcCwgcm93ICsgdG1wLCBjb2wgKyB0bXApOwogICAgICAgICAgICAgICAgaW50IHN1bVVuaXRBcmVhID0KICAgICAgICAgICAgICAgICAgICBnZXRTdW1Vbml0KHJvdyAtIHRtcCwgY29sIC0gdG1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IDFdW3YgKyAxXSAtIGdldFN1bVVuaXQoMSwgeSwgdSAtIDEsIHYpOwogICAgcmV0dXJuIHJlc3VsdDsKfQoKdm9pZCBjYWxjdWxhdGVBbnN3ZXJZKCkgewogICAgbWVtc2V0KHByZWZpeFN1bSwgMCwgc2l6ZW9mKHByZWZpeFN1bSkpOwogICAgbWVtc2V0KHByZWZpeFN1bVRyaWFuZ2xlLCAwLCBzaXplb2YocHJlZml4U3VtVHJpYW5nbGUpKTsKICAgIG1lbXNldChwcmVmaXhTdW1Vbml0VHJpYW5nbGUsIDAsIHNpemVvZihwcmVmaXhTdW1Vbml0VHJpYW5nbGUpKTsKICAgIGludCByb3csIGNvbDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG07IGorKykgewogICAgICAgICAgICB0aWUocm93LCBjb2wpID0gZ2V0UG9zKGksIGopOwogICAgICAgICAgICBwcmVmaXhTdW1bcm93XVtjb2xdID0gaiAqIGFycltpXVtqXTsKICAgICAgICAgICAgcHJlZml4U3VtVW5pdFRyaWFuZ2xlW3Jvd11bY29sXSA9IGFycltpXVtqXTsKICAgICAgICAgICAgcHJlZml4U3VtVHJpYW5nbGVbcm93XVtjb2xdID0gaiAqIGFycltpXVtqXTsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuUm90YXRlOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBuUm90YXRlOyBqKyspIHsKICAgICAgICAgICAgcHJlZml4U3VtW2ldW2pdID0gcHJlZml4U3VtW2ldW2pdICsgcHJlZml4U3VtW2kgLSAxXVtqXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVtpXVtqIC0gMV0gLSBwcmVmaXhTdW1baSAtIDFdW2ogLSAxXTsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMjsgaSA8PSBuUm90YXRlOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBuUm90YXRlOyBqKyspIHsKICAgICAgICAgICAgcHJlZml4U3VtVW5pdFRyaWFuZ2xlW2ldW2pdID0gcHJlZml4U3VtVW5pdFRyaWFuZ2xlW2ldW2pdICsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJlZml4U3VtVW5pdFRyaWFuZ2xlW2kgLSAxXVtqXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVVuaXRUcmlhbmdsZVtpIC0gMV1baiArIDFdIC0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJlZml4U3VtVW5pdFRyaWFuZ2xlW2kgLSAyXVtqICsgMV07CgogICAgICAgICAgICBwcmVmaXhTdW1UcmlhbmdsZVtpXVtqXSA9IHByZWZpeFN1bVRyaWFuZ2xlW2ldW2pdICsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmVmaXhTdW1UcmlhbmdsZVtpIC0gMV1bal0gKwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVRyaWFuZ2xlW2kgLSAxXVtqICsgMV0gLQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByZWZpeFN1bVRyaWFuZ2xlW2kgLSAyXVtqICsgMV07CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbTsgaisrKSB7CiAgICAgICAgICAgIGlmIChyYWRpdXNbaV1bal0gPiAwKSB7CiAgICAgICAgICAgICAgICB0aWUocm93LCBjb2wpID0gZ2V0UG9zKGksIGopOwogICAgICAgICAgICAgICAgaW50IHRtcCA9IHJhZGl1c1tpXVtqXSAtIDE7CiAgICAgICAgICAgICAgICBpbnQgc3VtVW5pdFVwVHJpYW5nbGUgPSBnZXRTdW1Vbml0VHJpYW5nbGVZKAogICAgICAgICAgICAgICAgICAgIHJvdyAtIHRtcCwgY29sICsgdG1wLCByb3cgKyB0bXAsIGNvbCAtIHRtcCk7CiAgICAgICAgICAgICAgICBsb25nIGxvbmcgc3VtVXBUcmlhbmdsZSA9CiAgICAgICAgICAgICAgICAgICAgZ2V0U3VtVHJpYW5nbGVZKHJvdyAtIHRtcCwgY29sICsgdG1wLCByb3cgKyB0bXAsIGNvbCAtIHRtcCk7CiAgICAgICAgICAgICAgICBpbnQgc3VtVW5pdEFyZWEgPQogICAgICAgICAgICAgICAgICAgIGdldFN1bVVuaXQocm93IC0gdG1wLCBjb2wgLSB0bXAsIHJvdyArIHRtcCwgY29sICsgdG1wKTsKICAgICAgICAgICAgICAgIGxvbmcgbG9uZyBzdW1BcmVhID0KICAgICAgICAgICAgICAgICAgICBnZXRTdW0ocm93IC0gdG1wLCBjb2wgLSB0bXAsIHJvdyArIHRtcCwgY29sICsgdG1wKTsKICAgICAgICAgICAgICAgIGFuc3dlcltpXVtqXSArPSBqICogc3VtVW5pdFVwVHJpYW5nbGUgLSBzdW1VcFRyaWFuZ2xlICsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoc3VtQXJlYSAtIHN1bVVwVHJpYW5nbGUpIC0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBqICogKHN1bVVuaXRBcmVhIC0gc3VtVW5pdFVwVHJpYW5nbGUpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cgpsb25nIGxvbmcgY2FsY3VsYXRlQW5zd2VyKCkgewogICAgbG9uZyBsb25nIHJlc3VsdCA9IDA7CiAgICBpbnQgcm93LCBjb2w7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBtOyBqKyspIHsKICAgICAgICAgICAgaWYgKHJhZGl1c1tpXVtqXSA+IDApIHsKICAgICAgICAgICAgICAgIHRpZShyb3csIGNvbCkgPSBnZXRQb3MoaSwgaik7CiAgICAgICAgICAgICAgICBpbnQgdG1wID0gcmFkaXVzW2ldW2pdIC0gMTsKICAgICAgICAgICAgICAgIHJlc3VsdCArPSBhbnN3ZXJbaV1bal0gKwogICAgICAgICAgICAgICAgICAgICAgICAgIHJhZGl1c1tpXVtqXSAqIChrIC0gZ2V0U3VtVW5pdChyb3cgLSB0bXAsIGNvbCAtIHRtcCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcm93ICsgdG1wLCBjb2wgKyB0bXApKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiByZXN1bHQ7Cn0KCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKDApOwogICAgY291dC50aWUoMCk7CiAgICBmcmVvcGVuKCJmb3JtYXRpb24uaW5wIiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJmb3JtYXRpb24ub3V0IiwgInciLCBzdGRvdXQpOwogICAgY2luID4+IG4gPj4gbSA+PiBrOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbTsgaisrKSB7CiAgICAgICAgICAgIGNpbiA+PiBhcnJbaV1bal07CiAgICAgICAgfQogICAgfQogICAgY2FsY3VsYXRlUmFkaXVzKCk7CiAgICBjYWxjdWxhdGVBbnN3ZXJYKCk7CiAgICBjYWxjdWxhdGVBbnN3ZXJZKCk7CiAgICBjb3V0IDw8IGNhbGN1bGF0ZUFuc3dlcigpOwogICAgcmV0dXJuIDA7Cn0K