using System;

/*
Veidoja: Kirills Erdmanis
Grupa: 110
Praktiskais darbs: Stundu Darbs
*/

class Program
{
    static Random rnd = new Random();

    static void Main()
    {
        int r = ReadPosInt("Ievadi rindu skaitu: ");
        int c = ReadPosInt("Ievadi kolonnu skaitu: ");

        int[,] mat = MakeRandomMatrix(r, c);

        int choice;
        do
        {
            Console.WriteLine();
            Console.WriteLine("1 - Izdrukāt matricu");
            Console.WriteLine("2 - Lielākais un mazākais elements");
            Console.WriteLine("3 - Kolonnu summas");
            Console.WriteLine("4 - Galvenās diagonāles summa");
            Console.WriteLine("5 - Blakusdiagonāles summa");
            Console.WriteLine("6 - Transponēt matricu");
            Console.WriteLine("7 - Meklēt skaitli");
            Console.WriteLine("0 - Iziet");
            Console.Write("Izvēle: ");

            choice = GetMenuChoice(0, 7);
            Console.WriteLine();

            switch (choice)
            {
                case 1:
                    PrintColoredTable(mat);
                    break;
                case 2:
                    Console.WriteLine("Max: " + MaxValue(mat));
                    Console.WriteLine("Min: " + MinValue(mat));
                    break;
                case 3:
                    int[] s = ColSums(mat);
                    Console.WriteLine(string.Join(", ", s));
                    break;
                case 4:
                    Console.WriteLine(DiagonalSum(mat));
                    break;
                case 5:
                    Console.WriteLine(SideDiagonalSum(mat));
                    break;
                case 6:
                    mat = Transp(mat);
                    break;
                case 7:
                    int target = ReadInt("Ievadi skaitli: ");
                    var pos = Find(mat, target);
                    if (pos.found)
                        Console.WriteLine(pos.row + " " + pos.col);
                    else
                        Console.WriteLine("Nav");
                    break;
            }

        } while (choice != 0);
    }

    static int ReadPosInt(string text)
    {
        int x;
        do { Console.Write(text); }
        while (!int.TryParse(Console.ReadLine(), out x) || x < 1);
        return x;
    }

    static int ReadInt(string text)
    {
        int y;
        for (;;)
        {
            Console.Write(text);
            if (int.TryParse(Console.ReadLine(), out y)) return y;
        }
    }

    static int GetMenuChoice(int min, int max)
    {
        int n;
        while (!int.TryParse(Console.ReadLine(), out n) || n < min || n > max)
            Console.Write("Ievadi skaitli " + min + "-" + max + ": ");
        return n;
    }

    static int[,] MakeRandomMatrix(int r, int c)
    {
        int[,] m = new int[r, c];
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                m[i, j] = rnd.Next(-100, 101);
        return m;
    }

    static void PrintColoredTable(int[,] m)
    {
        int rr = m.GetLength(0);
        int cc = m.GetLength(1);

        Console.WriteLine();

        for (int i = 0; i < rr; i++)
        {
            for (int j = 0; j < cc; j++)
            {
                Console.BackgroundColor = RandomColor();
                Console.ForegroundColor = ConsoleColor.White;

                Console.Write($"{m[i, j],6}");

                Console.ResetColor();
            }
            Console.WriteLine();
        }

        Console.WriteLine();
    }

    static ConsoleColor RandomColor()
    {
        ConsoleColor[] colors = {
            ConsoleColor.DarkBlue, ConsoleColor.DarkCyan, ConsoleColor.DarkMagenta,
            ConsoleColor.DarkYellow, ConsoleColor.DarkGreen, ConsoleColor.DarkRed,
            ConsoleColor.Blue, ConsoleColor.Cyan, ConsoleColor.Magenta
        };
        return colors[rnd.Next(colors.Length)];
    }

    static int MaxValue(int[,] m)
    {
        int max = m[0, 0];
        foreach (int x in m)
            if (x > max) max = x;
        return max;
    }

    static int MinValue(int[,] m)
    {
        int min = m[0, 0];
        foreach (int x in m)
            if (x < min) min = x;
        return min;
    }

    static int[] ColSums(int[,] m)
    {
        int col = m.GetLength(1);
        int row = m.GetLength(0);
        int[] s = new int[col];
        for (int j = 0; j < col; j++)
            for (int i = 0; i < row; i++)
                s[j] += m[i, j];
        return s;
    }

    static int DiagonalSum(int[,] m)
    {
        int size = Math.Min(m.GetLength(0), m.GetLength(1));
        int sum = 0;
        for (int i = 0; i < size; i++)
            sum += m[i, i];
        return sum;
    }

    static int SideDiagonalSum(int[,] m)
    {
        int r = m.GetLength(0);
        int c = m.GetLength(1);
        int size = Math.Min(r, c);
        int sum = 0;
        for (int i = 0; i < size; i++)
            sum += m[i, c - 1 - i];
        return sum;
    }

    static int[,] Transp(int[,] m)
    {
        int r = m.GetLength(0);
        int c = m.GetLength(1);
        int[,] t = new int[c, r];

        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                t[j, i] = m[i, j];

        return t;
    }

    static (bool found, int row, int col) Find(int[,] m, int x)
    {
        int rr = m.GetLength(0);
        int cc = m.GetLength(1);

        for (int i = 0; i < rr; i++)
            for (int j = 0; j < cc; j++)
                if (m[i, j] == x)
                    return (true, i, j);

        return (false, -1, -1);
    }
}