title: C# - Trying to solve Kattis Coast Length
body: <p>I'm trying to solve this Kattis Coast problem, which I found on website <a href="https://open.kattis.com/problems/coast" rel="nofollow noreferrer">https://open.kattis.com/problems/coast</a>. The goal of the challenge is to calculate the coastline. For that we have been given map of landscape. Which consists of this inupt: 5 6, 011110, 010110, 111000, 000010, 000000. The output or the result should be 20 as a integer. </p>
  
  <p>The idea was to put the input in a jagged array, one for the X-axis(N) and one for the Y-Axis(M). Then looping this arrays and calculate the differenses between land(1) and water(0). This has'nt worked out so I tried hard-coding the accurate coast line by the grid-lines, giving the answer 20. </p>
  
  <p>I don't know if I'm on the right track with arrays and calculating differenses so thats why I'm here asking for help. 
  Found this: <a href="http://challenge.csc.kth.se/2011/solutions.pdf" rel="nofollow noreferrer">http://challenge.csc.kth.se/2011/solutions.pdf</a>. At page/slide 3 there is a breif explaination of the coast length problem. It does'nt give me anything but maybe someone gets a grip of it.</p>
  
  <p>Can someone look at my program and point me to the solution?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO;
  
  namespace Task_1  // Coast
  {
  class Program
  {
  
      static void Main(string[] args)
      {
  
          int[][] nboxjaggedarray = new int[5][]
          {
              new int[6]  { 0, 1, 1, 1, 1, 0 }, // The N-input. Counted from the X-Axis
              new int[6]  { 0, 1, 0, 1, 1, 0 },
              new int[6]  { 1, 1, 1, 0, 0, 0 },
              new int[6]  { 0, 0, 0, 0, 1, 0 },
              new int[6]  { 0, 0, 0, 0, 0, 0 }
          };
  
          int[][] mboxjaggedarray = new int[6][]  // The M-input. Counted from the Y-Axis
          {
              new int[5]  { 0, 0, 1, 0, 0 },
              new int[5]  { 1, 1, 1, 0, 0 },
              new int[5]  { 1, 0, 1, 0, 0 },
              new int[5]  { 1, 1, 0, 0, 0 },
              new int[5]  { 1, 1, 0, 1, 0 },
              new int[5]  { 0, 0, 0, 0, 0 }
          };
  
          int[][] ngridlinesjaggedarray = new int[6][] //Consists of every N-line in the grid. (N = X-Axis)
          {
              new int[6]  { 0, 1, 1, 1, 1, 0 },
              new int[6]  { 0, 0, 0, 0, 0, 0 },
              new int[6]  { 1, 0, 0, 1, 1, 0 },
              new int[6]  { 1, 1, 1, 0, 1, 0 },
              new int[6]  { 0, 0, 0, 0, 1, 0 },
              new int[6]  { 0, 0, 0, 0, 0, 0 }
          };
  
          int[][] mgridlinesjaggedarray = new int[7][] // Consists of every M-line in the grid. (M = Y-Axis)
          {
              new int[5]  { 0, 0, 1, 0, 0 },
              new int[5]  { 1, 1, 0, 0, 0 },
              new int[5]  { 0, 0, 0, 0, 0 },
              new int[5]  { 0, 0, 1, 0, 0 },
              new int[5]  { 0, 0, 0, 1, 0 },
              new int[5]  { 1, 1, 0, 1, 0 },
              new int[5]  { 0, 0, 0, 0, 0 }
          };
  
  
  
          int ntotal = Sum(ngridlinesjaggedarray);
          int mtotal = Sum(mgridlinesjaggedarray);
          Console.WriteLine(ntotal + mtotal);
          Console.ReadKey();
      }
  
      public static int Sum(int[][] arr)  // Sums up every item in a JaggedArray
      {
          int total = 0;
          for (int i = 0; i &lt; arr.Length; i++)
          {
              if (arr[i] != null)
              {
                  for (int j = 0; j &lt; arr[i].Length; j++)
                  {
                      total += arr[i][j];
                  }
              }
          }
          return total;
      }
  
  }
  }
  </code></pre>