The Summing Square
UptoSkills Arena's canteen serves delicious rice bowls. The bowls are arranged in a square grid. The manager wants to know the total amount of rice (in grams) in a specific square sub-section of the grid. Given the grid dimensions and the top-left corner and size of the sub-square, calculate the total rice.
Let the grid be represented by a 2D array G where G[i][j] is the amount of rice in grams at row i and column j.
You are given the grid, and three integers r, c, and k. You need to calculate the sum of all elements in the square subgrid starting at row r, column c with side length k.
Input
First line: two integers N, M (grid dimensions). Next N lines: M integers each, representing the grid. Last line: three integers r, c, k (top-left corner row and column, and side length). Rows and columns are 0-indexed.
Output
A single integer: the sum of elements in the specified sub-square.
Constraints
1 <= N, M <= 100 0 <= G[i][j] <= 1000 0 <= r < N 0 <= c < M 1 <= k r + k <= N c + k <= M
Example 1
3 4 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2
34
Sum of the 2x2 square starting at (1,1): 6 + 7 + 10 + 11 = 34.
Example 2
2 2 10 20 30 40 0 0 1
10
Sum of the 1x1 square starting at (0,0): 10.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
3 4 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2
2 2 10 20 30 40 0 0 1