#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
int T[10][10];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(y,N) FOR(x,N) cin>>T[y][x];
	vector<int> V;
	FOR(x,N-1) V.push_back(x+1);
	int ret=0;
	do {
		int sum=T[0][V[0]]+T[V.back()][0];
		FOR(i,V.size()-1) sum+=T[V[i]][V[i+1]];
		if(sum==K) ret++;
		
	} while(next_permutation(ALL(V)));
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}

#pragma GCC optimize("Ofast") 
#include <bits/stdc++.h>
using namespace std;
using ll = long long int ;
using P = pair<ll,ll>;
using Graph= vector<vector<ll>>;
struct edge{ll to ; ll cost ;} ;
using graph =vector<vector<edge>> ;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back 
#define eb emplace_back 
#define ppb pop_back 
#define mpa make_pair 
#define fi first  
#define se second  
#define set20 cout<<fixed<<setprecision(20)  ;
const ll INF=1e18 ;   
inline void chmax(ll& a,ll b){a=max(a,b);}  
inline void chmin(ll& a,ll b){a=min(a,b);} 
long double pi=acos(-1) ;  
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}  
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} 
ll dx[4] {1,0,-1,0} ;
ll dy[4] {0,1,0,-1} ;

// sum(x) x以下の和
// sum(a,b) a以上b以下の和
template<typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n=0):n(n),d(n+1) {}
  void add(int i, T x) {  //x=1ならsumは個数のカウント
    for (i++; i <= n; i += i&-i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i&-i) {
      x += d[i];
    }
    return x;
  }
  T sum(int i,int j) {
    if(i>0) return sum(j)-sum(i-1);
    else return sum(j) ; } 
};

int main(){
  ios::sync_with_stdio(false) ;
  cin.tie(nullptr) ;
  ll n,m,q ; cin>>n>>m>>q ;
  vector<ll> A(n) ,B(m) ;
  ll sum=0 ;
  vector<pair<ll,pair<ll,ll>>> f(q) ;
  rep(i,q){
    cin>>f[i].fi>>f[i].se.fi>>f[i].se.se ;
  }
  
  vector<ll> ff(q+1) ;
  ff[0]=0ll ;
  rep(i,q) ff[i+1]=f[i].se.se ;
  vector<ll> ans(q+1) ; //圧縮後の配列
 vector<ll> za ;
 rep(i,q+1) za.pb(ff[i]) ;
 sort(za.begin(),za.end()) ;
 za.erase(unique(za.begin(),za.end()),za.end()) ;
 rep(i,q+1) ans[i] = lower_bound(za.begin(),za.end(),ff[i])-za.begin() ;
  //ans[i] //i+1
  map<ll,ll> mp ;
  rep(i,q+1){
    mp[ff[i]]=ans[i] ;
  }
  BIT<ll> bit(500000) ,cit(500000) ;
  BIT<ll> x(500000),y(500000) ;
  x.add(0,n) ;
  y.add(0,m) ;
  
  rep(i,q){
    if(f[i].fi==1){
      ll now= A[f[i].se.fi-1] ;
      ll next = f[i].se.se ;
      ll aa= mp[now] ;
      ll bb = mp[next] ;
      if(aa<bb){
        sum-= cit.sum(aa,bb)  ;
        sum+= y.sum(aa,bb)*next ;
        if(aa>0) sum-= y.sum(0,aa-1)*now ;
        if(aa>0) sum+= y.sum(0,aa-1)*next ;
      }
      else{
       sum-= y.sum(bb,aa)*now ;
       sum+=cit.sum(bb,aa) ;
       if(bb>0) sum-= y.sum(0,bb-1)*now ;
       if(bb>0) sum+= y.sum(0,bb-1)*next ;
      }

      A[f[i].se.fi-1]=next ;
      bit.add(aa,-now) ;
      bit.add(bb,next) ;
      x.add(bb,1) ;
      x.add(aa,-1) ;
    }
    else{
     ll now= B[f[i].se.fi-1] ;
      ll next = f[i].se.se ;
      ll aa= mp[now] ;
      ll bb = mp[next] ;
      if(aa<bb){
        sum-= bit.sum(aa,bb)  ;
        sum+= x.sum(aa,bb)*next ;
        if(aa>0) sum-= x.sum(0,aa-1)*now ;
        if(aa>0) sum+= x.sum(0,aa-1)*next ;
      }
      else{
       sum-= x.sum(bb,aa)*now ;
       sum+=bit.sum(bb,aa) ;
       if(bb>0) sum-= x.sum(0,bb-1)*now ;
       if(bb>0) sum+= x.sum(0,bb-1)*next ;
      }
      B[f[i].se.fi-1]=next ;
      cit.add(bb,next) ;
      cit.add(aa,-now) ;
      y.add(bb,1) ;
      y.add(aa,-1) ;
    }

    cout<<sum<<endl ;
  }
  
   return 0 ;
}
