#include <iostream>
#include <algorithm>
using namespace std;

int main(void){
	int a,b,c;
	
	while(cin>>a>>b>>c && (a||b||c)){
		int i,n;
		cin>>n;
		
		int res[a+b+c+1];
		int tmp[n][3];
		fill(res,res+a+b+c+1,2);
		
		for(i=0;i<n;i++){
			int I,J,K,R;
			cin>>I>>J>>K>>R;
			if(R == 1){
				res[I] = res[J] = res[K] = 1;
				i--;
				n--;
			}
			else{
				tmp[i][0] = I;
				tmp[i][1] = J;
				tmp[i][2] = K;
			}
		}
		int size = i;
		for(i=0;i<size;i++){
			if(res[tmp[i][0]] == 1 && res[tmp[i][1]] == 1) res[tmp[i][2]] = 0;
			else if(res[tmp[i][0]] == 1 && res[tmp[i][2]] == 1) res[tmp[i][1]] = 0;
			else if(res[tmp[i][1]] == 1 && res[tmp[i][2]] == 1) res[tmp[i][0]] = 0;
		}
		for(i=1;i<=a+b+c;i++){
			cout<<res[i]<<endl;
		}
	}
	
	return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1LL<<32;;
int n,m;
ll dis[300][300];
int main()
{
    cin >> n >> m;
        int i,j,k;
        for(i = 0;i < n;i++)
            for(j = 0;j < n;j++)
			{
				if(i == j)
					dis[i][j] = 0;
				else
                    dis[i][j] = INF;
			}
        while(m--)
        {
            int a,b,c;
            cin >> a >> b >> c;
			dis[a][b] = c;
        }
        for(k = 0 ; k<n ; k++){
        for(i = 0 ; i<n ; i++){
            if(dis[i][k] == INF)
                continue;
            for(j = 0 ; j<n ; j++){
                if(dis[k][j] == INF)
                    continue;
                dis[i][j] = min(dis[i][j] , dis[i][k]+dis[k][j]);
            }
        }
    }
    bool flag = 0;
    for( i = 0 ; i<n ; i++)
    {
        if(dis[i][i] < 0)
        {
            flag = 1;
            break;
        }
    }
        if(flag == 1)
            cout << "NEGATIVE CYCLE" << endl;
        else
        {
        for(i = 0;i < n;i++)
        {
            for(j = 0;j < n;j++)
            {
                if(j == n-1)
                {
                    if(dis[i][j] == INF)
                        cout << "INF";
                    else
                        cout << dis[i][j];
                }
                else
                {
                    if(dis[i][j] == INF)
                        cout << "INF ";
                    else
                        cout << dis[i][j] << ' ';
                }
            }
            cout << endl;
        }
        }
    return 0;
}

