#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; class Solution { public: void solve(std::istream& in, std::ostream& out) { int n, m; in >> n >> m; ll mat[n][n]; for (int ctr1 = 0; ctr1 < n; ++ctr1) for (int ctr2 = 0; ctr2 < n; ++ctr2) in >> mat[ctr1][ctr2]; ll acc[n + 1][n + 1]; memset(acc, 0, sizeof(acc)); for (int ctr1 = 0; ctr1 < n; ++ctr1) for (int ctr2 = 0; ctr2 < n; ++ctr2) acc[ctr1 + 1][ctr2 + 1] = mat[ctr1][ctr2] + acc[ctr1 + 1][ctr2] + acc[ctr1][ctr2 + 1] - acc[ctr1][ctr2]; ll dp[n + 1][n + 1]; memset(dp, 0, sizeof(dp)); for (int ctr1 = 0; ctr1 < n; ++ctr1) for (int ctr2 = 0; ctr2 < n; ++ctr2) for (int ctr3 = ctr1; ctr3 < n; ++ctr3) for (int ctr4 = ctr2; ctr4 < n; ++ctr4) if (acc[ctr3 + 1][ctr4 + 1] - acc[ctr3 + 1][ctr2] - acc[ctr1][ctr4 + 1] + acc[ctr1][ctr2] == 0) { dp[ctr3 + 1][ctr4 + 1] += 1; dp[ctr3 + 1][ctr2] -= 1; dp[ctr1][ctr4 + 1] -= 1; dp[ctr1][ctr2] += 1; } for (int ctr1 = 0; ctr1 <= n; ++ctr1) for (int ctr2 = 0; ctr2 <= n; ++ctr2) { if (ctr1 > 0) dp[ctr1][ctr2] += dp[ctr1 - 1][ctr2]; if (ctr2 > 0) dp[ctr1][ctr2] += dp[ctr1][ctr2 - 1]; if (ctr1 > 0 && ctr2 > 0) dp[ctr1][ctr2] -= dp[ctr1 - 1][ctr2 - 1]; } int x, y; while (m--) { in >> x >> y; out << dp[x - 1][y - 1] << "\n"; } } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } // Powered by caide (code generator, tester, and library code inliner) #include #include int main() { ios_base::sync_with_stdio(false); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); return 0; }