/*
* @Author: hungeazy
* @Date: 2025-11-16 00:24:36
* @Last Modified by: hungeazy
* @Last Modified time: 2025-11-16 00:45:45
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
using namespace __gnu_pbds;
bool M1;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long
#define ll long long
#define ull unsigned long long
#define sz(x) x.size()
#define sqr(x) (1LL * (x) * (x))
#define all(x) x.begin(), x.end()
#define fill(f,x) memset(f,x,sizeof(f))
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define FOD(i,r,l) for(int i=r;i>=l;i--)
#define debug(x) cout << #x << " = " << x << '\n'
#define ii pair<int,int>
#define iii pair<int,ii>
#define di pair<ii,ii>
#define vi vector<int>
#define vii vector<ii>
#define mii map<int,int>
#define fi first
#define se second
#define pb push_back
#define MOD 1000000007
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define MASK(i) (1LL << (i))
#define c_bit(i) __builtin_popcountll(i)
#define BIT(x,i) ((x) & MASK(i))
#define SET_ON(x,i) ((x) | MASK(i))
#define SET_OFF(x,i) ((x) & ~MASK(i))
#define oo 1e18
#define name ""
#define endl '\n'
#define memory() cerr << abs(&M2-&M1)/1024.0/1024 << " MB" << endl
#define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." << endl
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
const int N = (int)5e3+10;
int n,q;
char a[N][N];
namespace sub3 {
int h[N],pre[N][N],L[N],R[N];
bool approved() {
return n <= 400 and q <= 400;
}
int calc()
{
FOR(i,1,n)
FOR(j,1,n)
if (a[i][j] == '1') pre[i][j] = 0;
else pre[i][j] = pre[i-1][j]+1;
int ans = 0;
FOR(i,1,n)
{
FOR(j,1,n) h[j] = pre[i][j];
deque<int> dq;
FOR(j,1,n)
{
while (!dq.empty() and h[j] <= h[dq.back()])
dq.pop_back();
L[j] = (!dq.empty() ? dq.back()+1 : 1);
dq.pb(j);
}
dq.clear();
FOD(j,n,1)
{
while (!dq.empty() and h[j] < h[dq.back()])
dq.pop_back();
R[j] = (!dq.empty() ? dq.back()-1 : n);
dq.pb(j);
}
FOR(j,1,n)
{
int x = j-L[j]+1, y = R[j]-j+1;
ans += x*y*pre[i][j];
}
}
return ans;
}
void solve(void)
{
cout << calc() << endl;
while (q--)
{
int x,y;
cin >> x >> y;
a[x][y] = (a[x][y] == '0' ? '1' : '0');
cout << calc() << endl;
}
}
}
bool M2;
signed main()
{
fast;
if (fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
cin >> n >> q;
FOR(i,1,n)
FOR(j,1,n) cin >> a[i][j];
if (sub3::approved()) return sub3::solve(), time(), memory(), 0;
time();
memory();
return 0;
}
// ██░ ██ █ ██ ███▄ █ ▄████
//▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
//▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
//░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
//░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
// ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
// ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
// ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
// ░ ░ ░ ░ ░ ░
LyoKKiBAQXV0aG9y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ICDilojiloggIOKWiOKWiOKWiOKWhCAgICDiloggICDiloTilojilojilojilogKLy/ilpPilojilojilpEg4paI4paI4paSIOKWiOKWiCAg4paT4paI4paI4paSIOKWiOKWiCDiloDiloggICDiloggIOKWiOKWiOKWkiDiloDilojilpIKLy/ilpLilojilojiloDiloDilojilojilpHilpPilojiloggIOKWkuKWiOKWiOKWkeKWk+KWiOKWiCAg4paA4paIIOKWiOKWiOKWkuKWkuKWiOKWiOKWkeKWhOKWhOKWhOKWkQovL+KWkeKWk+KWiCDilpHilojilogg4paT4paT4paIICDilpHilojilojilpHilpPilojilojilpIgIOKWkOKWjOKWiOKWiOKWkuKWkeKWk+KWiCAg4paI4paI4paTCi8v4paR4paT4paI4paS4paR4paI4paI4paT4paS4paS4paI4paI4paI4paI4paI4paTIOKWkuKWiOKWiOKWkSAgIOKWk+KWiOKWiOKWkeKWkeKWkuKWk+KWiOKWiOKWiOKWgOKWkgovLyDilpIg4paR4paR4paS4paR4paS4paR4paS4paT4paSIOKWkiDilpIg4paRIOKWkuKWkSAgIOKWkiDilpIgIOKWkeKWkiAgIOKWkgovLyDilpIg4paR4paS4paRIOKWkeKWkeKWkeKWkuKWkSDilpEg4paRIOKWkSDilpHilpEgICDilpEg4paS4paRICDilpEgICDilpEKLy8g4paRICDilpHilpEg4paRIOKWkeKWkeKWkSDilpEg4paRICAgIOKWkSAgIOKWkSDilpEg4paRIOKWkSAgIOKWkQovLyDilpEgIOKWkSAg4paRICAg4paRICAgICAgICAgICAgICDilpEgICAgICAg4paR